C++11新特性
馨er BOSS

C++11 是2011年官方为c++带来的新语法新标准

auto声明

让编译器根据初始值类型直接推断出变量的类型

1
2
3
4
5
6
7
8
// set的迭代器原始写法:
for(set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
// auto写法:
for(auto it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}

基于范围的for循环

遍历数组中的每一个元素时使用时比较方便

1
2
3
4
5
int arr[4] = {0, 1, 2, 3};
for (int i : arr)
cout << i << endl; // 输出数组中的每一个元素的值,每个元素占据一行
for (int &i : arr) // i为引用变量
i = i * 2; // 将数组中的每一个元素都乘以2,arr[4]的内容变为了{0, 2, 4, 6}

当然上面的也可以替换为auto

转换类型

stoi 转换为int

stod 转换为double

stof 转换为float

stold 转换为long double

stol 转换为long

stoll 转换为long long

stoul 转换为unsigh long

stoull 转换为unsigh long long

  • 本文标题:C++11新特性
  • 本文作者:馨er
  • 创建时间:2021-05-06 17:33:37
  • 本文链接:https://sjxbbd.vercel.app/2021/05/06/4f237a55e414/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!