#include<iostream>
#include<vector>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
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 << "name:" << (*it).m_Name << " age:" << (*it).m_Age << endl;
}
}
void test02()
{
vector<Person*>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
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 << "name:" << (*it)->m_Name << " age:" << (*it)->m_Age << endl;
}
}
int main()
{
test02();
system("pause");
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
void test03()
{
vector<vector<int>>v;
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>v4;
for (int i = 0; i < 4; i++)
{
v1.push_back(i + 1);
v2.push_back(i + 2);
v3.push_back(i + 3);
v4.push_back(i + 4);
}
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
for (vector<vector<int>>::iterator it = v.begin(); it < v.end(); it++)
{
for (vector<int>::iterator it1 = (*it).begin(); it1 < (*it).end(); it1++)
{
cout << *it1 << " " ;
}
cout << endl;
}
}
int main()
{
test03();
system("pause");
return 0;
}