#include
#include
using namespace std;
vector <数据类型> 对象名;
vector<int> v;
//插入数据 push_back()
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vector<int>::iterator itBegin = v.begin();
vector<int>::iterator itEnd = v.end();
while (itBegin != itEnd)
{
cout << *itBegin << endl;
itBegin++;
}
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}
for_each()相当于:
template<typename InputIterator, typename Function>
Function for_each(InputIterator beg, InputIterator end, Function f) {
while(beg != end)
f(*beg++);
}
(1)先创建一个仿函数myprint()
void myprint(int val)
{
cout << val << endl;
}
(2)使用for_each()
for_each(v.begin(), v.end(), myprint);
自定义一个Person的数据类型:
//自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
创建vector对象
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 << "姓名:" << it->m_Name << " 年龄:" << (*it).m_Age << endl;
}
}
int main(void)
{
test01();
cout << "自定义数据类型Person" << endl;
test02();
return 0;
}
//创建嵌套容器对象--大容器
vector<vector<int>> v;
//创建小容器对象
vector<int> v1;
vector<int> v2;
vector<int> v3;
//给小容器赋值
for (int i = 0; i < 5; i++)
{
v1.push_back(i + 1);
v2.push_back(i + 10);
v3.push_back(i + 100);
}
//将小容器添加进大容器
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
循环嵌套
//遍历
for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
{
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout << *vit << " ";
}
cout << endl;
}
main测试:
int main(void)
{
/*
test01();
cout << "====自定义数据类型Person====" << endl;
test03();*/
cout << "====容器的嵌套====" << endl;
test04();
return 0;
}
结果:
#include
#include
#include //标准算法头文件
using namespace std;
//
void myprint(int val)
{
cout << val << endl;
}
void test01()
{
vector<int> v;
//插入数据 push_back()
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
//遍历:1.迭代器while 2.迭代器for
/*1.
vector::iterator itBegin = v.begin();
vector::iterator itEnd = v.end();
while (itBegin != itEnd)
{
cout << *itBegin << endl;
itBegin++;
}
*/
/*2
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}*/
//3.for_each
for_each(v.begin(), v.end(), myprint);
/*
template
Function for_each(InputIterator beg, InputIterator end, Function f) {
while(beg != end)
f(*beg++);
}
*/
}
//自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
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 << "姓名:" << it->m_Name << " 年龄:" << (*it).m_Age << endl;
}
}
//指针类型的对象
void test03()
{
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 << "姓名:" << (*it)->m_Name << " 年龄:" << (*it)->m_Age << endl;
}
}
//容器的嵌套
void test04()
{
//创建嵌套容器对象--大容器
vector<vector<int>> v;
//创建小容器对象
vector<int> v1;
vector<int> v2;
vector<int> v3;
//给小容器赋值
for (int i = 0; i < 5; i++)
{
v1.push_back(i + 1);
v2.push_back(i + 10);
v3.push_back(i + 100);
}
//将小容器添加进大容器
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
//遍历
for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
{
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout << *vit << " ";
}
cout << endl;
}
}
int main(void)
{
/*
test01();
cout << "====自定义数据类型Person====" << endl;
test03();*/
cout << "====容器的嵌套====" << endl;
test04();
return 0;
}