【C++11(上)】—— 我与C++的不解之缘(三十)
一、C++11
这里简单了解一下C++发展好吧:
C++11是C++的第二个大版本,也是自C++98以来最重要的一个版本。
它引入了大量的更改,它曾被人们称为C++0x,因为它被期待在2010年之前发布;但在2011年8月12日才被采纳。
C++03到C++11花了8年时间,这是迄今为止最长的版本间隙;自C++11起C++就有规律的每三年更新一次。
二、列表初始化
C++98中的{}
在之前C++98中是支持对一般数组和结构体使用{}来初始化的
struct A { int _x; int _y; }; int main() { int arr1[] = { 1,2,3,4,5 };//数组使用{}进行初始化 int arr2[3] = { 0 }; A a = { 2,2 };//结构体使用{}进行初始化 return 0; }
这些在C语言当中都是支持的。
C++11中的{}
- C++11中的{},想统一初始化方式,想要实现一切对象皆可以使用{}初始化;{}初始化也称为列表初始化。
- 内置类型支持使用{}进行初始化;自定义类型也支持,但是自定义类型支持的本质是类型转化,会产生临时对象,最后被优化成了直接构造
- {}在初始化的过程中,也可以省略掉=
class Date { public: Date(int year = 1, int month, int day) :_year(year) ,_month(month) ,_day(day) { cout cout //C++11 //内置类型支持{}初始化 int a = { 1 }; //自定义类型也支持{}初始化 //本质上是{2025,3,31}构造一个Date对象,再进行拷贝构造 //编译器优化二合一成{2025,3,31}直接构造 Date d1 = { 2025,3,31 }; //这里d2引用的就是{2025,3,31}构造的临时对象 const Date& d2 = { 2025,3,31 }; //C++98中支持单参数时 隐式类型转换,可以不写{} Date d3 = { 2025 }; Date d4 = 2025; //可以省略掉= int b{ 2 }; Date d5{ 2025 }; const Date& d6{ 2006,7,20 }; //只有使用{}时,才可以省略= //Date d7 2025; return 0; } vector 2025,3,31 }; v.push_back(d); v.push_back(Date(2025, 1, 1));//匿名对象 //相比较于有名对象和你们对象,这里使用{}还是很便利的 v.push_back({ 2025, 1, 1 }); return 0; } std::initializer_list 10, 20, 30 }; cout 1,2,3,4,5 }); vector 1,2,3,4,5 }; const vector 1,2,3,4,5 }; //这里pair对象的{} 吃石化和map的initializer_list的构造相结合 map {"sort", "排序"}, {"string", "字符串"} }; // initializer_list版本支持的赋值 v1 = { 10,20,30,40,50 }; return 0; } //左值可以取地址 //p,b,c,*p,str,str[0]都是左值 int* p = new int(0); int b = 1; const int& c = b; *p = 3; string str = "12345"; str[0] = '0'; cout int* p = new int(0); int b = 1; const int c = b; *p = 10; string s("111111"); s[0] = 'x'; double x = 1.1, y = 2.2; //左值引用 int& r1 = b; int*& r2 = p; int& r3 = *p; string& r4 = s; char& r5 = s[0]; //右值引用 int&& rr1 = 10; double&& rr2 = x + y; double&& rr3 = fmin(x, y); string&& rr4 = string("11111"); //左值引用不能直接引用右值,const左值引用可以引用右值 const int& rx1 = 10; const double& rx2 = x + y; const double& rx3 = fmin(x, y); const string& rx4 = string("11111"); //右值引用不能直接引用左值,但可以引用move(左值) int&& rrx1 = move(b); int*&& rrx2 = move(p); int&& rrx3 = move(*p); string&& rrx4 = move(s); string&& rrx5 = (string&&)s; // b、r1、rr1都是变量表达式,都是左值 cout string s1 = "666666"; const string& s2 = s1 + s1;//const 左值引用延长临时对象生命周期 //s2 += "999";//const左值引用不能修改 string&& s3 = s1 + s1;//右值引用延长临时对象生命周期 s3 += "999";//可以进行修改 cout cout cout cout int i = 1; const int ci = 2; func(i); //调用func(int& x) func(ci); //调用func(const int& x) func(3); //调用func(int&& x),如果没有就调用func(const int& x) func(std::move(i)); //调用func(int&& x),如果没有就调用func(const int& x) int&& x = 1; func(x); //右值引用变量的属性是左值,调用func(int& x) func(std::move(x)); //x是左值,move(x)是右值,调用func(int&& x) return 0; } string str; int end1 = num1.size() - 1, end2 = num2.size() - 1; // 进位 int next = 0; while (end1 = 0 || end2 = 0) { int val1 = end1 = 0 ? num1[end1--] - '0' : 0; int val2 = end2 = 0 ? num2[end2--] - '0' : 0; int ret = val1 + val2 + next; next = ret / 10; ret = ret % 10; str += ('0' + ret); } if (next == 1) str += '1'; reverse(str.begin(), str.end()); return str; } //这里进行传值返回的代价就非常大了 vector vector vv[i].resize(i + 1, 1); } for (int i = 2; i
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。