vector容器用法详解
vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组。像数组一样,vector类也用从0开始的下标表示元素的位置;但和数组不同的是,当vector对象创建后,数组的元素个数会随着vector对象元素个数的增大和缩小而自动变化。
vector类常用的函数如下所示:
1.构造函数
- vector():创建一个空vector
- vector(int nSize):创建一个vector,元素个数为nSize
- vector(int nSize,const t& t):创建一个vector,元素个数为nSize,且值均为t
- vector(const vector&):复制构造函数
- vector(begin,end):复制[begin,end)区间内另一个数组的元素到vector中
2.增加函数
- void push_back(const T& x):向量尾部增加一个元素X
- iterator insert(iterator it,const T& x):向量中迭代器指向元素前增加一个元素x
- iterator insert(iterator it,int n,const T& x):向量中迭代器指向元素前增加n个相同的元素x
- iterator insert(iterator it,const_iterator first,const_iterator last):向量中迭代器指向元素前插入另一个相同类型向量的[first,last)间的数据
3.删除函数
- iterator erase(iterator it):删除向量中迭代器指向元素
- iterator erase(iterator first,iterator last):删除向量中[first,last)中元素
- void pop_back():删除向量中最后一个元素
- void clear():清空向量中所有元素
4.遍历函数
- reference at(int pos):返回pos位置元素的引用
- reference front():返回首元素的引用
- reference back():返回尾元素的引用
- iterator begin():返回向量头指针,指向第一个元素
- iterator end():返回向量尾指针,指向向量最后一个元素的下一个位置
- reverse_iterator rbegin():反向迭代器,指向最后一个元素
- reverse_iterator rend():反向迭代器,指向第一个元素之前的位置
5.判断函数
- bool empty() const:判断向量是否为空,若为空,则向量中无元素
6.大小函数
- int size() const:返回向量中元素的个数
- int capacity() const:返回当前向量张红所能容纳的最大元素值
- int max_size() const:返回最大可允许的vector元素数量值
7.其他函数
- void swap(vector&):交换两个同类型向量的数据
- void assign(int n,const T& x):设置向量中第n个元素的值为x
- void assign(const_iterator first,const_iterator last):向量中[first,last)中元素设置成当前向量元素
示例:
1.初始化示例
- #include "stdafx.h"
- #include<iostream>
- #include<vector>
- using namespace std;
- class A
- {
- //空类
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- //int型vector
- vector<int> vecInt;
- //float型vector
- vector<float> vecFloat;
- //自定义类型,保存类A的vector
- vector<A> vecA;
- //自定义类型,保存指向类A的指针的vector
- vector<A*> vecPointA;
- return 0;
- }
- // vectorsample.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include<iostream>
- #include<vector>
- using namespace std;
- class A
- {
- //空类
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- //int型vector,包含3个元素
- vector<int> vecIntA(3);
- //int型vector,包含3个元素且每个元素都是9
- vector<int> vecIntB(3,9);
- //复制vecIntB到vecIntC
- vector<int> vecIntC(vecIntB);
- int iArray[]={2,4,6};
- //创建vecIntD
- vector<int> vecIntD(iArray,iArray+3);
- //打印vectorA,此处也可以用下面注释内的代码来输出vector中的数据
- /*for(int i=0;i<vecIntA.size();i++)
- {
- cout<<vecIntA[i]<<" ";
- }*/
- cout<<"vecIntA:"<<endl;
- for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
- {
- cout<<*it<<" ";
- }
- cout<<endl;
- //打印vecIntB
- cout<<"VecIntB:"<<endl;
- for(vector<int>::iterator it = vecIntB.begin() ;it!=vecIntB.end();it++)
- {
- cout<<*it<<" ";
- }
- cout<<endl;
- //打印vecIntC
- cout<<"VecIntB:"<<endl;
- for(vector<int>::iterator it = vecIntC.begin() ;it!=vecIntC.end();it++)
- {
- cout<<*it<<" ";
- }
- cout<<endl;
- //打印vecIntD
- cout<<"vecIntD:"<<endl;
- for(vector<int>::iterator it = vecIntD.begin() ;it!=vecIntD.end();it++)
- {
- cout<<*it<<" ";
- }
- cout<<endl;
- return 0;
- }
程序的运行结果如下:

上面的代码用了4种方法建立vector并对其初始化
2.增加及获得元素示例:
- // vectorsample.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include<iostream>
- #include<vector>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- //int型vector,包含3个元素
- vector<int> vecIntA;
- //插入1 2 3
- vecIntA.push_back(1);
- vecIntA.push_back(2);
- vecIntA.push_back(3);
- int nSize = vecIntA.size();
- cout<<"vecIntA:"<<endl;
- //打印vectorA,方法一:
- for(int i=0;i<nSize;i++)
- {
- cout<<vecIntA[i]<<" ";
- }
- cout<<endl;
- //打印vectorA,方法二:
- for(int i=0;i<nSize;i++)
- {
- cout<<vecIntA.at(i)<<" ";
- }
- cout<<endl;
- //打印vectorA,方法三:
- for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
- {
- cout<<*it<<" ";
- }
- cout<<endl;
- return 0;
- }
上述代码对一个整形向量类进行操作,先定义一个整形元素向量类,然后插入3个值,并用3种不同的方法输出,程序运行结果如下:

- // vectorsample.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include<iostream>
- #include<vector>
- using namespace std;
- class A
- {
- public:
- int n;
- public:
- A(int n)
- {
- this->n = n;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- //int型vector,包含3个元素
- vector<A> vecClassA;
- A a1(1);
- A a2(2);
- A a3(3);
- //插入1 2 3
- vecClassA.push_back(a1);
- vecClassA.push_back(a2);
- vecClassA.push_back(a3);
- int nSize = vecClassA.size();
- cout<<"vecClassA:"<<endl;
- //打印vecClassA,方法一:
- for(int i=0;i<nSize;i++)
- {
- cout<<vecClassA[i].n<<" ";
- }
- cout<<endl;
- //打印vecClassA,方法二:
- for(int i=0;i<nSize;i++)
- {
- cout<<vecClassA.at(i).n<<" ";
- }
- cout<<endl;
- //打印vecClassA,方法三:
- for(vector<A>::iterator it = vecClassA.begin();it!=vecClassA.end();it++)
- {
- cout<<(*it).n<<" ";
- }
- cout<<endl;
- return 0;
- }
上述代码通过定义元素为类的向量,通过插入3个初始化的类,并通过3种方法输出,运行结果如下:
- // vectorsample.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include<iostream>
- #include<vector>
- using namespace std;
- class A
- {
- public:
- int n;
- public:
- A(int n)
- {
- this->n = n;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- //int型vector,包含3个元素
- vector<A*> vecClassA;
- A *a1 = new A(1);
- A *a2 = new A(2);
- A *a3 = new A(3);
- //插入1 2 3
- vecClassA.push_back(a1);
- vecClassA.push_back(a2);
- vecClassA.push_back(a3);
- int nSize = vecClassA.size();
- cout<<"vecClassA:"<<endl;
- //打印vecClassA,方法一:
- for(int i=0;i<nSize;i++)
- {
- cout<<vecClassA[i]->n<<"\t";
- }
- cout<<endl;
- //打印vecClassA,方法二:
- for(int i=0;i<nSize;i++)
- {
- cout<<vecClassA.at(i)->n<<"\t";
- }
- cout<<endl;
- //打印vecClassA,方法三:
- for(vector<A*>::iterator it = vecClassA.begin();it!=vecClassA.end();it++)
- {
- cout<<(**it).n<<"\t";
- }
- cout<<endl;
- delete a1; delete a2;delete a3;
- return 0;
- }
上述代码通过定义元素为类指针的向量,通过插入3个初始化的类指针,并通过3种方法输出指针指向的类,运行结果如下:
3.修改元素示例
- // vectorsample.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include<iostream>
- #include<vector>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- //int型vector,包含3个元素
- vector<int> vecIntA;
- //插入1 2 3
- vecIntA.push_back(1);
- vecIntA.push_back(2);
- vecIntA.push_back(3);
- int nSize = vecIntA.size();
- //通过引用修改vector
- cout<<"通过数组修改,第二个元素为8:"<<endl;
- vecIntA[1]=8;
- cout<<"vecIntA:"<<endl;
- //打印vectorA
- for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
- {
- cout<<*it<<" ";
- }
- cout<<endl;
- //通过引用修改vector
- cout<<"通过引用修改,第二个元素为18:"<<endl;
- int &m = vecIntA.at(1);
- m=18;
- cout<<"vecIntA:"<<endl;
- //打印vectorA
- for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
- {
- cout<<*it<<" ";
- }
- cout<<endl;
- //通过迭代器修改vector
- cout<<"通过迭代器修改,第二个元素为28"<<endl;
- vector<int>::iterator itr = vecIntA.begin()+1;
- *itr = 28;
- cout<<"vecIntA:"<<endl;
- //打印vectorA
- for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
- {
- cout<<*it<<" ";
- }
- cout<<endl;
- return 0;
- }
程序运行结果如下:

4.删除向量示例
- // vectorsample.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include<iostream>
- #include<vector>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- //int型vector,包含3个元素
- vector<int> vecIntA;
- //循环插入1 到10
- for(int i=1;i<=10;i++)
- {
- vecIntA.push_back(i);
- }
- vecIntA.erase(vecIntA.begin()+4);
- cout<<"删除第5个元素后的向量vecIntA:"<<endl;
- //打印vectorA
- for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
- {
- cout<<*it<<"\t";
- }
- cout<<endl;
- //删除第2-5个元素
- vecIntA.erase(vecIntA.begin()+1,vecIntA.begin()+5);
- cout<<"删除第2-5个元素后的vecIntA:"<<endl;
- //打印vectorA
- for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
- {
- cout<<*it<<"\t";
- }
- cout<<endl;
- //删除最后一个元素
- vecIntA.pop_back();
- cout<<"删除最后一个元素后的vecIntA:"<<endl;
- //打印vectorA
- for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
- {
- cout<<*it<<"\t";
- }
- cout<<endl;
- return 0;
- }
程序运行结果如下:

3.进一步理解vector,如下图所示:

4.综合示例
- // vectorsample.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include<iostream>
- #include<vector>
- #include<string>
- using namespace std;
- class Student
- {
- public:
- string m_strNO;
- string m_strName;
- string m_strSex;
- string m_strDate;
- public:
- Student(string strNO,string strName,string strSex,string strDate)
- {
- m_strNO = strNO;
- m_strName = strName;
- m_strSex = strSex;
- m_strDate = strDate;
- }
- void Display()
- {
- cout<<m_strNO<<"\t";
- cout<<m_strName<<"\t";
- cout<<m_strSex<<"\t";
- cout<<m_strDate<<"\t";
- }
- };
- class StudCollect
- {
- vector<Student> m_vStud;
- public:
- void Add(Student &s)
- {
- m_vStud.push_back(s);
- }
- Student* Find(string strNO)
- {
- bool bFind = false;
- int i;
- for(i = 0;i < m_vStud.size();i++)
- {
- Student& s = m_vStud.at(i);
- if(s.m_strNO == strNO)
- {
- bFind = true;
- break;
- }
- }
- Student *s = NULL;
- if(bFind)
- s = &m_vStud.at(i);
- return s;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- Student s1("1001","zhangsan","boy","1988-10-10");
- Student s2("1002","lisi","boy","1988-8-25");
- Student s3("1003","wangwu","boy","1989-2-14");
- StudCollect s;
- s.Add(s1);
- s.Add(s2);
- s.Add(s3);
- Student *ps = s.Find("1002");
- if(ps)
- ps->Display();
- return 0;
- }
代码运行实例如下:

vector容器用法详解的更多相关文章
- STL:vector容器用法详解
vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组.像数组一样,vector类也用从0开始的下标表示元素的位置:但和数组不同的是,当vector对象创建后,数组的元素个数会随着ve ...
- STL之二:vector容器用法详解
转载于:http://blog.csdn.net/longshengguoji/article/details/8507394 vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组. ...
- c++中vector的用法详解
c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...
- STL vector常见用法详解
<算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...
- C++:vector的用法详解
原文地址:http://blog.csdn.net/hancunai0017/article/details/7032383 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于 ...
- C++标准模板库(STL)——vector常见用法详解
vector的定义 vector<typename> name; 相当于定义了一个一维数组name[SIZE],只不过其长度可以根据需要进行变化,比较节省空间,通俗来讲,vector就是& ...
- 7-set用法详解
C++中set用法详解 转载 http://blog.csdn.net/yas12345678/article/details/52601454 C++ / set 更详细见:http://www.c ...
- STL priority_queue 常见用法详解
<算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...
- STL set 常见用法详解
<算法笔记>学习笔记 set 常见用法详解 set是一个内部自动有序且不含重复元素的容器 1. set 的定义 //单独定义一个set set<typename> name: ...
随机推荐
- LeetCode - 刷题经验
1.加快代码速度 刷了前面几道题发现速度总是处于尾部10%,刚开始非常不服,后来仔细一看那些排名靠前的提交,发现了猫腻.几乎每一个提交都有这样的一段代码: static const auto io_s ...
- [TFS教程]TFS: Get Command
Get Command Visual Studio 2012 Gets (downloads) either the latest version or a specified version o ...
- 20175227张雪莹 2018-2019-2 《Java程序设计》第三周学习总结
20175227张雪莹 2018-2019-2 <Java程序设计>第三周学习总结 教材学习内容总结 (仅在此列举个性化学习总结) 一.编程语言的几个发展阶段. 1.面向机器语言:汇编语言 ...
- LDAP认证模式简介
今天发现公共服务中有ldap数据库服务,先大概了解一下ldap,转载下面的文章.原文链接:https://www.jianshu.com/p/d3f8c8f5d661 另外记录一篇文章地址:https ...
- [UE4]ProgressBar,进度条
准备好2张进度条图片 一.新建名为“testProgress”的UserWidget,添加一个名为“ProgressBar_0”的ProgressBar到默认容器Canvas Panel 二.进度条进 ...
- rabbitmq (三) 发布/订阅
rabbitmq的目的并不是让生产者把消息直接发到队列里面去, 这样不能实现解耦的目的,也不利于程序的扩展. 所以就有交换机(exchanges)的概念. 交换机有几种类型:direct, topic ...
- mysql中间件kingshard
这样写是OK的: select * from bind_history limit 10;select id, passport_id, person_id, create_time, cast(is ...
- Python—requests模块详解
1.模块说明 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支持使用co ...
- [持续交付实践] 开篇:持续集成&持续交付综述
前言 随着微服务架构与容器虚拟化技术的发展,持续集成与持续交付的概念又重新回到了大家的视野,越来越多的公司开始使用持续集成的系统来解决频繁发布带来的质量问题:使用持续交付的工具来实现代码在不同环境上的 ...
- ipv6无网络访问权限怎么办
有时IP4和IP6都正常连接,但突然又出现“IPV6无网络访问权限” 这是win7系统下经常发生的事情,如下图. 方法/步骤 1.IPV6没网络权限是正常的因为你没有IPV6的网络环境,那个只有部分教 ...