C++11特性--基于范围的for循环,新的STL容器,新的STL方法( cbegin(),cend(),crbegin(),crend())
来源:互联网 发布:电脑超频软件 编辑:程序博客网 时间:2024/05/01 12:31
1.模板和STL方面的修改
(1)基于范围的for循环
*对于内置数组以及包含方法begein()和end()的类合和STL容器,基于范围的for循环可简化为它们编写循环的工作
*如果要在循环中修改数组或容器的每个元素,可使用引用类型
Example:
int main()
{
int arr[]{1,2,3};
for(auto x:arr)
{
cout<<x<<ends;
}
cout<<endl;
for(auto &x:arr)
{
x=0;
}
for(auto x:arr)
{
cout<<x<<ends;
}
return 0;
}
output:
1 2 3
0 0 0
(2)新的STL容器
*array模板,可以指定元素类型和固定的元素个数,虽然长度固定,
但是,因为array包含begin()和end(),使得array对象能使用于众多基于范围的STL算法
Example:
int main()
{
array<int,5> arr{1,3,5,4,2};
sort(arr.begin(),arr.end());
for(auto x:arr)
{
cout<<x<<ends;//1 2 3 4 5
}
return 0;
}
*forward_list,unordered_map,unordered_multimap,unordered_set,unordered_multiset
其中forward_list是一种单链表,只能沿着一个方向遍历,其余四种都是使用哈希表实现的。
(3)新的STL方法
cbegin(),cend(),crbegin(),crend()分别是的begin(),end(),rbegin(),rend()的const版本
Example:
int main()
{
forward_list<int> flst{1,2,3};
for(auto p=flst.cbegin();p!=flst.cend();p++)
{
cout<<*p<<ends;//1 2 3
}
return 0;
}
(4)摒弃export
C++11终止了export用法,但仍然保留了关键字export,供以后使用
(5)尖括号
C++11不在要求声明嵌套模板是使用空格将尖括号分开
Example:
vector<list<int>> vec;//ok
(1)基于范围的for循环
*对于内置数组以及包含方法begein()和end()的类合和STL容器,基于范围的for循环可简化为它们编写循环的工作
*如果要在循环中修改数组或容器的每个元素,可使用引用类型
Example:
int main()
{
int arr[]{1,2,3};
for(auto x:arr)
{
cout<<x<<ends;
}
cout<<endl;
for(auto &x:arr)
{
x=0;
}
for(auto x:arr)
{
cout<<x<<ends;
}
return 0;
}
output:
1 2 3
0 0 0
(2)新的STL容器
*array模板,可以指定元素类型和固定的元素个数,虽然长度固定,
但是,因为array包含begin()和end(),使得array对象能使用于众多基于范围的STL算法
Example:
int main()
{
array<int,5> arr{1,3,5,4,2};
sort(arr.begin(),arr.end());
for(auto x:arr)
{
cout<<x<<ends;//1 2 3 4 5
}
return 0;
}
*forward_list,unordered_map,unordered_multimap,unordered_set,unordered_multiset
其中forward_list是一种单链表,只能沿着一个方向遍历,其余四种都是使用哈希表实现的。
(3)新的STL方法
cbegin(),cend(),crbegin(),crend()分别是的begin(),end(),rbegin(),rend()的const版本
Example:
int main()
{
forward_list<int> flst{1,2,3};
for(auto p=flst.cbegin();p!=flst.cend();p++)
{
cout<<*p<<ends;//1 2 3
}
return 0;
}
(4)摒弃export
C++11终止了export用法,但仍然保留了关键字export,供以后使用
(5)尖括号
C++11不在要求声明嵌套模板是使用空格将尖括号分开
Example:
vector<list<int>> vec;//ok
- C++11特性--基于范围的for循环,新的STL容器,新的STL方法( cbegin(),cend(),crbegin(),crend())
- 【C/C++开发】容器set和multiset,C++11对vector成员函数的扩展(cbegin()、cend()、crbegin()、crend()、emplace()、data())
- 实战c++中的vector系列--C++11对vector成员函数的扩展(cbegin()、cend()、crbegin()、crend()、emplace()、data())
- STL deque的crend方法(11)
- c++11的新特性---基于范围的for循环
- 类内初始化,范围for语句,vector的vector,容器的cbegin和cend
- C++11 新特性(6) 基于范围的for循环
- C++11新特性-基于范围的for循环
- STL array的crend方法(8)
- STL deque的crbegin方法(10)
- STL array的cend方法(6)
- STL deque的cend方法(8)
- STL array的crbegin方法(7)
- STL deque的方法cbegin(7)
- C++11新特性之基本范围的For循环(range-based-for)
- STL array的cbegin(5)
- For循环的新特性
- for循环的新特性
- android 基础知识 十八
- POJ 2253 Frogger
- hdu 4324
- WebGame开发总结
- MyEclipse里有些特殊符号,不知道还以为是乱码
- C++11特性--基于范围的for循环,新的STL容器,新的STL方法( cbegin(),cend(),crbegin(),crend())
- 英英词典client
- ubuntu小技巧
- ASP.NET 3.5 如何安裝在windows 2003的IIS 6.0
- gVim的字体和背景颜色设置
- CTime类
- 使用MiniTools更方便的调试TQ210裸机程序
- Tower Defence hdu3958
- 鄙人flex小试身手,做了一个计算器