/*初级篇:简单数组中for和foreach的用法*/
int nums[3] = { 0, 1, 2 };
for (int num : nums)
{
cout << num << ",";
}
for each (int num in nums)
{
cout << num << ",";
}
cout << endl;
//输出为:0,1,2,0,1,2,
分割线///
/*高级篇:map数组中for和foreach的用法*/
map<string, string> mapp[3];
mapp[0]["a0"] = "b0";
mapp[1]["a1"] = "b1";
mapp[2]["a1"] = "b2";
for (map<string, string> num : mapp)
{
cout << num["a1"]<<",";
}
for each (map<string, string> num in mapp)
{
cout << num["a1"]<< ",";
}
cout << endl;
//输出为:,b1,b2,,b1,b2,
分割线///
/*初级篇:简单向量中for和foreach的用法*/
/*vector相当于动态数组,在不知道数组长度情况下使用*/
vector<int>vs = { 0, 1, 2, 3, 4 };
for (int v : vs)
{
cout << v << ",";
}
for each (int v in vs)
{
cout << v << ",";
}
cout << endl;
//输出为:0,1,2,3,4,0,1,2,3,4,
分割线///
/*高级篇:封装map向量中for和foreach的用法*/
vector<map<string, string>>vec;
map<string, string> map1,map2;
map1["a"] = "a1";
map2["a"] = "a2";
vec.push_back(map1);
vec.push_back(map2);
for (map<string, string> num : vec)
{
cout << num["a"] << ",";
}
for each (map<string, string> num in vec)
{
cout << num["a"] << ",";
}
cout << endl;
//输出为:a1,a2,a1,a2,
这个好例子对map的理解就差不多够用了,另外再附上博友详细的map用法讲解,以备学习。