从上个c++版本过去11年了,c11有了更多的特性,使各个模块组装的的更合适。
“ It now supports lambda expressions, automatic type deduction of objects, uniform initialization syntax(统一初始化语法), delegating constructors(委派构造函数), deleted and defaulted function declarations, nullptr
, and most importantly, rvalue references — a feature that augurs a paradigm shift(思考模式的转移) in how one conceives and handles objects(怎样构思和解决类). And that’s just a sample.”
The C++11 Standard Library was also revamped with new algorithms, new container classes, atomic operations(原子操作数), type traits, regular expressions, new smart pointers(智能指针), async()
facility, and of course a multithreading library.
98版c++时还争辩到下版c++一定要增加GC垃圾回收,并且可能不再支持多线程。but现在看来依然缺少GC功能但是增加包含了一系列的线程库。
Lambda表达式格式:
[capture](parameters)->return-type {body}
举例:
假设你要计算一个字符串中有多少大写字母。比如“Hello World!”,有2个大写字母,以下的lambda表达式判断哪个字母是大写。每找到一个大写字母,Uppercase自增存放。
int main()
{
char s[]="Hello World!";
int Uppercase = 0; //modified by the lambda
for_each(s, s+sizeof(s), [&Uppercase] (char c) {
if (isupper(c))
Uppercase++;
});
cout<< Uppercase<<" uppercase letters in: "<< s<<endl;
}
自动类型:
可以应用某些冗长的变量类型替代:vector<int>::const_iterator ci=vi.begin(); 被替代为
auto ci=vi.begin();
auto x=0; //x has type int because 0 is int
auto c='a'; //char
auto d=0.5; //double
auto national_debt=14400000000000LL;//long long