STL---Vector(2)

Vector的简单使用

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

//vector<int>的迭代器 vector<int>::iterator
void myPrint(int val)
{
	cout << val << endl;
}

//vector中存放int类型
void test01()
{
	vector<int> v;

	//向容器中插入数据
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);

	//通过迭代器访问容器中的数据
	vector<int>::iterator arr_Begin = v.begin(); //起始迭代器 指向容器中第一个元素
	vector<int>::iterator arr_End = v.end();   //结束迭代器 指向容器中最后一个元素的下一个

	//第一种遍历方式
	cout << "第一种遍历方式" << endl;
	while (arr_Begin != arr_End)
	{
		cout << *arr_Begin << endl;
		arr_Begin++;
	}

	//第二种遍历方式
	cout << "第二种遍历方式" << endl;
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << endl;
	}

	//第三种遍历方式  利用STL提供的遍历算法
	cout << "第三种遍历方式" << endl;
	for_each(v.begin(),v.end(),myPrint);


}

//vector中存放自定义数据类型
class Student
{
public:
	Student(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	int m_age;
};

void test02()
{
	vector<Student> array;
	Student s1("Tom",18);
	Student s2("Lisa", 18);
	Student s3("Liss", 18);
	Student s4("Chenyao", 18);
	Student s5("Zhangchao", 18);

	//向容器中加入数据
	array.push_back(s1);
	array.push_back(s2);
	array.push_back(s3);
	array.push_back(s4);
	array.push_back(s5);

	//遍历
	for (vector<Student>::iterator it = array.begin(); it != array.end(); it++)
	{
		//cout << "姓名:" << (*it).m_name << "年龄:" << (*it).m_age << endl;
		cout << "姓名:" << it->m_name << "年龄:" << it->m_age << endl;
	}
}

//vector存放自定义指针
void  test03()
{
	vector<Student*> array;
	Student s1("Tom", 18);
	Student s2("Lisa", 18);
	Student s3("Liss", 18);
	Student s4("Chenyao", 18);
	Student s5("Zhangchao", 18);

	//向容器中加入数据
	array.push_back(&s1);
	array.push_back(&s2);
	array.push_back(&s3);
	array.push_back(&s4);
	array.push_back(&s5);

	//遍历容器
	for (vector<Student*>::iterator it = array.begin(); it != array.end(); it++)
	{
		 cout << "姓名:" << (*it)->m_name << "年龄:" << (*it)->m_age << endl;
		
	}
}

//嵌套容器 相当于二维数组
void test04()
{
	vector<vector<int>> p;

	//创建四个小容器
	vector<int> p1;
	vector<int> p2;
	vector<int> p3;
	vector<int> p4;

	//向小容器中加入数据
	for (int i = 0; i < 4; i++)
	{
		p1.push_back(i+1);
		p2.push_back(i + 1);
		p3.push_back(i + 1);
		p4.push_back(i + 1);
	}

	//将小容器插入大容器中
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);

	//遍历
	for (vector<vector<int>>::iterator it = p.begin(); it != p.end(); it++)
	{
		//(*it)  容器vector<int>
		for (vector<int>::iterator jt = (*it).begin(); jt != (*it).end(); jt++)
		{
			cout << *jt << " ";
		}
		cout << endl;
	}
}
int main()
{
	test01();
	cout << "---------------------------" << endl;
	test02();
	cout << "---------------------------" << endl;
	test03();
	cout << "---------------------------" << endl;
	test04();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值