#include<iostream>
using namespace std;
#include<vector>
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test0201()
{
vector<Person> v;
Person p1("xian", 11);
Person p2("si", 22);
Person p3("fan", 33);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << it->m_Name << "\t"
<< "年龄:" << it->m_Age << endl;
}
}
//存放自定义数据类型的指针
void test0202()
{
vector<Person*> v;
Person p1("xian", 11);
Person p2("si", 22);
Person p3("fan", 33);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it)->m_Name << "\t"
<< "年龄:" << (*it)->m_Age << endl;
}
}
int main()
{
test0201();
test0202();
system("pause");
return 0;
}
网友评论