检测vector容器是否为空:

 1  #include <iostream>
2 #include <string>
3 #include <vector>
4 using namespace std;
5
6 int main()
7 {
8 //检测容器是否为空
9 vector<string>vecA;
10 if (vecA.empty())
11 {
12 cout << "mapText为空" << endl;
13 }
14 else
15 {
16 cout << "mapText不为空" << endl;
17 }
18
19 //向容器中添加元素
20 vecA.push_back("A");
21 vecA.push_back("B");
22 vecA.push_back("C");
23
24 if (vecA.empty())
25 {
26 cout << "mapText为空" << endl;
27 }
28 else
29 {
30 cout << "mapText不为空" << endl;
31 }
32
33 cin.get();
34 return 0;
35 }


访问vector容器中的元素:

 1  #include <iostream>
2 #include <string>
3 #include <vector>
4 using namespace std;
5
6 int main()
7 {
8 vector<string>vecA;
9 vecA.push_back("A");
10 vecA.push_back("B");
11 vecA.push_back("C");
12
13 cout << "第1个元素:" << vecA.front() << endl;
14 cout << "最后1个元素:" << vecA.back() << endl;
15 cout << endl;
16
17 cout << "第2个元素:" << *(vecA.begin() + 1) << endl;
18 cout << "倒数第2个元素:" << *(vecA.end() - 1 - 1) << endl;
19 cout << endl;
20
21 cout << "第1个元素:" << *(vecA.begin()) << endl;
22 cout << "最后1个元素:" << *(vecA.end() - 1) << endl;
23 cout << endl;
24
25 for (int i = 0; i < vecA.size(); i++)
26 {
27 cout << "第" << i + 1 << "个元素:" << vecA[i] << endl;
28 }
29 cout << endl;
30
31 //通过迭代器遍历
32 for (vector<string>::iterator pd = vecA.begin(); pd != vecA.end(); pd++)
33 {
34 cout << "第?个元素:" << *pd << endl;
35 }
36 cout << endl;
37
38 cin.get();
39 return 0;
40 }


合并两个Vector,遍历的方法效率极低,推荐使用insert()函数

 1  #include <iostream>
2 #include <string>
3 #include <vector>
4 using namespace std;
5 int main()
6 {
7 vector<string>vecA;
8 vecA.push_back("A1");
9 vecA.push_back("A2");
10
11 vector<string>vecB;
12 vecB.push_back("B1");
13 vecB.push_back("B2");
14
15 // 方法:insert() 函数
16 vector<string>vecAB;
17 vecAB.insert(vecAB.end(), vecA.begin(), vecA.end()); //将vecA插入
18 vecAB.insert(vecAB.end(), vecB.begin(), vecB.end()); //将vecB插入
19 for (int i = 0; i < vecAB.size(); i++)
20 {
21 cout << "vecAB=" << vecAB[i] << endl;
22 }
23 cin.get();
24 return 0;
25
26 }


将一个Vector的所有元素复制到另一个中

 1  #include <iostream>
2 #include <string>
3 #include <vector>
4 using namespace std;
5 int main()
6 {
7 vector<string>vecA;
8 vecA.push_back("A1");
9 vecA.push_back("A2");
10
11 //方法1:声明
12 vector<string> vecB(vecA);
13 for (int i = 0; i < vecB.size(); i++)
14 {
15 cout << "vecB=" << vecB[i] << endl;
16 }
17 cout << endl;
18
19 //方法2:使用函数assign进行赋值
20 vector<string> vecC;
21 vecC.assign(vecA.begin(), vecA.end());
22 for (int i = 0; i < vecC.size(); i++)
23 {
24 cout << "vecC=" << vecC[i] << endl;
25 }
26 cout << endl;
27
28 // 方法3:insert() 函数
29 vector<string>vecD;
30 vecD.insert(vecD.end(), vecA.begin(), vecA.end()); //将vecA插入
31 for (int i = 0; i < vecD.size(); i++)
32 {
33 cout << "vecD=" << vecD[i] << endl;
34 }
35 cout << endl;
36
37 cin.get();
38 return 0;
39 }

将两个Vector的元素互换

 1 #include <iostream>
2 #include <string>
3 #include <vector>
4 using namespace std;
5 int main()
6 {
7 vector<string>vecA;
8 vecA.push_back("A1");
9 vecA.push_back("A2");
10
11 vector<string>vecB;
12 vecB.push_back("B1");
13 vecB.push_back("B2");
14
15 for (int i = 0; i < vecA.size(); i++)
16 {
17 cout << "vecA=" << vecA[i] << endl;
18 }
19 for (int i = 0; i < vecB.size(); i++)
20 {
21 cout << "vecB=" << vecB[i] << endl;
22 }
23 cout << endl;
24
25 //方法:使用swap进行交换
26 vecB.swap(vecA);//交换
27
28 for (int i = 0; i < vecA.size(); i++)
29 {
30 cout << "vecA=" << vecA[i] << endl;
31 }
32 for (int i = 0; i < vecB.size(); i++)
33 {
34 cout << "vecB=" << vecB[i] << endl;
35 }
36
37 cin.get();
38 return 0;
39 }


删除Vector元素

 1 #include <iostream>
2 #include <string>
3 #include <vector>
4 using namespace std;
5 int main()
6 {
7 vector<string>vecA;
8 vecA.push_back("A0");
9 vecA.push_back("A1");
10 vecA.push_back("A2");
11 vecA.push_back("A2");
12 vecA.push_back("A2");
13 vecA.push_back("A3");
14 vecA.push_back("A4");
15 vecA.push_back("A5");
16 vecA.push_back("A6");
17
18 for (int i = 0; i < vecA.size(); i++)
19 {
20 cout << "vecA=" << vecA[i] << endl;
21 }
22 cout << endl;
23
24 //删除最后一个元素
25 //vecA.pop_back();
26
27 //删除最后一个元素
28 //vecA.erase(vecA.end()-1);
29
30 //删除第2个元素
31 //vecA.erase(vecA.begin()+1);
32
33 //删除所有"A2"
34 for (vector<string>::iterator itor = vecA.begin(); itor != vecA.end(); /*++itor*/)
35 {
36 if (*itor == "A2")
37 {
38 itor = vecA.erase(itor);
39 }
40 else
41 {
42 ++itor;
43 }
44 }
45
46 for (int i = 0; i < vecA.size(); i++)
47 {
48 cout << "vecA=" << vecA[i] << endl;
49 }
50
51 cin.get();
52 return 0;
53 }


vector相关:

【C++】Vector判断元素是否存在,去重,求交集,求并集

【C++】Vector排序

【C++】Vector求最大值最小值

【C++】vector容器的用法的更多相关文章

  1. vector容器的用法

    转自一篇博客^-^: 1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<int> vec; (3)尾部插入数字:vec.p ...

  2. c++ vector容器基本用法

    基本用法 #include<iostream> #include<string> #include<vector> using namespace std; cla ...

  3. vector容器的用法以及动态数组

    vector容器不必去管大小 string申明的数组已经是动态的了 若是int类型的话,需要 cin>>N: int a[N]会出错 ,必须是int *p = new int[N] 然后再 ...

  4. (转载)C++STL中vector容器的用法

     vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说vec ...

  5. vector容器经常用法

    容器简单介绍 定义及初始化 末尾插入元素 遍历 size 函数是能够动态添加的 通过下标操作添加改变vector内容不是安全的操作 仅能对已存在元素进行下标操作不存在会crash 将元素一个容器复制给 ...

  6. Vector 容器简单介绍

    # Vector STL简要介绍 关于STL中的vector容器,以下做一些相关介绍. #### vector 简要概述 vector 称作向量类,属于容器类,实现了动态的数组,用于元素数量变化的对象 ...

  7. STL:vector容器用法详解

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

  8. vector容器用法详解

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

  9. STL之二:vector容器用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8507394 vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组. ...

随机推荐

  1. Maven关于web.xml中Servlet和Servlet映射的问题

    在配置Servlet时,有两个地方需要配置. 一个是<servlet>,另一个是<servlet-Mapping>,这两个一个是配置Servlet,一个是配置其映射信息. &l ...

  2. 『动善时』JMeter基础 — 12、JMeter取样器详解:sampler

    目录 1.取样器介绍 2.JMeter自带的取样器 3."HTTP请求"为例介绍一下取样器 (1)HTTP Request: (2)Web服务器: (3)HTTP请求: (4)同请 ...

  3. 【转】风控中的特征评价指标(二)——PSI

    转自:https://zhuanlan.zhihu.com/p/79682292 风控业务背景 在风控中,稳定性压倒一切.原因在于,一套风控模型正式上线运行后往往需要很久(通常一年以上)才会被替换下线 ...

  4. JS求一个字符串在另一个字符串中出现的次数

    参数说明: subString子字符串 originString母字符串 isIgnoreCap是否忽略大小写,默认忽略 function stringFre(subString, originStr ...

  5. Spring cloud 基础框架集成

    Spring cloud 基础框架集成 1. 注册中心 -eurekar 1. pom依赖 <?xml version="1.0" encoding="UTF-8& ...

  6. mybatis-plus批量插入saveBatch太慢?我愿意称rewriteBatchedStatements为神

    最近在做项目优化,代码优化之后,测试接口,好家伙.一个定时任务接口执行要10秒左右. 一点点追踪,给每个方法打上执行时间,一点点缩小范围.好家伙,终于让我锁定了目标. 这是mybatis-plus的批 ...

  7. vmware安装ubuntu ,一直处于end kernel panic - not syncing : corrupted stack end detected inside scheduler

    vmware安装ubuntu ,一直处于end kernel panic - not syncing : corrupted stack end detected inside scheduler y ...

  8. 在Vim中查看文件编码和文件编码转换

    在Vim中查看文件编码和文件编码转换 风亡小窝 关注  0.2 2016.09.26 22:43* 字数 244 阅读 5663评论 0喜欢 2 在Vim中查看文件编码 :set fileencodi ...

  9. JQuery 使用教程

    引言 JQuery 是一个 JavaScript 库,它极大地简化了 JavaScript 编程.JQuery 拥有丰富的选择器,可以非常方便的获取和操作 DOM 元素,而在 JQuery 中所有选择 ...

  10. C语言printf-(转自shiney)

    1.调用格式为  printf("<格式化字符串>", <参量表>);   其中格式化字符串包括两部分内容: 一部分是正常字符, 这些字符将按原样输出; 另 ...