c++11 for (auto & item:v)
作者:互联网
//遍历容器元素
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class A
{
public:
int id=0;
};
int main()
{
vector<A> v;
A a0,a1,a2;
a0.id=1;a1.id=2;a2.id=3;
v.push_back(a0);v.push_back(a1);v.push_back(a2);
// for (std::vector<A>::iterator item = v.begin(); item != v.end(); ++item)//注释的四行也是可以的
// {
// cout<<item->id<<endl;
// }
for (auto & item:v) //c++11语法
{
cout<<item.id<<endl;
}
string s="Jack-Ma";
for (auto & item:s)
{
cout<<item<<endl;
}
system("pause");
return 0;
}
标签:11,a0,auto,back,c++,a1,item,id 来源: https://blog.csdn.net/weixin_39926429/article/details/121564772