vector 数据结构和数组非常相似,也称为单端数组
不同之处在于数组是静态空间,而vector可以动态扩展
vector 的动态扩展,并不是在原空间之后续接新空间(因为无法保证其后续的存储空间是否被占用),而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间
vector容器的迭代器是支持随机访问的迭代器
构造函数
功能:
构建 vector 容器
函数原型:
vector<T> v;
//采用模板实现类实现,默认构造函数vector(v.begin(), v.end());
// 将vector容器 v 的 [v.begin(),v.end()) 区间内的元素拷贝给当前 vector 容器vector(n, elem);
//构造函数将n个elem拷贝给本身。vector(const vector &vec);
//拷贝构造函数。
#include<iostream>
#include<vector>
#include<string>
using namespace std;
// 遍历输出 vector
template<typename T>
void print_vector(vector<T>& v) {
for (vector<int>::iterator ite = v.begin(); ite < v.end(); ite++) {
cout << (*ite) << " ";
}
cout << endl;
}
int main() {
// 默认构造函数:构造一个存储 int 类型数据的vector容器
vector<int> v1;
v1.push_back(1);
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
print_vector(v1); // 1 1 2 3
// 构建v2时,将 v1 中的元素拷贝给v2
vector<int> v2(v1.begin(), v1.end());
print_vector(v2); // 1 1 2 3
// 使用 5 个 6 构建 v3
vector<int> v3(5, 6);
print_vector(v3); // 6 6 6 6 6
// 使用拷贝构造函数
vector<int> v4(v3);
print_vector(v4); // 6 6 6 6 6
return 0;
}
赋值操作
功能描述:
- 给vector容器进行赋值
函数原型:
-
vector& operator=(const vector &vec);
//重载等号操作符 -
assign(beg, end);
//将[beg, end)区间中的数据拷贝赋值给本身。 -
assign(n, elem);
//将n个elem拷贝赋值给本身。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<class T>
void print_vector(vector<T> v) {
for (vector<int>::iterator ite = v.begin(); ite < v.end(); ite++) {
cout << *ite << " ";
}
cout << endl;
}
int main() {
vector<int> v;
for (int i = 0; i < 10; i++){
v.push_back(i);
}
print_vector(v); // 0 1 2 3 4 5 6 7 8 9
// 将 v 的所有元素拷贝给 v1
vector<int> v1 = v;
v.push_back(10);
print_vector(v); // 0 1 2 3 4 5 6 7 8 9 10
print_vector(v1); // 0 1 2 3 4 5 6 7 8 9
// 将v的位置为 [v.begin(),v.end()) 区间内的元素拷贝给 v2
vector<int> v2;
v2.assign(v.begin(),v.end());
print_vector(v2); // 0 1 2 3 4 5 6 7 8 9 10
// 将 5 个 6 存入v3
vector<int> v3;
v3.assign(5,6);
print_vector(v3); // 6 6 6 6 6
return 0;
}
容量和大小
功能描述:
- 对vector容器的容量和大小操作
函数原型:
-
empty();
//判断容器是否为空 -
capacity();
//容器的容量 -
size();
//返回容器中元素的个数 -
resize(int num);
//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。 如果容器变短,则末尾超出容器长度的元素被删除。 -
resize(int num, elem);
//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<class T>
void print_vector(vector<T> v) {
for (vector<int>::iterator ite = v.begin(); ite < v.end(); ite++) {
cout << *ite << " ";
}
cout << endl;
}
int main(){
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back( i*i+1);
}
print_vector(v); // 1 2 5 10 17 26 37 50 65 82
// 判断v是否为空
if (v.empty()) {
cout << "v 为空" << endl;
}
else {
cout << "v 不为空" << endl;
}
// 获取v的容量
int capacity = v.capacity();
cout << "v 的容量为:"<< capacity << endl; // v 的容量为:13
// 获取 v 的大小
int size = v.size();
cout << "v 的大小为:" << size << endl; // v 的大小为:10
// 重新设置容器的大小为 15,多出的空余位置默认使用 0 填补
v.resize(15);
print_vector(v); // 1 2 5 10 17 26 37 50 65 82 0 0 0 0 0
// 重新设置容器的大小为 15,多出的空余位置指定使用 1 填补
v.resize(20, 1);
print_vector(v); // 1 2 5 10 17 26 37 50 65 82 0 0 0 0 0 1 1 1 1 1
// 重新设置容器的大小为 5,容器长度变小,超出的元素直接舍弃
v.resize(5);
print_vector(v); // 1 2 5 10 17
return 0;
}
插入和删除
功能描述:
- 对vector容器进行插入、删除操作
函数原型:
push_back(ele);
//尾部插入元素elepop_back();
//删除最后一个元素insert(const_iterator pos, ele);
//迭代器指向位置pos插入元素eleinsert(const_iterator pos, int count,ele);
//迭代器指向位置pos插入count个元素eleerase(const_iterator pos);
//删除迭代器指向的元素erase(const_iterator start, const_iterator end);
//删除迭代器从start到end之间的元素clear();
//删除容器中所有元素
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<class T>
void print_vector(vector<T> v) {
for (vector<int>::iterator ite = v.begin(); ite < v.end(); ite++) {
cout << *ite << " ";
}
cout << endl;
}
int main() {
vector<int> v;
cout << "v的容量:" << v.capacity() << endl; // v的容量:0
cout << "v的大小:" << v.size() << endl; // v的大小:0
for (int i = 0; i < 5; i++) {
// 向 容器 尾部插入一个元素
v.push_back(i * i + 1);
}
print_vector(v); // 1 2 5 10 17
cout << "v的容量:" << v.capacity() << endl; // v的容量:6
cout << "v的大小:" << v.size() << endl; // v的大小:5
// 删除最后一个元素
v.pop_back();
print_vector(v); // 1 2 5 10
cout << "v的容量:" << v.capacity() << endl; // v的容量:6
cout << "v的大小:" << v.size() << endl; // v的大小:4
// 在迭代器指向位置插入一个元素 8
v.insert(v.begin(), 8);
print_vector(v); // 8 1 2 5 10
// 在迭代器指向位置插入2 个元素 9
v.insert(v.end(), 2,9);
print_vector(v); // 8 1 2 5 10 9 9
// 删除迭代器指向的元素
v.erase(v.begin());
print_vector(v); // 1 2 5 10 9 9
// 删除所有元素
// v.erase(v.begin(),v.end());
v.clear();
print_vector(v);
cout << "v的容量:" << v.capacity() << endl; // v的容量:9
cout << "v的大小:" << v.size() << endl; // v的大小:0
return 0;
}
数据存取
功能描述:
- 对vector中的数据的存取操作
函数原型:
at(int idx);
//返回索引idx所指的数据operator[];
//返回索引idx所指的数据front();
//返回容器中第一个数据元素back();
//返回容器中最后一个数据元素
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<class T>
void print_vector(vector<T> v) {
for (vector<int>::iterator ite = v.begin(); ite < v.end(); ite++) {
cout << *ite << " ";
}
cout << endl;
}
int main() {
vector<int> v;
v.push_back(2);
v.push_back(4);
v.push_back(5);
v.push_back(6);
v.push_back(8);
print_vector(v); // 2 4 5 6 8
// 第一个元素赋值
v[0] = 1;
// 第二个元素赋值
v.at(1) = 3;
print_vector(v); // 1 3 5 6 8
// 取出第一个元素
int e1 = v[0];
cout << e1 << endl; // 1
// 取出第二个元素
int e2 = v.at(1); // 3
cout << e2 << endl;
// 遍历
for (int i = 0; i < v.size(); i++)
{
cout << v.at(i) << " ";
// cout << v[i] << " ";
}
cout << endl;
// 设置容器第一个元素
v.front() = 6;
// 获取容器第一个元素
int first = v.front();
// 设置容器最后一个元素
v.back() = 9;
// 获取容器最后一个元素
int last = v.back();
print_vector(v); // 6 3 5 6 9
}
互换容器
功能描述:
- 实现两个容器内元素进行互换
函数原型:
swap(vec);
// 将vec与本身的元素互换
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
void fun(int n) {
cout << n << " ";
}
// 交换两个vector 容器
void test16_1() {
vector<int> v1;
for (int i = 0; i < 5; i++) {
v1.push_back(i);
}
vector<int> v2;
for (int i = 9; i >= 0; i--) {
v2.push_back(i);
}
// 使用算法遍历
for_each(v1.begin(), v1.end(), fun);
cout << endl; // 0 1 2 3 4
for_each(v2.begin(), v2.end(), fun);
cout << endl; // 9 8 7 6 5 4 3 2 1 0
// 交换 v1 和 v2
v1.swap(v2);
for_each(v1.begin(), v1.end(), fun);
cout << endl; // 9 8 7 6 5 4 3 2 1 0
for_each(v2.begin(), v2.end(), fun);
cout << endl; // 0 1 2 3 4
}
// 使用 swap 函数实现容器收缩
void test16_2() {
vector<int> v;
for (int i = 0; i < 10000; i++) {
v.push_back(i);
}
// v的容量:12138 v的大小:10000
cout << "v的容量:" << v.capacity() << " v的大小:" << v.size() << endl;
// 重新设置 v 的大小
v.resize(100);
// v的容量:12138 v的大小:100
cout << "v的容量:" << v.capacity() << " v的大小:" << v.size() << endl;
// 此时,容器v中只有100个元素,但是其容量依然是12138
// 通过swap交换容器,收缩内存
// 使用匿名对象进行交换
vector<int>(v).swap(v);
// v的容量:100 v的大小:100
cout << "v的容量:" << v.capacity() << " v的大小:" << v.size() << endl;
}
int main() {
test16_1();
test16_2();
return 0;
}
预留空间
功能描述:
- 减少vector在动态扩展容量时的扩展次数
函数原型:
reserve(int len);
//容器预留len个元素长度,预留位置不初始化,元素不可访问。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<class T>
void print_vector(vector<T> v) {
for (vector<int>::iterator ite = v.begin(); ite < v.end(); ite++) {
cout << *ite << " ";
}
cout << endl;
}
int main() {
vector<int> v;
// 预留 100 个元素的空间
v.reserve(100);
// 只给前20个赋值
for (int i = 0; i < 20; i++){
v.push_back(i * i + 1);
}
print_vector(v);
// v的容量:100 v的大小:20
cout << "v的容量:" << v.capacity() << " v的大小:" << v.size() << endl;
// 尝试访问第 21个:出错,预留位置没有初始化,不可访问
// cout << v[20] << endl;
return 0;
}