vector类型是一个标准库种的类型,代表一个容器,集合或者动态数组这样的一种概念。可以把若干个对象放到这个容器里面。
要想使用这种类型 需要在.cpp 源文件开头包含 vector 头文件:
#include <vector>
另外为了方便引用这种类型,也需要添加:
using namespace std;
下面定义几种对象容器是可以的:
(1)定义一个整型类型的容器对象。
vector <int> vIntData;
上面的代码定义了-个vector类型的对象,名字叫作vIntData,这个对象里而保存的就是 int 型数据。
(2)定义一个结构体类型的容器对象。
struct STUDENT
{
int num;
};
vector <STUDENT> vStudent;
(3)定义一个string类型的容器对象。
vector <string> vString;
(4)定义一个int指针类型的容器对象。
vector <int *> vString;
(1)空vector。
定义如下:
vector <string> mystr; //创建一个string类型的空vector对象(容器)。现在mystr里面不包含任何元素。
这样定义了一个空容器后,就可以往里面增加数据了。可以使用vector的成员函数push_back往容器末尾增加数据,看看一下例子:
- vector <string> mystr;
-
- mystr.push_back("123"); //往容器里面添加”123”字符串
- mystr.push_back("456"); //往容器里面添加”456”字符串
-
- for (auto &i : mystr) //打印mystr里面的元素
- {
- cout << i << endl;
- }

(2))vector 对象元素类型栩同的情况下,迸行vector 对象元素复制(新副本)。
- vector <string> mystr1;
-
- mystr1.push_back("Helloworld!");
- vector <string> mystr2(mystr1); //把mystr1元素赋值给mystr2
- vector <string> mystr3 = mystr1; //把mystr1元素赋值给mystr3
-
- for (auto &i : mystr2) //打印mystr2容器里面的值
- {
- cout << i << endl;
- }
-
- for (auto &j : mystr3)//打印mystr3容器里面的值
- {
- cout << j << endl;
- }

(3)在C++11中,还可以用初始化列表方法给初值,这个时候用”{ }”括起来。
- vector <string> mystr = { "123","456","789","helloworld!","I love China!"};
- for (auto &i : mystr)
- {
- cout << i << endl;
- }

(4)创建指定数量的元素。请注意,有元素数量概念的初始化,用的都是“( )”。
- vector <int> myInt(10, 50); //10个元素,下标[0]~[19],每一个元素值都是50
- vector <string> mystr2(10, "Hello"); //10个元素,下标[0]~[19],每一个元素值都是"Hello"
-
- for (auto &i : myInt)//遍历mystr1容器中的元素
- {
- cout << i <<" ";
- }
- cout << endl;
-
- for (auto& j : mystr2)//遍历mystr2容器中的元素
- {
- cout << j << " ";
- }
- cout << endl;

如果不给元素初值,那么元素的初值要根据元素类型确定,例如元素类型为int,系统给的初值就是0,元素类型为string,系统给的初值是"",但是也存在有些类型,必须给初值,否则就会报错。
- vector <int> myInt(10); //10个元素,下标[0]~[19],每一个元素值都是0
- vector <string> mystr2(10); //10个元素,下标[0]~[19],每一个元素值都是""
-
- for (auto &i : myInt)//遍历myInt容器中的元素
- {
- cout << i <<" ";
- }
- cout << endl;
-
- for (auto& j : mystr2)//遍历mystr2容器中的元素
- {
- cout << j << " ";
- }
- cout << endl;

(5)多种初始化。“( )”一般表示对象中的元素数量这种概念,“{ }”一般表示元素的内容这种概念,但又不是绝对。
可以看一下下面的例子:
- vector <int> myInt1(10); //表示元素数量,每个元素的值都是0
- vector <int> myInt2{ 10 }; //表示myInt容器里面只有一个元素,元素值为10
- vector <string> mystr1{"Hello"}; //1个元素,内容是Hello
- vector <string> mystr2{10}; //10个元素,每个元素都为"",因为10是一个整数,不能作为string对象的内容,所以系统把它处理成了元素的数量,不提倡这样写
- vector <string> mystr3{10,"Hello"};//10个元素,每个元素内容都是"Hello"
- vector <string> mystr4(10, "Hello");//10个元素,下标[0]~[19],每一个元素值都是"Hello"
- vector <int> myInt3(10,1);//先数量后元素内容:10个元素,每个元素的值都为1
- vector <int> myInt4{10,1};//两个元素,第一个元素值为10,第二个元素值为1,等同于初始化列表
- vector <int> myInt5{"Hello"};//系统直接报错
经过上面的初始化例子可以看到:想要正常通过”{ }”进行初始化,那么”{ }”里面的值类型得跟vector后面的”< >”里面的元素类型相同,否则“<>”里面提供的值就无法作为元素初始值,如mystr2、mystr3和myInt5。
(1)判断是否为空empty(),返回布尔值。
- vector <int> myInt1;
- if(myInt1.empty())
- {
- cout<<"myInt1为空"endl;
- }
(2)push_back:一个非常常用的方法,用于向vector末尾增加一个元素。
- vector <int> myInt1;
-
- myInt1.push_back(100);
- myInt1.push_back(200);
-
- for (int i = 300; i <= 900; i=i+100)
- {
- myInt1.push_back(i);
- }
-
- for (auto &i:myInt1)
- {
- cout << i << " ";
- }
- cout << endl;

(3)size:返回元素个数。
cout << myInt1.size() << endl; //10
(4)clear:移除所有元素,将容器清空。
myInt1.clear();
cout << myInt1.size() << endl; //0
(5)myInt1[n]:返回myInt1中的第n个元素,n下标从0开始。位置值n也必须小于.size(),如果下标引用超过这个范围,或许用下标访问一个空的vector,都会产生不可预测的结果。
- vector <int> myInt1;
-
- myInt1.push_back(100);
- myInt1.push_back(200);
-
- myInt1.clear();
-
- myInt1.push_back(300);
- myInt1.push_back(400);
- cout << myInt1[1] << endl; //400
(6)赋值运算符(=)。
- vector <int> myInt1;
-
- myInt1.push_back(100);
- myInt1.push_back(200);
- for (int i = 300; i <= 900; i=i+100)
- {
- myInt1.push_back(i);
- }
-
- vector <int> myInt2;
- myInt2.push_back(1000);
- myInt2.push_back(2000);
-
- myInt2 = myInt1;//myInt2 也得到10个元素,用myInt1的内容取代了myInt2 中原有的内容,myInt2中的1000和2000就被冲掉了。
- myInt2 = {1,2,3,4,5,6};
- cout << myInt2.size() << endl;
(7)相等和不等(== 和!=)。
- vector <int> myInt1;
- vector <int> myInt2;
-
- myInt1.push_back(100);
- myInt1.push_back(200);
-
- for (int i = 300; i <= 900; i = i + 100)
- {
- myInt1.push_back(i);
- }
-
- myInt2 = myInt1;
- if (myInt2 == myInt1)//条件成立
- {
- cout << "myInt2 == myInt1" << endl;
- }
-
- myInt2.push_back(1234);
- if (myInt2 != myInt1)//条件成立
- {
- cout << "myInt2 != myInt1" << endl;
- }
(8)范围for的应用:和讲解string时对范围for的应用相似。
- vector <int> myInt1;
- for (int i = 1; i <= 9; i++)
- {
- myInt1.push_back(i);
- }
-
- for (auto &j : myInt1)
- {
- cout << j <<" ";
- }
-
- cout << endl;

到此,vector类型的简介和操作说明已完成。
2022.06.26结。