实例代码如下:
/*
使用vector容器进行打印姓名和年龄的操作
*/
#include <iostream>
using namespace std;
#include <vector>
#include <string>
class Person
{
public:
Person(string name,int age)
{
this->m_Name = name;
this->m_Age = age;
}
int m_Age;
string m_Name;
};
void test01()
{
vector<Person> v;
Person p1("baga",666);
Person p2("maka",777);
Person p3("baka",888);
Person p4("daota",999);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
for(vector<Person>::iterator it = v.begin();it!=v.end();it++)
{
cout << "姓名为:"<< it->m_Name<<" "<< "年龄为 "<< it->m_Age << endl;
}
}
int main()
{
system("color f5");
test01();
system("pause");
return 0;
}
好好学习好好学习好好学习