字符串 string str = "this is a string"; for(auto ch : str) cout << ch <&
string str = "this is a string";
for(auto ch : str)
cout << ch << endl;
等价于
for(int i = 0; i < str.size(); i++)
cout << str[i] << endl;
vector
vector<int> v = {1, 2, 3, 4, 5};
for(auto i : v)
cout << i << endl;
等价于
for(int i = 0; i < v.size(); i++)
cout << v[i] << endl;
二维vector
vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for(auto i : v)
for(auto j : i)
cout << j << endl;
等价于
for(int i = 0; i < v.size(); i++)
for(int j = 0; j < v[i].size(); j++)
cout << v[i][j] << endl;
int ary[] = {1, 2, 3, 4, 5};
for(auto i : ary)
cout << i << endl;
等价于
for(int i = 0; i < 5; i++)
cout << ary[i] << endl;
map
map<char, int> m = {{'a', 1}, {'b', 2}, {'c', 3}};
for(auto t : m)
cout << t.first << ' ' << t.second << endl;
等价于
for(map<char, int> :: iterator itr = m.begin(); itr != m.end(); itr++)
cout << itr ->first << ' ' << itr ->second << endl;
到此这篇关于c++11的for循环的新用法的文章就介绍到这了,更多相关C++11 for循环内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: C++11的for循环的新用法(推荐)
本文链接: https://lsjlt.com/news/156971.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0