文章目录
前言
vector是一个表示可变大小数组的动态序列容器,它像数组一样,采用连续的存储空间来存储元素,所以它的随机访问很高效,其尾插、尾删也很高效,但是在头部以及中间部分的插入、删除相比其它某些动态序列容器就慢很多了。
一、构造一个vector
要使用STL的vector,需要包含头文件:
#include<vector>
构造vector有四种方式,分别为:
- 默认构造:
vector<T> v; //可以传递任意类型的模板参数
- 开辟指定空间,并全部初始化为指定的值:
vector<int> v(10, 6); //初始化空间大小为10,并且每一个初始值都是6
- 通过迭代器区间构造:
vector<int> v1(10, 6);
vector<int> v2(v1.begin() + 1, v1.end() - 1);
在SGI版本下的STL中的vector的迭代器底层就是指针,所以也可以通过指针区间进行构造:
int arr[] = { 1,2,3,4,5,6,7,8 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
- 拷贝构造:
vector<int> v1(10, 8);
vector<int> v2(v1);
用一个已经存在的vector来构造一个新的vector
- 赋值运算符
vector<int> v1(10, 8);
vector<int> v2 = v1;
vector内部是重载了赋值运算符的,如果被赋值的对象不存在那么该赋值就相当于拷贝构造了,如果存在就是单纯的赋值
二、迭代器
迭代器是STL中一个很重要的部分,各个容器的底层都是不同的,有物理空间连续的,也有不连续的,但是通过迭代器,无论底层是怎样的,都可以通过相同的方法来遍历、修改操作不同容器的数据。
而SGI版本下的vector的迭代器的底层就是一个指针,而它们的指向如下所示:
正向迭代器(iterator)
正向迭代器,从头到尾。
1.begin
vector<int> v(10, 1);
vector<int>::iterator it = v.begin();
begin() 返回的是该数据块的起始位置的迭代器。
2.end
vector<int> v(10, 1);
vector<int>::iterator it = v.end();
end() 返回的是该数据块的末尾位置的下一个的迭代器,所以begin() 和end() 是一个左闭右开的区间。
反向迭代器(reverse_iterator)
反向迭代器,从尾到头,其中r表示reverse(颠倒)。
1.rbegin
vector<int> v(10, 1);
vector<int>::reverse_iterator rit = v.rbegin();
与begin相反,rbegin() 返回的是数据块的结束位置的迭代器。
2.rend
vector<int> v(10, 1);
vector<int>::reverse_iterator rit = v.rend();
通过上图不难看出来,rend返回的是数据块起始位置的前一个位置的迭代器。
使用举例
通过正、反向迭代器输出数据:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
vector<int>::iterator it = v.begin();
vector<int>::reverse_iterator rit = v.rbegin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
cout << endl;
while (rit != v.rend())
{
cout << *rit << " ";
++rit;
}
cout << endl;
return 0;
}
输出结果:
需要注意的是,在使用反向迭代器时,我们通过看图知道rbegin() 指向的是结束位置,那么在输出10之后,应该是 - - 迭代器获得9位置的迭代器,但是不是这样的,这也是在我们了解底层的情况下才会有这样的感觉,而迭代器的实现就是我们在使用该迭代器后,想继续使用下一个位置,那么就直接 ++ 就好了。
三、常用成员函数
以下介绍一些vector中比较常用的成员函数:
1.size
函数原型:
size_type size() const;
返回当前数据块存储的有效数据的个数
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
cout << v.size() << endl;
return 0;
}
2.resize
函数原型:
void resize (size_type n, value_type val = value_type());
- n:表示该数据块能存储多少个元素
- val:将每个元素初始为val
改变当前数据块的总存储空间大小,并初始化,存在三种情况:
- 当n小于当前数据块的总大小时,只会保留前n个元素,会改变数据块的size(),但是不会改变原本的capacity()
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
cout << "before resize:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl;
cout << "before capacity:" << endl << v.capacity() << endl;
cout << "before size:" << endl << v.size() << endl;
cout << endl;
v.resize(5);
cout << "after resize:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl;
cout << "after capacity:" << endl << v.capacity() << endl;
cout << "after size:" << endl << v.size() << endl;
return 0;
}
- 当n等于当前数据块的总大小,但是该数据块,只用了部分空间时,其余空间都会被初始化为val
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.reserve(10);
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
cout << "before resize:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl;
cout << "before capacity:" << endl << v.capacity() << endl;
cout << "before size:" << endl << v.size() << endl;
cout << endl;
v.resize(10, 10);
cout << "after resize:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl;
cout << "before capacity:" << endl << v.capacity() << endl;
cout << "before size:" << endl << v.size() << endl;
return 0;
}
- 当n大于当前数据块的总大小时,相当于是对数据块进行扩容了,原本的数据会被拷贝到新的空间中,并且后续未使用的空间都会被初始化为val
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
cout << "before resize:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl;
cout << "before capacity:" << endl << v.capacity() << endl;
cout << "before size:" << endl << v.size() << endl;
cout << endl;
v.resize(15, 0);
cout << "after resize:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl;
cout << "before capacity:" << endl << v.capacity() << endl;
cout << "before size:" << endl << v.size() << endl;
return 0;
}
3.capacity
函数原型:
size_type capacity() const;
返回当前数据块的容量大小。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
cout << v.capacity() << endl;
return 0;
}
4.empty
函数原型:
bool empty() const;
判断当前的容量是否为空,如果为空返回true(1),反之返回false(0)。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
//不为空
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
cout << v.empty() << endl;
//清空所存储元素
v.clear();
cout << v.empty() << endl;
return 0;
}
5.reserve
函数原型:
void reserve (size_type n);
- n:修改后的容量大小
reserve可以扩容,但是并不会初始化,所以它不会改变容器的size。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
cout << "before capacity:" << endl << v.capacity() << endl;
cout << "before size:" << endl << v.size() << endl << endl;
v.reserve(15);
cout << "after capacity:" << endl << v.capacity() << endl;
cout << "after size:" << endl << v.size() << endl;
return 0;
}
6.operator[ ]
函数原型:
//const
reference operator[] (size_type n);
//非const
const_reference operator[] (size_type n) const;
- n:需要访问位置的下标
方括号的返回值reference、const_reference是容器元素的引用类型。
返回对向量容器中位置n处元素的引用,可以通过 [ ] 对该位置进行赋值、修改等操作,使用起来和数组的下标 [ ] 一样。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v(10);
for (int i = 0; i < 10; ++i)
{
v[i] = i + 1;
}
return 0;
}
[ ] 的重载大大提高了vector使用的效率。
7.push_back
函数原型:
void push_back (const value_type& val);
- val:要尾插的数据
从数据块的尾部插入一个元素。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
return 0;
}
8.pop_back
函数原型:
void pop_back();
删除数据块尾部的一个元素。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
v.pop_back();
v.pop_back();
v.pop_back();
return 0;
}
9.insert
最常用的一种插入方式:
iterator insert (iterator position, const value_type& val);
- position:一个迭代器,表示插入在该迭代器位置之前
- val:表示要插入的数据
返回值是新插入元素的迭代器。
在数据块的头部和尾部插入数据很简单,利用begin() 和end() 可以轻松确定头部和尾部的迭代器:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
cout << "before insert:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl << endl;
//头部插入0
v.insert(v.begin(), 0);
cout << "head insert:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl << endl;
//尾部插入6
v.insert(v.end(), 6);
cout << "tail insert:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl << endl;
return 0;
}
在任意位置的插入,可以借助算法库中的find() 来获取需要插入位置的迭代器:
find是一个函数模板:
//头文件
#include<algorithm>
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
传递的参数分别是区间的开头和结尾的迭代器,最后一个参数val则表示所要在该区间内查询的元素,返回值则为该元素的迭代器。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
cout << "before insert:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl << endl;
//在3的前面插入30
vector<int>::iterator pos = find(v.begin(), v.end(), 3);
v.insert(pos, 30);
cout << "after insert:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl << endl;
return 0;
}
关于insert存在迭代器失效的问题,vector的底层是动态的连续存储的物理空间,而它的空间在经过不断的添加数据后总会满的,所以在空间满了之后势必会进行扩容,而在进行扩容后,迭代器的位置就是不合法的:
此时再对该迭代器进行操作就不合法了,虽然是存在迭代器失效的问题,但是vector底层的实现也是解决了该问题,但是还是要知道有该问题的存在。
10.erase
最常用的一种删除方式:
iterator erase (iterator position);
- position:所要删除数据的迭代器
erase的返回值为删除元素的后一个位置的迭代器。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
cout << "before erase:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl << endl;
//删除4
vector<int>::iterator pos = find(v.begin(), v.end(), 4);
v.erase(pos);
cout << "after erase:" << endl;
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl << endl;
return 0;
}
erase也存在迭代器失效的问题,即删除了迭代器原本指向的位置后,该迭代器指向的就不是自己原本的数据了,此时如果再对该迭代器进行操作的话,就会出现异常:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
vector<int>::iterator pos = find(v.begin(), v.end(), 4);
//删除该迭代器pos位置的4
v.erase(pos);
//再对pos位置进行赋值
*pos = 100;
return 0;
}
所以再删除了迭代器位置的元素之后,就不要再使用该迭代器了,以免出现不必要的麻烦。
总结
温故而知新。