[C++ STL] vector使用详解

一、概述

vector(向量): 是一种序列式容器,事实上和数组差不多,但它比数组更优越。一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界。而vector正好弥补了这个缺陷,它的特征是相当于可分配拓展的数组(动态数组),它的随机访问快,在中间插入和删除慢,但在末端插入和删除快。

二、定义及初始化

使用之前必须加相应容器的头文件:

#include <vector> // vector属于std命名域的,因此需要通过命名限定,例如using std::vector;

定义的实现代码如下:

vector<int> a; // 定义一个int类型的向量a
vector<int> a(10); // 定义一个int类型的向量a,并设置初始大小为10
vector<int> a(10, 1); // 定义一个int类型的向量a,并设置初始大小为10且初始值都为1
vector<int> b(a); // 定义并用向量a初始化向量b
vector<int> b(a.begin(), a.begin()+3); // 将a向量中从第0个到第2个(共3个)作为向量b的初始值

除此之外,还可以直接使用数组来初始化向量:

int n[] = {1, 2, 3, 4, 5} ;
// 将数组n的前5个元素作为向量a的初值
// 说明:当然不包括arr[4]元素,末尾指针都是指结束元素的下一个元素,
// 这个主要是为了和vec.end()指针统一。
vector<int> a(n, n+5) ;              
vector<int> a(&n[1], &n[4]) ;        // 将n[1]、n[2]、n[3]作为向量a的初值

三、基本操作函数

3.1 容量函数

  • 容器大小:vec.size();
  • 容器容量:vec.capacity(); // 指在发生 realloc 前能允许的最大元素数,即预分配的内存空间,与 size() 不同
  • 容器最大容量:vec.max_size();
  • 更改容器大小:vec.resize();
  • 容器判空:vec.empty();
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
	vector<int> vec;
	for (int i = 0; i<6; i++)
	{
		vec.push_back(i);
	}

	cout << vec.size() << endl; // 输出:6
	cout << vec.capacity() << endl; // 输出:6
	cout << vec.max_size() << endl; // 输出:1073741823
	vec.resize(0);
	cout << vec.size() << endl; // 输出:0
	if (vec.empty())
		cout << "元素为空" << endl; // 输出:元素为空

	return 0;
}

注意:vec.resize(10),会分配 10 个 0 给vec,相当于 push_back(0) 10 次。

3.2 添加函数

  • 末尾添加元素:vec.push_back(const T& x);
  • 任意位置插入一个元素:vec.insert(iterator it, const T& x);
  • 任意位置插入 n 个相同元素:vec.insert(iterator it, int n, const T& x);
  • 插入另一个向量的 [forst,last] 间的数据:vec.insert(iterator it, iterator first, iterator last);
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
	vector<int> vec;

	// 末尾添加元素
	vec.push_back(5);
	// 任意位置插入一个元素
	vector<int>::iterator it = vec.begin();
	vec.insert(it, 2);
	// 任意位置插入n个相同元素
	it = vec.begin();
	vec.insert(it, 3, 9);
	// 插入另一个向量的[forst,last]间的数据
	vector<int> vec2(5, 8);
	it = vec.begin();
	vec.insert(it, vec2.end() - 1, vec2.end());

	// 遍历显示
	for (it = vec.begin(); it != vec.end(); it++)
		cout << *it << " "; // 输出:8 9 9 9 2 5
	cout << endl;

	return 0;
}

3.3 删除函数

  • 末尾删除元素:vec.pop_back();
  • 任意位置删除一个元素:vec.erase(iterator it);
  • 删除 [first,last] 之间的元素:vec.erase(iterator first, iterator last);
  • 清空所有元素:vec.clear();
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
	vector<int> vec;
	for (int i = 0; i < 8; i++)
		vec.push_back(i);

	// 末尾删除元素
	vec.pop_back();
	// 任意位置删除一个元素
	vector<int>::iterator it = vec.begin();
	vec.erase(it);
	// 删除[first,last]之间的元素
	vec.erase(vec.begin(), vec.begin() + 1);

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

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

	// 遍历显示   
	for (it = vec.begin(); it != vec.end(); it++)
		cout << *it << " "; // 输出:2 3 4 5 6
	cout << endl;

	return 0;
}

3.4 访问函数

  • 下标访问: vec[1]; // 并不会检查是否越界
  • at 方法访问:vec.at(1); // 以上两者的区别就是 at 会检查是否越界,是则抛出 out of range 异常
  • 访问第一个元素:vec.front();
  • 访问最后一个元素:vec.back();
  • 返回一个指针:int* p = vec.data(); // 可行的原因在于 vector 在内存中就是一个连续存储的数组,所以可以返回一个指针指向这个数组。这是是 C++11 的特性。
#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
	vector<int> vec;
	for (int i = 0; i < 6; i++)
		vec.push_back(i);

	// 下标访问
	cout << vec[0] << endl; // 输出:0
	// at方法访问
	cout << vec.at(0) << endl; // 输出:0
	// 访问第一个元素
	cout << vec.front() << endl; // 输出:0
	// 访问最后一个元素
	cout << vec.back() << endl; // 输出:5
	// 返回一个指针
	int *p = vec.data();
	cout << *p <<endl; // 输出:0

	return 0;
}

3.5 其他函数

  • 多个元素赋值:vec.assign(int nSize, const T& x); // 类似于初始化时用数组进行赋值
  • 交换两个同类型容器的元素:swap(vector&);
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
	// 多个元素赋值
	vector<int> vec;
	vec.assign(3, 1);
	vector<int> vec2;
	vec2.assign(3, 2);

	// 交换两个容器的元素
	vec.swap(vec2);

	// 遍历显示
	cout << "vec: ";
	for (int i = 0; i < vec.size(); i++)
		cout << vec[i] << " "; // 输出:2 2 2
	cout << endl;

	// 遍历显示
	cout << "vec2: ";
	for (int i = 0; i < vec2.size(); i++)
		cout << vec2[i] << " "; // 输出:1 1 1
	cout << endl;

	return 0;
}

四、迭代器与算法

1. 迭代器

  • 开始指针:vec.begin();
  • 末尾指针:vec.end(); // 指向最后一个元素的下一个位置
  • 指向常量的开始指针:vec.cbegin(); // 意思就是不能通过这个指针来修改所指的内容,但还是可以通过其他方式修改的,而且指针也是可以移动的。
  • 指向常量的末尾指针:vec.cend();
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
	vector<int> vec;
	vec.push_back(1);
	vec.push_back(2);
	vec.push_back(3);

	cout << *(vec.begin()) << endl; // 输出:1
	cout << *(--vec.end()) << endl; // 输出:3
	cout << *(vec.cbegin()) << endl; // 输出:1
	cout << *(--vec.cend()) << endl; // 输出:3
	cout << *(vec.rbegin()) << endl; // 输出:3
	cout << *(--vec.rend()) << endl; // 输出:1
	cout << endl;

	return 0;
}

2. 算法

  • 遍历元素
vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); it++)
    cout << *it << endl;
// 或者
for (int i = 0; i < vec.size(); i++) {
    cout << vec.at(i) << endl;
}

  • 元素翻转
#include <algorithm>
reverse(vec.begin(), vec.end());

  • 元素排序
#include <algorithm>
sort(vec.begin(), vec.end()); // 采用的是从小到大的排序

// 如果想从大到小排序,可以采用先排序后反转的方式,也可以采用下面方法:
// 自定义从大到小的比较器,用来改变排序方式
bool Comp(const int& a, const int& b) 
{
    return a > b;
}

sort(vec.begin(), vec.end(), Comp);

参考:

[清水汪汪-博客园博客](https:// www.cnblogs.com/zhonghuasong/p/5975979.html)

原文链接: https://www.cnblogs.com/linuxAndMcu/p/10259630.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    [C++ STL] vector使用详解

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/288370

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 上午10:56
下一篇 2023年2月15日 上午10:56

相关推荐