C++ 学习日志——STL中的容器 Vector

本文简单的介绍下STL下的容器——vector的简单的存放和遍历方法

目录

一、vector存放和访问方式

二、三种遍历方式

三、vector存放对象

四、vector存放指针——难点


一、vector存放和访问方式

迭代器:vector<int>::iterator

容器头文件:vector

迭代器头文件:algorithm

存放数据

  •   使用 vector<数据类型> 对象  创建一个容器
  •   存放数据  push_back() 

访问数据

  •  使用vector<int>::iterator 创建一个起始和结束的指针  
  • itBegin和 itEnd 分别指向容器中的第一个元素和容器最后一个元素的下一个位置
  • 使用 * 解引用访问源数据

二、三种遍历方式

使用while遍历

        其中itBegin 指向起始地址,使用while循环,每次左移指针,直到该指针指向尾指针。

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

int main() {

	//创建一个vector容器
	vector<int> ve;

	ve.push_back(10);
	ve.push_back(20);
	ve.push_back(30);

	//通过迭代器访问数据  指针类型   itBegin 为指针类型,可以通过 解引用(*)访问
	vector<int>::iterator itBegin = ve.begin();//起始迭代器 指向容器中的第一个元素
	vector<int>::iterator itEnd = ve.end();//结束迭代器 指向容器最后一个元素的下一个位置

/*----------第一种遍历方式----------*/
	cout << "第一种遍历方式" << endl;
	while (itBegin != itEnd) {
		cout << *itBegin ++<< endl;
	}
	//cout << *ve.begin() << endl;

	system("pause");
}

 

使用for遍历

        其中it指向容器中的起始位置,判断条件为是否指向尾指针,通过 *来解引用 输出容器中的数据。

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

int main() {
	//创建一个vector容器 数组
	vector<int> ve;

	ve.push_back(10);
	ve.push_back(20);
	ve.push_back(30);

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

使用 for_each 遍历

        for_each函数中有三个参数,其中前两个为起始地址和尾地址,第三个是一个回调函数,需要自己定义和实现。

#include <iostream>
#include <vector>
#include <algorithm>//标准算法头文件 for_each
using namespace std;

void fun(int a) {
	cout << a << endl;
}
int main() {

	//创建一个vector容器 数组
	vector<int> ve;

	ve.push_back(10);
	ve.push_back(20);
	ve.push_back(30);

	/*----------第三种遍历方式----------*/
	cout << "第三种遍历方式" << endl;
	// fun回调函数
	for_each(ve.begin(), ve.end(), fun);
	system("pause");
}

三、vector存放对象

  •  使用vector创建Person类的容器
  • 准备好要存放的对象(初始化对象)
  • 使用push_back存放
  • 遍历输出

        其中,for 里面的 it表示指向的对象,使用 * 解引用,即 *it 可以 表示p1 ~ p4

因为 it指向的是某个对象的地址,所以可以使用 it-> 来访问对象的成员

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

class Person {
public:
	string name;
	int age;
	Person(string name1,int age1) {
		name = name1;
		age = age1;
	}
};
void fun(Person p) {
	cout<< p.name << "\t"<<p.age << endl;
}

void test1() {
	vector<Person> v;

	//准备好要添加的对象
	Person p1("王二", 12);
	Person p2("刘琴", 18);
	Person p3("张北", 17);
	Person p4("刘通", 15);

	//向容器中添加对象
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	cout << "for 循环输出" << endl;
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
		cout << (*it).name << " " << (*it).age << endl;
		//cout <<it->name<< " " << it->age << endl;//等同上
	}

	cout << "for_each输出" << endl;
	for_each(v.begin(), v.end(), fun);
}

int main() {	

	test1();

	system("pause");
}

四、vector存放指针——难点

在这里,把创建好的对象的地址分别存放到vector容器里

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

class Person {
public:
	string name;
	int age;
	Person(string name1, int age1) {
		name = name1;
		age = age1;
	}
};
void test2() {
	vector<Person*> v;
	
	Person p1("王二", 12);
	Person p2("刘琴", 18);
	Person p3("张北", 17);
	Person p4("刘通", 15);

	//向容器添加数据
	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++) {
		// it是指向person的地址的指针   *it 是 person 对象的地址  eg: *it 
		cout << (*it)->age << "\t" << (*it)->name << endl;
		// 理解(*v.begin())->age   v.begin()是指向第一个的内容的指针
	}
}

int main() {
	test2();
	system("pause");
}

 

        这里不太好理解,使用vector存放对象的地址,那么 v.begin()就是 指向p1的地址了,解引用后 *v.begin() 就是p1的地址,使用->可以调出p1的成员

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值