1.Vector容器

1)vector是将元素置于一个动态数组中加以管理的容器。

2)vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲)。

3)vector尾部添加或移除元素非常快速。但是在中部或头部插入元素或移除元素比较费时

2.vector对象的默认构造

vector采用模板类实现,vector对象的默认构造形式

vector<T> vecT;

vector<int> vecInt;          //一个存放int的vector容器。

vector<float> vecFloat;     //一个存放float的vector容器。

vector<string> vecString;     //一个存放string的vector容器。

...                                     //尖括号内还可以设置指针类型或自定义类型。

Class CA{};

vector<CA*> vecpCA;             //用于存放CA对象的指针的vector容器。

vector<CA> vecCA;             //用于存放CA对象的vector容器。由于容器元素的存放是按值复制的方式进行的,所以此时CA必须提供CA的拷贝构造函数,以保证CA对象间拷贝正常。

3.vector对象的带参数构造

vector(beg,end);    //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。

vector(n,elem);   //构造函数将n个elem拷贝给本身。

vector(const vector &vec);  //拷贝构造函数

int  iArray[] = {,,,,};

vector<int>  vecIntA( iArray,  iArray+ );

vector<int> vecIntB (  vecIntA.begin() , vecIntA.end()  );   //用构造函数初始化容器vecIntB

vector<int> vecIntB (  vecIntA.begin() , vecIntA.begin()+  ); 

vector<int> vecIntC(,); //此代码运行后,容器vecIntB就存放3个元素,每个元素的值是9。

vector<int> vecIntD(vecIntA);

4.vector的赋值

vector.assign(beg,end);    //将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。

vector.assign(n,elem);  //将n个elem拷贝赋值给本身。

vector& operator=(const vector  &vec);          //重载等号操作符

vector.swap(vec);  // 将vec与本身的元素互换。

vector<int> vecIntA, vecIntB, vecIntC, vecIntD;
int iArray[] = {,,,,};
vecIntA.assign(iArray,iArray+); vecIntB.assign( vecIntA.begin(), vecIntA.end() ); //用其它容器的迭代器作参数。 vecIntC.assign(,); vector<int> vecIntD;
vecIntD = vecIntA; vecIntA.swap(vecIntD);

5.vector的大小

vector.size();         //返回容器中元素的个数

vector.empty();     //判断容器是否为空

vector.resize(num);   //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。

vector.resize(num, elem);  //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。

vecInt是vector<int>  声明的容器,现已包含1,,3元素。
int iSize = vecInt.size(); //iSize == 3;
bool bEmpty = vecInt.empty(); // bEmpty == false; 执行vecInt.resize(); //此时里面包含1,2,3,0,0元素。
再执行vecInt.resize(,); //此时里面包含1,2,3,0,0,3,3,3元素。
再执行vecInt.resize(); //此时里面包含1,2元素。

6.vector末尾的添加移除操作

vector<int> vecInt;

vecInt.push_back();  //在容器尾部加入一个元素

vecInt.push_back();  //移除容器中最后一个元素

vecInt.push_back(); 

vecInt.push_back();

vecInt.push_back();

vecInt.pop_back();   

vecInt.pop_back();

//{5 ,7 ,9}  

7vector的数据存取

vec.at(idx);    //返回索引idx所指的数据,如果idx越界,抛出out_of_range异常。

vec[idx];          //返回索引idx所指的数据,越界时,运行直接报错

vector<int> vecInt;    //假设包含1 ,3 ,5 ,7 ,9
vecInt.at() == vecInt[] ; //
vecInt.at() = ; 或 vecInt[] = ;
vecInt 就包含 , , , , 9值 int iF = vector.front(); //iF==1
int iB = vector.back(); //iB==9
vector.front() = ; //vecInt包含{11,3,8,7,9}
vector.back() = ; //vecInt包含{11,3,8,7,19}

8.vector的插入

vector.insert(pos,elem);   //在pos位置插入一个elem元素的拷贝,返回新数据的位置。

vector.insert(pos,n,elem);   //在pos位置插入n个elem数据,无返回值。

vector.insert(pos,beg,end);   //在pos位置插入[beg,end)区间的数据,无返回值

vector<int> vecA;
vector<int> vecB; vecA.push_back();
vecA.push_back();
vecA.push_back();
vecA.push_back();
vecA.push_back(); vecB.push_back();
vecB.push_back();
vecB.push_back();
vecB.push_back(); vecA.insert(vecA.begin(), ); //{11, 1, 3, 5, 7, 9}
vecA.insert(vecA.begin()+,,); //{11,33,33,1,3,5,7,9}
vecA.insert(vecA.begin() , vecB.begin() , vecB.end() ); //{2,4,6,8,11,33,33,1,3,5,7,9}

9.vector的删除

vector.clear();    //移除容器的所有数据

vec.erase(beg,end);  //删除[beg,end)区间的数据,返回下一个数据的位置。

vec.erase(pos);    //删除pos位置的数据,返回下一个数据的位置。

删除区间内的元素

vecInt是用vector<int>声明的容器,现已包含按顺序的1,,,,9元素。

vector<int>::iterator itBegin=vecInt.begin()+;

vector<int>::iterator itEnd=vecInt.begin()+;

vecInt.erase(itBegin,itEnd);

//此时容器vecInt包含按顺序的1,6,9三个元素。

假设 vecInt 包含1,3,2,3,3,3,4,3,5,3,删除容器中等于3的元素


for(vector<int>::iterator it=vecInt.being(); it!=vecInt.end(); )    //小括号里不需写  ++it

{

if(*it == 3)

{

it  =  vecInt.erase(it);       //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。

//此时,不执行  ++it;

}

else

{

++it;

}

}

//删除vecInt的所有元素


vecInt.clear();                    //容器为空

STL之Vector容器的更多相关文章

  1. 带你深入理解STL之Vector容器

    C++内置了数组的类型,在使用数组的时候,必须指定数组的长度,一旦配置了就不能改变了,通常我们的做法是:尽量配置一个大的空间,以免不够用,这样做的缺点是比较浪费空间,预估空间不当会引起很多不便. ST ...

  2. 跟我一起学STL(2)——vector容器详解

    一.引言 在上一个专题中,我们介绍了STL中的六大组件,其中容器组件是大多数人经常使用的,因为STL容器是把运用最广的数据结构实现出来,所以我们写应用程序时运用的比较多.然而容器又可以序列式容器和关联 ...

  3. STL 查找vector容器中的指定对象:find()与find_if()算法

    1 从vector容器中查找指定对象:find()算法 STL的通用算法find()和find_if()可以查找指定对象,参数1,即首iterator指着开始的位置,参数2,即次iterator指着停 ...

  4. STL之vector容器详解

    vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...

  5. STL笔记(に)--vector容器

    Vector 1.可变长的动态数组 2.需包含头文件#include<vector> (当然,如果用了万能头文件#include<bits/stdc++.h>则可忽略) 3.支 ...

  6. [转]STL之vector容器详解

    vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...

  7. STL之vector容器的实现框架

    说明:本文仅供学习交流,转载请标明出处,欢迎转载. 实现vector容器的思路等同于实现一个动态数组,以下我们參照源代码的相关资料,给出一个vector容器的大致框架,仅仅有声明,没给出详细的实现. ...

  8. 【C++】STL,vector容器操作

    C++内置的数组支持容器的机制,但是它不支持容器抽象的语义.要解决此问题我们自己实现这样的类.在标准C++中,用容器向量(vector)实现.容器向量也是一个类模板.标准库vector类型使用需要的头 ...

  9. STL中vector容器实现反转(reverse)

    vector容器中实现可以通过以下两种方式实现: #include "stdafx.h" #include <vector> #include <iostream ...

  10. STL:vector容器用法详解

    vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组.像数组一样,vector类也用从0开始的下标表示元素的位置:但和数组不同的是,当vector对象创建后,数组的元素个数会随着ve ...

随机推荐

  1. LeetCode 136 Single Number 解题报告

    题目要求 Given a non-empty array of integers, every element appears twice except for one. Find that sing ...

  2. LeetCode 104 Maximum Depth of Binary Tree 解题报告

    题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  3. 新建虚拟机_WIN8 64位系统_启动报错Directory "EZBOOT" not found

    准备工作:下载win8 64 镜像文件 1.虚拟机安装win8 64位操作系统,新建虚拟机步骤同XP系统 2.BIOS设置CD/ROM启动,但启动报错,如下,由于镜像文件超过4G,无法从虚拟机安装,需 ...

  4. 新兴的API(fileReader、geolocation、web计时、web worker)

    requestAnimationFrame() 每次浏览器重绘之前会调用这个方法!!! 它接收一个参数,就是回调函数: 它可以保证在最佳的间隔时间调用传入的回调函数,以达到让屏幕产生最流畅的动画效果. ...

  5. python全栈开发day10

    day10知识点总结 while循环补充: continue,终止当前循环,开始下一次循环 break,终止所有循环 pycharm 技巧1.setting 中搜索 mouse 设置鼠标滚轮 改变字体 ...

  6. MySQL Community Server 8.0.11下载与安装配置

    一.下载 1.选择合适的安装包,我在这里下载的是目前最新的安装包,8.0.11,而且我选择下载的是解压版的,安装版的话,安装会比较麻烦. MySQL Community Server下载链接:http ...

  7. 关于Redis的配置

    Redis-配置 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运行时,Redis默认会把pid ...

  8. ssh工具自动化命令

    SCP命令 scp [options] SRC... DEST/ 两种方式: scp [options] [user@]host:/sourcefile  /destpath scp [options ...

  9. poi 实战代码---导出Excel(根据模板导出)

    /** * 导出excel * @param request * @param response * @return * @throws Exception */ @RequestMapping(&q ...

  10. 向数据库中添加数据,通过se16 不能添加,通过 代码可以添加的原因

    1:  在向数据库中添加数据时,通过客户端se16 准备对 数据表进行添加数据,提示如下: 找了以下原因,如下: https://www.baidu.com/link?url=3yRtAfY1_9XG ...