C++11特性--基于范围的for循环,新的STL容器,新的STL方法( cbegin(),cend(),crbegin(),crend())
2013-07-24 18:24
836 查看
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循环
- C++11 新特性(6) 基于范围的for循环
- C++11特性:auto关键字与基于范围的for循环
- c++11的新特性---基于范围的for循环
- c++11——基于范围的for循环
- c++11的一些特性auto和范围for循环
- C++11特性——基于范围的for语句
- c++11之一: 基于范围的for循环
- C++11 基于范围的for循环
- C++11之基于范围的for循环(影响面广)
- 实战c++中的vector系列--C++11对vector成员函数的扩展(cbegin()、cend()、crbegin()、crend()、emplace()、data())
- C++11基于范围的for循环
- C++11之基于范围的for循环
- 实战c++中的vector系列--C++11对vector成员函数的扩展(cbegin()、cend()、crbegin()、crend()、emplace()、data())
- C++11 学习笔记 基于范围的for循环
- 基于范围的for循环(C++11)
- C++11新特性:range based for loop-范围for循环基本使用方法
- C++11 基于范围的for循环
- c++11 基于范围的for循环
- 基于范围的FOR循环(C++11标准新内容)