【c++11】范围for循环
来源:互联网 发布:linux切换到指定目录 编辑:程序博客网 时间:2024/06/14 10:31
参考:http://blog.csdn.net/aspnet_lyc/article/details/30971195
#include <iostream>#include <map>#include <vector>#include <string>#include <typeinfo>#include <cassert>using namespace std;struct Test{int i;int j;};int main(){map<string, int> ms;ms.insert(make_pair("a", 1));ms.insert(make_pair("b", 2));ms.insert(make_pair("c", 3));ms.insert(make_pair("d", 4));// for(元素类型 元素变量名:容器变量名)for (const auto& element: ms){assert(typeid(element) == typeid(map<string, int>::value_type));//assert(typeid(element) == typeid(pair<string, int>)); // false!assert(typeid(element) == typeid(pair<const string, int>));cout << typeid(element).name() << endl; // print typecout << element.first << ":" << element.second << endl;}int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};for (const auto& element: a){assert(typeid(element) == typeid(int)); cout << element << ends;}cout << endl;vector<int> v(a, a+10);for (auto element: v){assert(typeid(element) == typeid(int)); cout << element << ends;}cout << endl;char str[10] = "Hello";for (const auto& element : str){assert(typeid(element) == typeid(char)); cout << element; // Hello}cout << endl;string s = "Hello";for (const auto& element : s){assert(typeid(element) == typeid(char)); cout << element;}cout << endl; vector<Test> vTest(10);for (const auto& element: vTest){assert(typeid(element) == typeid(Test)); cout << element.i << "," << element.j << ends; // 0,0 ...}cout << endl;// Test t; // 0,0// Test t1(10, 2); // error: 无此构造函数//Test test[10] = { 0,1,2,3 }; // 初始化:test[0]:i:0,j:1; test[1]:i:2,j:3; 其他元素:0,0Test test[] = { 0,1,2,3 };// 数组长度为2,初始化:test[0]:i:0,j:1; test[1]:i:2,j:3;for (const auto& element: test){assert(typeid(element) == typeid(Test)); cout << element.i << "," << element.j << ends; // 0,1 2,3}cout<<endl;return 0;}
0 0
- 【c++11】范围for循环
- C++11: 范围for循环
- c++primer之范围for循环
- c++ 11 基于范围的for循环
- C++11 基于范围的for循环
- 基于范围的for循环(C++11)
- [C++] 使用基于范围的for循环操作string
- 基于范围的for循环
- 基于范围的for循环
- 范围for 循环的坑
- C++11 新特性(6) 基于范围的for循环
- C++11新特性:范围for循环和lambda表达式
- c++11的一些特性auto和范围for循环
- C++11 学习笔记 基于范围的for循环
- C++11之基于范围的for循环
- c++11的一些特性auto和范围for循环
- C++11新特性-基于范围的for循环
- 基于范围的for循环(C++11)
- 读入一个句子,把所有词都变成大写,输出,每个词占一行
- 阿里云服务器cent0S安装web环境PHP+MySql+Nginx,给nginx添加网站
- this 指向区分
- TPLINK WR703N刷OPENWRT
- 合伙人还有另一个创业的朋友一起
- 【c++11】范围for循环
- poj1251 Jungle Roads (最小生成树)
- android 上拉下拉我只要最强大的
- Deep Learning: Doubly Easy and Doubly Powerful with GraphLab Create
- 项目中问题总结:
- ImageView控件
- 狄更斯 双城记
- HDU 1016 Prime Ring Problem
- java 序列化和反序列化