我对学习C++很感兴趣,我找到了一本适合我的书。然而,尽管这本书是很新的,但一些读者批评说,这本书的早期版本并没有真正深入到新的C++标准(如C++11、C++14、C++17)中,而是使用了“旧C++标准”(即pre C++11)。
虽然我检查了更新的版本是否涵盖了C++11等的新方面(它似乎涵盖了相当多的方面),但我不确定是否可能学习预C++11标准会效率低下?看起来新版本确实涵盖了新的C++标准(C++11和更高版本),但我们假设它没有。我所获得的知识会不会过时?
同样,我对C++还是新手,所以我不确定新的C++标准是否完全改变了语言,可能会删除/改变我将通过这本(可能过时的)书学习的语言的一部分(整体)。我只想避免学习(整体)语言的一部分,这些部分不再被使用,或者现在使用不同的语言(用更新的标准)。但是,如果所有新的C++标准都添加了新特性(但没有删除/更改旧特性),我不认为从这本书中学习将是一个问题,因为我以后可以简单地学习新特性。
那么,新的C++标准在哪些方面改变了语言,你会说从一本可能过时的书中学习是个问题吗?
发布于 2021-01-18 01:00:49
C++11中的一些代码已经更改,主要更改是:
- Pre-C++11, it has been recommended to use output parameter to avoid copy:空MakeVector(std::Vector&v){ v.clear();//填充v;}
- Since C++11, no longer required for movable types, and return by value is the way to go:向量MakeVector() {std::向量v;//填充v;返回v;}
迭代容器的
- Pre-C++11, it would be a mix between index and iterator way:void (std::vector&v){ for (std::vector::iterator it = v.begin();it != v.end();++it) { /* *it .*/} for (std::size_t i= 0;i= v.size();++i) { /* vi .*}}
- Since C++11, it is shorter (and optimal (`end()` is computed only once)):void (std::vector&v){ for (int& e: v) { /* .*/}
- Pre-C++11, type should be explicitly written:{std::vector= std::find(v.begin(),v.end(),42);// .}
- Since C++11, type can be deduced:{ auto = std::find(v.begin(),v.end(),42);// .}
创建谓词的
- Pre-C++11, predicate should be done outside of the function as function or class:bool less_than_42(int i) {返回i< 42;} struct less_than_x { less_than_x(int x):x(x) {} bool运算符()( int i) const { it1 i< x;} int x;};void Foo(std::vector&v,int x) {std::less_than_x::iterator it1= std::find_if(v.begin(),v.end,less_than_42);it2 = std::find_if(v.begin(),v.end(),less_than_x(x));// .}
- Since C++11, lambda simplify stuff (dedicated function might still be useful to avoid to duplicate lambda though):void (std::vector& v,int x) { auto it1 = std::find_if(v.begin(),v.end(),int e{ it2 e< 42;});auto it2= std::find_if(v.begin(),v.end(),x{返回e< x;});// .}
还有其他一些更改,但这些更改减少了C++03编码方式的有效性。
https://stackoverflow.com/questions/65767289
复制相似问题