//遍历vector容器
#include <iostream>
#include <vector>
using namespace std;
struct ST{
int id;
double db;
};
void Origin(int num,vector<ST>& vt){
int m=num;
ST temp;
for(int i=0;i<m;++i){
temp.id=i+1;
temp.db=(i+1)*10;
vt.push_back(temp);
}
}
void Iter_for(vector<ST>& vt){
ST temp;
vector<ST>::iterator iter;
for(iter=vt.begin();iter!=vt.end();++iter){
temp=*iter;
cout<<"id: "<<temp.id<<",db: "<<temp.db<<endl;
}
}
void at_for(vector<ST>& vt){
ST temp;
int i=0;
int m=vt.size();
for(i=0;i<m;++i){
temp = vt.at(i);
cout<<"id: "<<temp.id<<",db: "<<temp.db<<endl;
}
}
int main()
{
ST tmp;
vector<ST> myvt;
Origin(5,myvt);
int size=myvt.size();
cout<<"size: "<<size<<endl;
cout<<"Iterator output!"<<endl;
Iter_for(myvt);
at_for(myvt);
cout<<"empty() usage:"<<endl;
while(!myvt.empty()){
tmp=myvt.back();
cout<<"id: "<<tmp.id<<",db: "<<tmp.db<<endl;
myvt.pop_back();
}
return 0;
}
C++ STL 遍历vector
最新推荐文章于 2024-08-17 16:50:55 发布