其他分享
首页 > 其他分享> > vector自定义数据的使用

vector自定义数据的使用

作者:互联网

黑马程序员187
#include
#include
using namespace std;
#include
#include

class person
{
public:
person(string m_name, int m_age)
{
this->name = m_name;
this->age = m_age;
}
string name;
int age;
};
void test()
{
vector v;
person p1(“tom”, 10);
person p2(“jerry”, 20);
person p3(“kangkang”, 30);
person p4(“jane”, 40);
person p5(“mike”, 50);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//vector::iterator itBegin = v.begin();
//vector::iterator itEnd = v.end();
//while (itBegin != itEnd)
//{
//
// cout << “name:” << itBegin->name << “\tage:” << itBegin->age << endl;
// itBegin++;
//}

for (vector<person>::iterator it = v.begin(); it!=v.end(); it++)
{
	cout << "name:" << it->name << "\tage:" <<it->age << endl;
}

}
//存放自定义指针
void test02()
{
vector<person*> v;
person p1(“tom”, 10);
person p2(“jerry”, 20);
person p3(“kangkang”, 30);
person p4(“jane”, 40);
person p5(“mike”, 50);
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 << "name:" << (*it)->name << "\tage:" << (*it)->age << endl;
}

}

int main()
{
/test();/
test02();
}

标签:定义数据,age,back,person,vector,使用,push,name
来源: https://blog.csdn.net/qq_43752920/article/details/120379721