ShuangChenYue ShuangChenYue
首页
  • Cpp之旅
  • Cpp专栏
  • Effective_CPP
  • muduo网络库
  • Unix环境高级编程
  • Cpp提高编程
  • 计算机网络
  • 操作系统
  • 数据结构
  • Linux
  • 算法
  • 基础篇
  • MySql
  • Redis
  • 电子嵌入式通信协议
  • 深入浅出SSD
  • 文件系统
  • 汇编语言
  • STM32
  • 随笔(持续更新)
  • Git知识总结
  • Git 创建删除远程分支
  • nvm使用小结
  • 虚拟机固定 IP 地址
  • Shell 脚本学习笔记
  • VScode 插件 CodeGeeX 使用教程
  • KylinV10 将项目上传至 Github教程
  • KylinV10 安装 MySQL 教程(可防踩雷)
  • kylinV10-SP1 安装 QT
  • 高并发内存池
  • USBGUARD 项目编译环境配置
  • Power_Destory 项目
  • U 盘清除工具编译教程
  • 个人博客代码推送教程
  • HTML与CSS
  • JS学习
  • Vue3入门
  • Vue3进阶
  • 黑马Vue3
  • MFC编程随记
  • MFC实现ini配置文件的读取
  • MFC实现点击列表头排序
  • 贴图法美化Button按钮
  • 如何高效阅读嵌入式项目代码
  • NAND Flash
  • ARM 处理器
  • 嵌入式基础知识-存储器
  • 闪存存储和制造技术概述
  • 芯片IO驱动力
  • 主流先进封装技术介绍
  • 虎牙C++技术面经
  • 金山一面复习
  • 完美世界秋招 C++ 游戏开发面经(Cpp部分)
  • 博客搭建
  • 网站收藏箱
首页
  • Cpp之旅
  • Cpp专栏
  • Effective_CPP
  • muduo网络库
  • Unix环境高级编程
  • Cpp提高编程
  • 计算机网络
  • 操作系统
  • 数据结构
  • Linux
  • 算法
  • 基础篇
  • MySql
  • Redis
  • 电子嵌入式通信协议
  • 深入浅出SSD
  • 文件系统
  • 汇编语言
  • STM32
  • 随笔(持续更新)
  • Git知识总结
  • Git 创建删除远程分支
  • nvm使用小结
  • 虚拟机固定 IP 地址
  • Shell 脚本学习笔记
  • VScode 插件 CodeGeeX 使用教程
  • KylinV10 将项目上传至 Github教程
  • KylinV10 安装 MySQL 教程(可防踩雷)
  • kylinV10-SP1 安装 QT
  • 高并发内存池
  • USBGUARD 项目编译环境配置
  • Power_Destory 项目
  • U 盘清除工具编译教程
  • 个人博客代码推送教程
  • HTML与CSS
  • JS学习
  • Vue3入门
  • Vue3进阶
  • 黑马Vue3
  • MFC编程随记
  • MFC实现ini配置文件的读取
  • MFC实现点击列表头排序
  • 贴图法美化Button按钮
  • 如何高效阅读嵌入式项目代码
  • NAND Flash
  • ARM 处理器
  • 嵌入式基础知识-存储器
  • 闪存存储和制造技术概述
  • 芯片IO驱动力
  • 主流先进封装技术介绍
  • 虎牙C++技术面经
  • 金山一面复习
  • 完美世界秋招 C++ 游戏开发面经(Cpp部分)
  • 博客搭建
  • 网站收藏箱
  • Cpp之旅

  • Cpp专栏

  • Effetcive_CPP

  • muduo网络库

  • Unix环境高级编程

  • Cpp提高编程

    • 函数模板
    • 类模板
    • 初识STL
    • string 容器
    • vector 容器
      • 5、vector 容器
        • 5.1 vector构造函数
        • 5.2 vector 赋值操作
        • 5.3 vector 容量和大小
        • 5.4 vector 插入和删除
        • 5.5 vector 互换容器
        • 5.6 vector预留空间
    • deque 容器
    • stack 容器
    • queue 容器
    • list 容器
    • set 容器
    • map 容器
    • STL 函数对象
  • CPP语言
  • Cpp提高编程
霜晨月
2023-12-21
目录

vector 容器

# 5、vector 容器

功能:

  • vector 数据结构和数组非常相似,也称为单端数组

vector与普通数组的区别:

  • 不同之处在于数组是静态空间,而 vector 可以动态扩展

动态扩展:

  • 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝到新空间,释放原空间

1694764289476

  • vector 容器的迭代器是支持随机访问的迭代器

# 5.1 vector构造函数

功能描述:

  • 创建 vector 容器

函数原型:

vector<T> v;						// 采用模板实现类实现,默认构造函数
vector(v.begin(), v.end());			// 将 v[begin(),end()] 区间中的元素拷贝给本身
vector(n, elem);					// 构造函数将 n 个 elem 拷贝给本身
vector(const vector vector &vec);	// 拷贝构造函数
1
2
3
4
#include <iostream>
#include <vector>

using namespace std;
void printVector(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
		cout << *it << " ";
	}
	cout << endl;
}
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; ++i) {
		v1.push_back(i);
	}
	printVector(v1);

	vector<int> v2(v1.begin(),v1.end());
	printVector(v2);

	vector<int> v3(10, 100);
	printVector(v3);

	vector<int> v4(v3);
	printVector(v4);
}

int main() {
	test01();
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 5.2 vector 赋值操作

功能描述:

  • 给 vector 容器进行赋值

函数原型:

vector& operator=(const vector &vec);	// 重载等号操作符
assign(beg, end);						// 将 [beg, end] 区间中的数据拷贝赋值给本身
assign(n, elem);						// 将 n 个 elem 拷贝赋值给本身
1
2
3
#include <iostream>
#include <vector>

using namespace std;
void printVector(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
		cout << *it << " ";
	}
	cout << endl;
}
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; ++i) {
		v1.push_back(i);
	}
	printVector(v1);

	vector<int> v2;
	v2 = v1;
	printVector(v2);

	vector<int> v3;
	v3.assign(v1.begin(),v1.end());
	printVector(v3);

	vector<int> v4;
	v4.assign(10, 100);
	printVector(v4);
}

int main() {
	test01();
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

总结:

  • vector 赋值方式比较简单,使用 operator=,或者 assign 都可以

# 5.3 vector 容量和大小

功能描述:

  • 对 vector 容器的容量和大小操作

函数原型:

empty();		// 判断容器是否为空
capacity();		// 容器的容量
size();			// 返回容器中元素的个数

// 重新指定容器的长度为 num,若容器边长,则默认值填充新位置。
// 如果容器变短,则末尾超出容器长度的元素被删除。
resize(int num);

// 重新指定容器的长度为 num,若容器变长,则以 elem 值填充新位
// 如果容器变短,则末尾超出容器长度的元素被删除
resize(int num, elem);
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>
using namespace std;

void printVector(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
		cout << *it << " ";
	}
	cout << endl;
}
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; ++i) {
		v1.push_back(i);
	}
	printVector(v1);
	if (v1.empty()) {
		cout << "v1为空" << endl;
	}
	else {
		cout << "v1不为空" << endl;
		cout << "v1的容量为:" << v1.capacity() << endl;
		cout << "v1的大小为:" << v1.size() << endl;
	}
 
	// resize 重新指定大小 ,若指定的更大,默认用 0 填充新位置,可以利用重载版本替换默认填充
	v1.resize(15, 10);
	printVector(v1);

	// resize 重新指定大小 ,若指定的更小,超出部分元素被删除
	v1.resize(5);
	printVector(v1);

}

int main() {
	test01();
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

总结:

  • 判断是否为空 --- empty
  • 返回元素个数 --- size
  • 返回容器容量 --- capacity
  • 重新指定大小 --- resize

# 5.4 vector 插入和删除

功能描述:

  • 对 vector 容器进行插入、删除操作

函数原型:

push_back(ele);								// 尾部插入元素 ele
pop_back();									// 删除最后一个元素
insert(const_iterator pos, ele);			// 迭代器指向位置 pos 插入元素 ele

// 迭代器指向位置 pos 插入 count 个元素 ele
insert(const_iterator pos, int count, ele);
erase(const_iterator pos);					// 删除迭代器指向的元素

// 删除迭代器从 start 到 end 之间的元素
erase(const_iterator start, const_iterator end);
clear();									// 删除容器中所有元素
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>
using namespace std;

void printVector(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
		cout << *it << " ";
	}
	cout << endl;
}
void test01() {
	vector<int> v1;
	// 尾插
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	
	// 尾删
	v1.pop_back();
	printVector(v1);

	// 插入
	v1.insert(v1.begin(), 100);
	printVector(v1);

	v1.insert(v1.begin(), 2, 1000);
	printVector(v1);

	// 删除
	v1.erase(v1.begin());
	printVector(v1);

	// 清空
	v1.erase(v1.begin(), v1.end());
	v1.clear();
	printVector(v1);
}

int main() {
	test01();
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

总结:

  • 尾插 --- push_back
  • 尾删 --- pop_back
  • 插入 --- insert (位置迭代器)
  • 删除 --- erase (位置迭代器)
  • 清空 --- clear

5.5 vecto r数据存取

功能描述:

  • 对 vector 中的数据的存取操作

函数原型:

at(int idx);// 返回索引 idx 所指的数据
operator[]; // 返回索引 idx 所指的数据
front();	// 返回容器中第一个数据元素
back();		// 返回容器中最后一个数据元素
1
2
3
4
#include <iostream>
#include <vector>
using namespace std;

void test01() {
	vector<int>v1;
	for (int i = 0; i < 10; ++i) {
		v1.push_back(i);
	}
	for (int i = 0; i < v1.size(); ++i) {
		cout << v1[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < v1.size(); ++i) {
		cout << v1.at(i) << " ";
	}
	cout << endl;

	cout << "v1的第一个元素为:" << v1.front() << endl;
	cout << "v1的最后一个元素为:" << v1.back() << endl;

}

int main() {
	test01();
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

总结:

  • 除了用迭代器获取 vector 容器中元素,[ ] 和 at 也可以
  • front 返回容器第一个元素
  • back 返回容器最后一个元素

# 5.5 vector 互换容器

功能描述:

  • 实现两个容器内元素进行互换

函数原型:

swap(vec);// 将vec与本身的元素互换
1
#include <iostream>
#include <vector>
using namespace std;

void printVector(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
		cout << *it << " ";
	}
	cout << endl;
}

void test01() {
	vector<int>v1;
	for (int i = 0; i < 10; ++i) {
		v1.push_back(i);
	}
	printVector(v1);

	vector<int>v2;
	for (int i = 10; i > 0; --i) {
		v2.push_back(i);
	}
	printVector(v2);

	// 互换容器
	cout << "互换容器后:" << endl;
	v1.swap(v2);
	printVector(v1);
	printVector(v2);
}

void test02() {
	vector<int>v;
	for (int i = 0; i < 100000; ++i) {
		v.push_back(i);
	}
	cout << "v的容量大小为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;
	v.resize(3);
	cout << "v的容量大小为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

	// 收缩内存
	vector<int>(v).swap(v);// 匿名对象
	cout << "v的容量大小为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;
}

int main() {
	test01();
	test02();
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

总结:

  • swap 可以使两个容器互换,可以达到实用的收缩内存效果

# 5.6 vector预留空间

功能描述:

  • 减少 vector 在动态扩展容量时的扩展次数

函数原型:

reserve(int len);// 容器预留 len 个元素长度,预留位置不初始化,元素不可访问
1
#include <iostream>
#include <vector>
using namespace std;

void test01() {
	vector<int>v;
	//预留空间
	v.reserve(100000);

	int num = 0;
	int* p = nullptr;
	for (int i = 0; i < 100000; ++i) {
		v.push_back(i);
		if (p != &v[0]) {
			p = &v[0];
			num++;
		}
	}
	cout << "num:" << num << endl;
}

int main() {
	test01();
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

总结:

  • 如果数据量较大,可以一开始利用 reserve 预留空间
上次更新: 2024/6/3 14:54:44
string 容器
deque 容器

← string 容器 deque 容器→

Theme by Vdoing | Copyright © 2023-2024 霜晨月
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式