跟我一起学STL(2)——vector容器详解
一、引言
在上一个专题中,我们介绍了STL中的六大组件,其中容器组件是大多数人经常使用的,因为STL容器是把运用最广的数据结构实现出来,所以我们写应用程序时运用的比较多。然而容器又可以序列式容器和关联式容器两类,C++语言本身提供了一个序列式容器数组,另外STL又提供vector,list,deque等序列式容器,本专题将详细介绍下vector容器。
二、vector详解
2.1 vector容器介绍
vector容器的数据结构为单端数组,其操作方式与数组的操作非常相似,唯一不同的是——数组是静态空间,一旦配置了数组大小就不能改变,而vector是动态空间,随着元素的插入,vector类内部机制会自行扩充空间来容纳新的元素,我们不需要担心因为空间不足而一开始就申请一个空间较大的数组,使用vector更加灵活。vector与C++中数组的区别,如.NET中List<T>类与数组的区别一样。
2.2 vector对象创建详解
vector有8个构造函数,其8个构造函数的定义如下代码所示:
// 默认无参构造函数
① vector()
: _Mybase()
{ // construct empty vector
}
//
② explicit vector(const _Alloc& _Al)
: _Mybase(_Al)
{ // construct empty vector with allocator
} ③ explicit vector(size_type _Count)
: _Mybase()
{ // construct from _Count * _Ty()
resize(_Count);
} ④ vector(size_type _Count, const _Ty& _Val)
: _Mybase()
{ // construct from _Count * _Val
_Construct_n(_Count, _STD addressof(_Val));
} ⑤ vector(size_type _Count, const _Ty& _Val, const _Alloc& _Al)
: _Mybase(_Al)
{ // construct from _Count * _Val, with allocator
_Construct_n(_Count, _STD addressof(_Val));
}
// 拷贝构造函数
⑥ vector(const _Myt& _Right)
: _Mybase(_Right._Alval)
{ // construct by copying _Right
if (_Buy(_Right.size()))
_TRY_BEGIN
this->_Mylast = _Ucopy(_Right.begin(), _Right.end(),
this->_Myfirst);
_CATCH_ALL
_Tidy();
_RERAISE;
_CATCH_END
} ⑦ template<class _Iter>
vector(_Iter _First, _Iter _Last)
: _Mybase()
{ // construct from [_First, _Last)
_Construct(_First, _Last, _Iter_cat(_First));
} ⑧ template<class _Iter>
vector(_Iter _First, _Iter _Last, const _Alloc& _Al)
: _Mybase(_Al)
{ // construct from [_First, _Last), with allocator
_Construct(_First, _Last, _Iter_cat(_First));
}
下面演示下利用上面的构造函数来创建不同的vector对象,并输出每个vector对象中的元素,具体演示代码如下所示:
// 额外引入vector头文件
#include <vector>
#include <iostream>
using namespace std;
void main()
{
// 使用_Vector_iterator<int>类默认无参构造函数初始化6个迭代器对象
vector<int>::iterator v1_Iter,v2_Iter,v3_Iter,v4_Iter,v5_Iter,v6_Iter;
// 创建一个空的vector对象v0,调用了①构造函数
vector<int> v0; // 创建vector对象v1,v1包含3个默认值为0的元素,调用③构造函数
vector<int> v1();
// 创建vector对象v2,v2包含5个值为2的元素,调用④构造函数
vector<int> v2(,);
// 创建vector对象v3,v3包含3个值为1的元素和空间配置器,调用了⑤构造函数
vector<int> v3(,,v2.get_allocator());
// 创建了vector对象v2的拷贝v4,调用了⑥构造函数
vector<int> v4(v2); // 创建一个临时vector对象并对对象元素进行初始化
vector<int> v5();
for(int index=;index<;index++)
{
v5[index]=index;
}
// 创建vector对象v6,调用⑦构造函数
vector<int> v6(v5.begin()+,v5.begin()+);
// 输出v1容器元素
cout<<"v1=";
for(v1_Iter=v1.begin();v1_Iter!=v1.end();v1_Iter++)
{
cout<<" "<<*v1_Iter;
}
cout<<endl; // 输出v2容器元素
cout<<"v2=";
for(v2_Iter=v2.begin();v2_Iter!=v2.end();v2_Iter++)
{
cout<<" "<<*v2_Iter;
}
cout<<endl; // 输出v3容器元素
cout<<"v3=";
for(v3_Iter=v3.begin();v3_Iter!=v3.end();v3_Iter++)
{
cout<<" "<<*v3_Iter;
}
cout<<endl; // 输出v1容器元素
cout<<"v4=";
for(v4_Iter=v4.begin();v4_Iter!=v4.end();v4_Iter++)
{
cout<<" "<<*v4_Iter;
}
cout<<endl;
cout << "v5 =";
for ( v5_Iter = v5.begin( ) ; v5_Iter != v5.end( ) ; v5_Iter++ )
{
cout << " " << *v5_Iter;
}
cout << endl; cout << "v6 =";
for ( v6_Iter = v6.begin( ) ; v6_Iter != v6.end( ) ; v6_Iter++ )
{
cout << " " << *v6_Iter;
}
cout << endl;
// move(v2)返回v2类型即vector类型,v7是通过拷贝构造函数进行创建的
vector<int> v7(move(v2));
vector <int>::iterator v7_Iter;
cout << "v7 =";
for ( v7_Iter = v7.begin( ) ; v7_Iter != v7.end( ) ; v7_Iter++ )
{
cout << " " << *v7_Iter;
}
cout << endl;
}
上面演示代码运行结果如下图所示:

2.3 vector元素操作
vector类中提供了很多成员函数,下面演示下一些常用函数使用,具体演示代码如下:
#include <vector>
#include <iostream>
using namespace std; void print(vector<int> &v)
{
for(size_t i=;i<v.size();i++)
{
cout<<v[i]<<" ";
}
cout<<endl;
} void main()
{
#pragma region 元素的遍历访问
// 可以采用数组下标、at()函数和迭代器的方式进行遍历访问
vector<int> vint;
// 向尾端插入一个元素20
vint.push_back();
vint.push_back();
vint.push_back();
vint.push_back();
// 使用数组小标方式
cout<<"vint[1] = "<<vint[]<<endl;
for(size_t i=;i <vint.size();i++)
{
// 数组小标访问
//cout<<"vint["<<i<<"] = "<<vint[i]<<" "; // 使用at函数访问
cout<<"vint["<<i<<"] = "<<vint.at(i)<<" ";
}
cout<<endl;
#pragma endregion #pragma region 利用erase函数删除元素
// 删除第二个元素,即删除11
vint.erase(vint.begin()+);
cout<<"删除第一个元素后:"<<endl;
int index1;
vector<int>::iterator iter;
// 使用迭代器访问元素
for(iter=vint.begin(),index1=;iter!=vint.end();iter++,index1++)
{
cout<<"vint["<<index1<<"] = "<<*iter<<" ";
}
cout<<endl;
#pragma endregion #pragma region 利用pop_back函数删除尾部元素
// 调用pop_back删除尾部元素,即删除25
vint.pop_back();
cout<<"删除最后一个元素后:"<<endl;
vector<int>::iterator iter1;
int j;
// 使用迭代器访问元素
for(iter1=vint.begin(),j=;iter1!=vint.end();iter1++,j++)
{
cout<<"vint["<<j<<"] = "<<*iter1<<" ";
}
cout<<endl;
#pragma endregion #pragma region 反向遍历 vector 的元素
// 调用inser把元素插入到vector容器中指定位置
// 插入11到容器中第二个位置
vint.insert(vint.begin()+,);
// 插入25到容器中最后一个位置,等效于push_back函数
vint.insert(vint.end(),);
vector<int>::iterator iter2;
int k;
cout<<"insert两个元素后容器元素为:"<<endl;
// 使用迭代器访问元素
for(iter2=vint.begin(),k=;iter2!=vint.end();iter2++,k++)
{
cout<<"vint["<<k<<"] = "<<*iter2<<" ";
}
cout<<endl;
cout<<"反向遍历的结果为:"<<endl;
vector<int>::reverse_iterator riter;
int index;
for( riter=vint.rbegin(),index=;riter!=vint.rend();riter++,index++)
{
cout<<"vint["<<index<<"] = "<<*riter<<" ";
}
cout<<endl;
#pragma endregion #pragma region 两个vector容器元素的交换
// 创建vector<int>对象vint2
vector<int> vint2;
vint2.push_back();
vint2.push_back();
vint2.push_back();
// 把vint与vint2进行交换
//swap(vint,vint2);
vint.swap(vint2);
cout<<"vint与vint2交换后的结果:"<<endl;
cout<<"vint = ";
print(vint);
cout<<"vint2 = ";
print(vint2);
#pragma endregion }
上面代码的运行结果为:

三、小结
到这里,本专题的内容介绍结束了,vector向量容器实现为数据线性存储的泛型类,本文介绍了使用数组下标、at()函数和迭代器方式来进行元素访问,然后演示了一些常用函数的使用。vector尾部添加和移除元素效率非常高,但在中部或头部插入元素和删除元素效率较低,这与它的数据结构(线性连续存储方式)有着密切的关系。
跟我一起学STL(2)——vector容器详解的更多相关文章
- STL之vector容器详解
vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...
- [转]STL之vector容器详解
vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...
- 2.3 C++STL vector容器详解
文章目录 2.3.1 引入 2.3.2 代码实例 2.3.3 运行结果 总结 2.3.1 引入 vector 容器 动态数组 可变数组 vector容器 单口容器(尾部操作效率高) vector动态增 ...
- C++STL之Vector向量详解,用法和例子 一起学习 一起加油
C++ STL之vector用法总结 1 ...
- [转]STL之list容器详解
List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容.实际上,list容器就是一个双向链表,可以高效地进行插入删除元素. 使用list容 ...
- [转]STL之deque容器详解
Deque 容器 deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容.deque容器类与vector类似,支持随机访问和快速插入删除,它在容器中 ...
- 2.8 C++STL set/multiset容器详解
文章目录 2.8.1 引入 2.8.2 代码示例 2.8.3 代码运行结果 2.8.4 对组pair的补充 代码实例 运行结果 总结 2.8.1 引入 set/multiset容器概念 set和mul ...
- 2.9 C++STL map/multimap容器详解
文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...
- 带你深入理解STL之Vector容器
C++内置了数组的类型,在使用数组的时候,必须指定数组的长度,一旦配置了就不能改变了,通常我们的做法是:尽量配置一个大的空间,以免不够用,这样做的缺点是比较浪费空间,预估空间不当会引起很多不便. ST ...
随机推荐
- ajax执行完成后,再执行下面的代码的解决办法
一般ajax设置的都是异步的,但是有时候我们有这种需求,就是等ajax执行完成之后,在执行下面的函数. 1设置async:false 在jq中直接设置了ajax是异步的还是同步的 一般如果不写这个,默 ...
- ajax XML
<script src="jquery-1.11.2.min.js"></script> </head> <body> <se ...
- EMR,电子病历(Electronic Medical Record)
电子病历 电子病历(EMR,Electronic Medical Record),也叫计算机化的病案系统或称基于计算机的病人记录(CPR,Computer-Based Patient Record). ...
- A required class was missing while executing org.apache.maven.plugins:maven-war-plugin:2.1.1:war
完美解决方案: http://stackoverflow.com/questions/18442753/a-required-class-was-missing-while-executing-org ...
- win7,win8.1下hosts文件无法修改的快速解决办法
一,找到C:\Windows\System32\drivers\etc,下hosts文件复制一份到桌面: 二,使用notepad++或其他编辑器修改桌面复制出来的那份HOSTS: 三,将修改后的文件复 ...
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。
今天单位一ASP.NET网站,里面有个功能是导出数据,发现一导出就报错,报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT ...
- java的四种取整方法
java 中取整操作提供了四种方法:分别是: public static double ceil(double a)//向上取整 public static double floor(double ...
- WebView加载本地Html文件并实现点击效果
Webview是用来与前端交互的纽,可以加载本地Html文件,和网页并实现交互的功能. WebView通过WebSetting可以使用Android原生的JavascriptInterface来进行j ...
- [原创]HEXO博客搭建日记
博客系统折腾了好久,使用过Wordpress,Ghost,Typecho,其中Typecho是我使用起来最舒心的一种,Markdown编辑+轻量化设计,功能不多不少刚好,着实让我这种强迫症患者舒服了好 ...
- Hdu OJ 5884-Sort (2016 ACM/ICPC Asia Regional Qingdao Online)(二分+优化哈夫曼)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 题目大意:有n个有序的序列,对于第i个序列有ai个元素. 现在有一个程序每次能够归并k个序列, ...