一、什么是Vector
        向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container)。跟任意其它类型容器一样,它能够存放各种类型的对象。可以简单的认为,向量是一个能够存放任意类型的动态数组。

二、定义和初始化Vector对象

//初始化vector对象的方法
vector<T> V1;
vector<T> v2(v1);
vector<T> v3 = v2;
vector<T> v4(n, val);
vector<T> v5(n);
vector<T> v6{a, b, c};
vector<T> v7 = {1, 2, 3};

三、增加函数

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)间的数据

1、使用push_back,将一个元素追加到vector的尾部

#include <iostream>
#include<string>
#include<vector>
using namespace std; int main(){
vector<string> v1;
string str;
while(cin >> str){
v1.push_back(str);
}
for(auto x : v1){ //范围for语句
cout << x << " ";
}
cout << endl;
return 0;
}

2、insert函数允许在容器任何位置插入一个或多个元素,每个Insert函数接收迭代器作为其第一个参数,指出在容器什么位置放置新元素。

#include <iostream>
#include<string>
#include<vector>
using namespace std; int main(){
vector<string> v1;
vector<string> v2 = {"z3", "w5", "y2"};
string str; v1.insert(v1.begin(), "Hello");
v1.insert(v1.end(), 3, "World"); //插入范围元素
v1.insert(v1.begin(), v2.begin(), v2.end()); //接收一对迭代器,将范围中的元素插入 for(auto x : v1){
cout << x << " ";
}
cout << endl;
return 0;
}

通过insert的返回值,可以在一个特定位置反复插入元素

四、删除函数

iterator erase(iterator it);//删除向量中迭代器指向元素
iterator erase(iterator first,iterator last);//删除向量中[first,last)中元素
void pop_back();//删除向量中最后一个元素
void clear();//清空向量中所有元素

1、删除vector中所有奇数元素

#include <iostream>
#include<vector>
using namespace std; int main(){ vector<int> v1 = {1, 2, 3, 4, 5};
auto it = v1.begin();
while(it != v1.end()){
if(*it % 2) it = v1.erase(it);
else ++it;
} for(auto x : v1){
cout << x << " ";
}
cout << endl;
return 0;
}

2、删除一个范围内的所有元素

五、其他函数

void swap(vector&);//交换两个同类型向量的数据
void assign(int n,const T& x);//设置向量中前n个元素的值为x
void assign(const_iterator first,const_iterator last);//向量中[first,last)中元素设置成当前向量元素

六、迭代器失效

迭代器失效是指迭代器底层对应的指针所指的空间无效了,而使用一块已经被释放的空间,造成的后果是程序崩溃。

1、扩容导致迭代器失效。vector插入元素可能会导致容量不足,会触发扩容,导致整个vector重新申请内存,并且将原有的数据复制到新的内存中,并将原有内存释放,迭代器所指的内存释放,迭代器失效。

#include <iostream>
#include<vector>
using namespace std; int main(){ vector<int> v1;
v1.push_back(6);
v1.push_back(9);
auto it = v1.begin(); cout << "v1的容量为:" << v1.capacity() << endl; //容量为2
cout << *it << endl; v1.push_back(10); //扩容,迭代器失效
v1.push_back(5);
cout << "v1的容量为:" << v1.capacity() << endl;
cout << *it << endl;
return 0;
}

2、insert导致迭代器失效

#include<iostream>
#include<vector>
using namespace std; int main()
{
vector<int> ta;
for (int i = 0; i < 13; i++)
{
ta.push_back(i);
} vector<int>::iterator it = ta.begin(); it += 5; cout << "容量是 " << ta.capacity() << endl;
cout << "it的值是 " << *it << endl;
ta.insert(it, 100); cout << "insert后容量是 " << ta.capacity() << endl;
cout << "此时it指向的值是 " << *it << endl; return 0;
}

3、erase导致迭代器失效

#include<iostream>
using namespace std;
#include<vector> int main()
{
vector<int> ta;
for (int i = 0; i < 10; ++i)
{
ta.push_back(i);
} vector<int>::iterator it = ta.begin(); it += 5;
cout << "容量是 " << ta.capacity() << endl;
cout << "it的值是 " << *it << endl; ta.erase(it);  //迭代器失效
cout << "erase后容量是 " << ta.capacity() << endl;
cout << "此时it指向的值是 " << *it << endl; return 0;
}

对于序列式容器,比如vector,插入删除当前的iterator会使后面所有元素的iterator都失效。

STL容器vector的更多相关文章

  1. 从零开始写STL—容器—vector

    从0开始写STL-容器-vector vector又称为动态数组,那么动态体现在哪里?vector和一般的数组又有什么区别?vector中各个函数的实现原理是怎样的,我们怎样使用会更高效? 以上内容我 ...

  2. [C++]STL容器Vector的内存释放

    直接抛出两句话,说明到底应该如何释放Vector占用的内存. “vector的clear不影响capacity,你应该swap一个空的vector.” <Effective STL>中的“ ...

  3. STL容器vector应用注意事项

    [1]提前分配足够空间以免不必要的重新分配和复制代价 关于vector容器重新分配和复制及析构释放的代价,请参见随笔<STL容器之vector>. 应用示例对比代码如下: #include ...

  4. STL容器 vector,list,deque 性能比较

    C++的STL模板库中提供了3种容器类:vector,list,deque对于这三种容器,在觉得好用的同时,经常会让我们困惑应该选择哪一种来实现我们的逻辑.在少量数据操作的程序中随便哪一种用起来感觉差 ...

  5. STL容器 -- Vector

    核心:Vector 是 STL 里的一个向量容器,可以像数组那样进行随机访问,能在尾部插入元素,对于元素的删除和插入可以动态管理内存. 头文件: #include <vector> 构造函 ...

  6. STL - 容器 - vector简单应用

    VectorTest.cpp #include <vector> #include <iostream> #include <string> #include &l ...

  7. ACM常用STL容器

    // STL(标准模板库),由三大部分组成:容器,算法,迭代器 // STL六大组件:container(容器),algorthm(算法),iterator(迭代器) // function obje ...

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

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

  9. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

随机推荐

  1. Pycharm和IDEA利用Git操作Github仓库

    1. Git Bash 选择一个本地代码仓库文件夹:D:/Github_Code/新建文件夹,然后在此目录打开git bash 依次进行: git init //首次需执行,之后可不用 git add ...

  2. 在ubuntu 上安装golang

    https://golang.google.cn/dl/ 方式一 下载安装包 wget https://golang.google.cn/dl/go1.19.linux-amd64.tar.gz 解压 ...

  3. 将Oracle数据库迁移到达梦数据库

    公司某产品在项目现场上常用到的数据库有Oracle和达梦. 做性能测试需要根据项目现场预埋大量的基础数据和业务数据,耗费时间.精力.故完成Oracle数据库的性能测试之后,采用直接将Oracle数据库 ...

  4. 使用 html2canvas 将页面中某一部分转为图片下载

    今天在项目中遇到一个需求是将生成的二维码和一些背景作为海报,然后将海报以图片的形式下载 使用了 html2canvas  插件 import html2canvas from "html2c ...

  5. pagehelper使用有误导致sql多了一个limit

    接口测试报告中发现时不时有一个接口报错,但再跑一次又没有这个报错了.报错信息是sql异常,sql中有两个limit.查看后台代码和XXmapper.xml,发现确实只有一个limit.一开始开发以为是 ...

  6. gin-k8s 运行的问题

    1,k8s admin dashboard项目地址:https://github.com/kubernetes/dashboard项目使用的是golang 作为后端,然后使用angular 作为前段框 ...

  7. 【深入浅出 Yarn 架构与实现】3-3 Yarn Application Master 编写

    本篇文章继续介绍 Yarn Application 中 ApplicationMaster 部分的编写方法. 一.Application Master 编写方法 上一节讲了 Client 提交任务给 ...

  8. linux启动终端出现To run a command as administrator (user root) use sudo command See man sudo_root

    解决方法 touch ~/.sudo_as_admin_successful

  9. JS逆向实战9——cookies DES加密混淆

    cookie加密 DES 混淆 目标网站:aHR0cHM6Ly90bGNoZW1zaG9wLnlvdXpoaWNhaS5jb20vbWFpbi90ZW5kP05vdGljZUNhdGVJZD0xJk5 ...

  10. elasticsearch 聚合之 date_histogram 聚合

    目录 1.背景 2.bucket_key如何计算 3.前置知识 4.日历和固定时间间隔 4.1 Calendar intervals 日历间隔 4.2 Fixed intervals 固定间隔 5.数 ...