#include<iostream>
using namespace std;
#include<vector>
class person
{
public:
person(string a, int b) :name(a), age(b) {}
string name;
int age;
};
void text01() {
vector<person> v;
person p1("a",11);
person p2("b", 12);
person p3("c", 13);
person p4("d", 14);
person p5("e", 15);
//向容器中加入数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
for (vector<person>::iterator it=v.begin(); it!=v.end(); it++)
{
cout << it->name<<":"<<(*it).age << endl;
}
}
//存放自定义数据类型指针
void text02() {
vector<person*> v;
person p1("a", 11);
person p2("b", 12);
person p3("c", 13);
person p4("d", 14);
person p5("e", 15);
//向容器中加入数据
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
for (vector<person*>::iterator it = v.begin(); it != v.end(); it++) {
cout << (*it)->name << "-" << (*it)->age << endl;
}
}
int main() {
person* p;
p = new person{ "wa",12 };
cout << p->name << endl;
cout << "-------------" << endl;
text01();
cout << "-------------" << endl;
text02();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)