Vector [contenedor de C ++]

envase: vector

algoritmo: for_each

Iterador: vector<int>::iterator

1. Vector almacena tipos de datos integrados

#include <iostream>
#include <vector>
#include <algorithm>     //算法
using namespace std;

//打印函数
void myPrint(int val)
{
	cout << val << endl;
}

//vector容器存放内置类型
void test01()
{
	//创建vector容器
	vector<int>v;
	//尾插
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);

	//通过迭代器访问容器中的数据
	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;
	}

	//第三种:利用STL种提供的算法  <algorithm>  标准算法头文件
	for_each(v.begin(),v.end(),myPrint);        //利用回调

}


int main(void)
{
	test01();
	system("pause");
	return 0;
}

Hay tres métodos de recorrido: while, for, for_each

Cuando se usa un iterador, el tipo de (* it) es el tipo de datos en <>, por ejemplo, el tipo de (* it) aquí es int

2. Vector almacena tipos de datos personalizados

Utilice vector para almacenar tipos personalizados y punteros de tipos personalizados

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

//1、vector存放自定义数据类型
class Person2
{
public:
	Person2(string name, int age) :m_Name(name), m_Age(age)
	{

	}
	string m_Name;
	int m_Age;
};

//1、vector存放自定义数据类型
void test02()
{
	vector<Person2> v;          //v是一个Person2数据类型
	Person2 p1("aaa", 10);
	Person2 p2("bbb", 20);
	Person2 p3("ccc", 30);
	Person2 p4("ddd", 40);
	Person2 p5("eee", 50);

	//向容器中添加数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	//遍历数据
	for (vector<Person2>::iterator it = v.begin(); it != v.end(); it++)
	{
		//*it的类型是Person2
		//cout << "姓名:" << (*it).m_Name <<"年龄:"<<(*it).m_Age<< endl;
		cout << "姓名:" << it->m_Name << "年龄:" << it->m_Age << endl;
	}
}


//2、vector存放自定义数据类型 指针
void test002()
{
	vector<Person2*> v;          //v是一个Person2类型指针
	Person2 p1("aaa", 10);
	Person2 p2("bbb", 20);
	Person2 p3("ccc", 30);
	Person2 p4("ddd", 40);
	Person2 p5("eee", 50);

	//向容器中添加数据
	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);
	v.push_back(&p5);

	//遍历
	for (vector<Person2*>::iterator it = v.begin(); it != v.end(); it++)
	{
		//*it的类型是Person2*
		cout << ":姓名:" << (*it)->m_Name << "  年龄:" << (*it)->m_Age << endl;
	}
}
int main(void)
{
	//test02();
	test002();
	system("pause");
	return 0;
}

3. Contenedor de vectores contenedor anidado

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
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);
		v2.push_back(i + 10);
		v3.push_back(i + 100);
		v4.push_back(i + 1000);
	}

	//将小容器放到大容器中
	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++)
	{
		//(*it)是 vector<int>类型
		for (vector<int>::iterator vIt = (*it).begin(); vIt != (*it).end(); vIt++)
		{
			//(*vIt)是int
			cout << (*vIt)<<"\t";
		}
		cout << endl;
	}

}

int main(void)
{
	test03();
	system("pause");
	return 0;
}

4. Resumen:

Cuando se usa un iterador, el tipo de (* it) es el tipo de datos en <>

Por ejemplo, el tipo de (* it) en el primer caso es tipo int

Los tipos de (* it) en el segundo caso son el tipo Person2 y el tipo Person2 * respectivamente

En el tercer caso, el tipo de (* it) es vector <int>, (* vIt) es de tipo int

Supongo que te gusta

Origin blog.csdn.net/Zhouzi_heng/article/details/115030389
Recomendado
Clasificación