C++ - 数据容器之 vector(创建与初始化、元素访问、元素添加、元素删除、元素遍历、容量判断)

一、创建与初始化

#include <iostream>
#include <vector>

using namespace std;

int main() {

	// 空的 vector
	vector<int> v1;

	// 指定初始大小的 vector,5 个元素,默认值初始化为 0
	vector<int> v2(5);

	// 指定大小和初始值的 vector,5 个元素,初始化为 10
	vector<int> v3(5, 10);

	// 列表初始化的 vector
	vector<int> v4 = { 1, 2, 3, 4, 5 };

	return 0;
}

二、元素访问

#include <iostream>
#include <vector>

using namespace std;

int main() {

	vector<int> v = { 10, 20, 30, 40, 50 };

	// 通过索引访问元素 - 不检查边界
	int a = v[2];
	cout << a << endl;

	// 通过索引访问元素 - 检查边界,越界抛出异常
	int b = v.at(3);
	cout << b << endl;

	// 访问首元素
	int first = v.front();
	cout << first << endl;

	// 访问尾元素
	int last = v.back();
	cout << last << endl;

	// 获取底层数据指针,指向第一个元素的指针
	int* p = v.data();
	cout << *p << endl;

	return 0;
}
  • 输出结果
30
40
10
50
10

三、元素添加

#include <iostream>
#include <vector>

using namespace std;

int main() {

	vector<int> v;

	// 在末尾添加元素
	v.push_back(10);

	// 在指定位置插入元素
	v.insert(v.begin(), 5); // 在开头插入
	v.insert(v.begin() + 2, 15); // 在第 2 个位置插入
	v.insert(v.end(), { 30, 40, 50 }); // 在末尾插入多个元素

	for (size_t i = 0; i < v.size(); ++i) {
		cout << v[i] << endl;
	}

	return 0;
}
  • 输出结果
5
10
15
30
40
50

四、元素删除

#include <iostream>
#include <vector>

using namespace std;

int main() {

	vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	// 删除最后一个元素
	v.pop_back();

	// 删除指定位置或范围的元素
	v.erase(v.begin() + 2); // 删除第 3 个元素
	v.erase(v.begin() + 1, v.begin() + 4); // 删除第 2 到第 4个元素

	for (size_t i = 0; i < v.size(); ++i) {
		cout << v[i] << endl;
	}

	cout << "----------" << endl;

	v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	// 清空所有元素
	v.clear();

	for (size_t i = 0; i < v.size(); ++i) {
		cout << v[i] << endl;
	}

	return 0;
}
  • 输出结果
1
6
7
8
----------

五、元素遍历

  1. 索引遍历
#include <iostream>
#include <vector>

using namespace std;

int main() {

	vector<int> vec = { 1, 2, 3, 4, 5 };

	for (size_t i = 0; i < vec.size(); ++i) {
		cout << vec[i] << endl;
	}

	return 0;
}
  1. 迭代器遍历
#include <iostream>
#include <vector>

using namespace std;

int main() {

	vector<int> vec = { 1, 2, 3, 4, 5 };

	for (auto it = vec.begin(); it != vec.end(); it++) {
		cout << *it << endl;
	}

	return 0;
}
  1. 范围的 for 循环遍历
#include <iostream>
#include <vector>

using namespace std;

int main() {

	vector<int> vec = { 1, 2, 3, 4, 5 };

	for (int val : vec) {
		cout << val << endl;
	}

	return 0;
}

六、容量判断

#include <iostream>
#include <vector>

using namespace std;

int main() {

	vector<int> v = { 1, 2, 3 };

	size_t size = v.size();
	cout << "获取元素数量:" << size << endl;

	bool empty = v.empty();
	cout << "检查是否为空:" << empty << endl;

	size_t cap = v.capacity();
	cout << "获取当前容量:" << cap << endl;

	v.reserve(100);
	cout << "预留容量后:" << v.capacity() << endl;

	v.shrink_to_fit();
	cout << "释放多余内存后:" << v.capacity() << endl;

	return 0;
}
  • 输出结果
获取元素数量:3
检查是否为空:0
获取当前容量:3
预留容量后:100
释放多余内存后:3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值