一、关于vector的理解
① vector该是我们在初学C++的时候遇到的第一个关于容器的知识点。vector本质上
是一个类,作用和C中的数组类型类似。目前你可以先把容器理解为和数组一样的意思。
② vectoru与内置数据(即C数组)不同是的:其可以动态控制元素的长度。而内置数据不行。
③ 使用vector需要包含<vector>头文件。
二、vector的基本使用
定义对象、计算对象的长度、判断是否为空
定义一个长度为0的vector整形对象: vector <int> a;
定义以一个长度为10的vector整形对象: vector <int> a(10);
定义以一个长度为10的vector整形对象,并且初始化为2: vector <int> a(10,2);
和string类一样,成员函数size可计算长度
成员函数empty可判断是否为空
输出元素值
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> a(10);
if(!a.empty())
{
cout<<a is ok !<<endl;
}
int size = a.size();
for(int n=0; n<size; n++)
{
cout<<a[n]<<endl;
}
return 0;
}
输出结果:默认初始化为0
a is ok !
0
0
0
0
0
0
0
0
0
0
动态添加删除vector对象的元素
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> a(10);
if(!a.empty())
{
cout<<a is ok !<<endl;
}
cout<<"befor add a size is "<<a.size()<<endl;
a.push_back(10);//往a中添加数据为10的元素
cout<<"after add a size is "<<a.size()<<endl;
/***************************************************************/
cout<<"befor del a size is "<<a.size()<<endl;
a.pop_back();//往a中删除最后的元素
cout<<"after del a size is "<<a.size()<<endl;
return 0;
}
运行结果:
a is ok !
befor add a size is 10
after add a size is 11
befor del a size is 11
after del a size is 10
三、将普通内置数组中的元素值拷贝到vector类对象中
① 单纯循环赋值法
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int array[10] = {10,9,8,7,6,5,4,3,2,1};//定义一个长度为10的普通内置数组
vector <int> a(10);
if(!a.empty())
{
cout<<a is ok !<<endl;
}
//单纯的循环赋值
int size = a.size();
for(int n=0; n<size; n++)
a[n] = array[n];
for(int n=0; n<size; n++)
cout<<a[n]<<endl;
return 0;
}
输出结果:
a is ok !
10
9
8
7
6
5
4
3
2
1
② 特殊写法
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int array[10] = {10,9,8,7,6,5,4,3,2,1};//定义一个长度为10的普通内置数组
vector <int> a(array,array+10);//也可写成 vector <int> a(array,&arra[10]);
if(!a.empty())
{
cout<<a is ok !<<endl;
}
int size = a.size();
for(int n=0; n<size; n++)
cout<<a[n]<<endl;
return 0;
}
输出结果:
a is ok !
10
9
8
7
6
5
4
3
2
1
四、顺序容器对象的操作
① 头部添加元素
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
vector <string> s_vect(1,"李大蕉");//定义一个string类型的vector对象
list <string> s_list(1,"李大蕉");//定义一个string类型的list对象
vector <string> :: iterator s_p = s_vect.begin();
list <string> :: iterator l_p = s_list.begin();
cout<<"头插之前:"<<*s_p<< *l_p <<endl;
string spouse("周树苗");
s_vect.insert(s_vect.begin(),spouse);
s_list.insert(s_list.begin(),spouse);
s_p = s_vect.begin();
l_p = s_list.begin();
cout<<"头插之后:"<<*s_p<< *l_p <<endl;
return 0;
}
运行结果:
头插之前:李大蕉李大蕉
头插之后:周树苗周树苗
**
② 赋值与对换
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector <string> vs_1;
vector <string> vs_2(10,"Dante");
vector<string>::iterator vp_1 = vs_1.begin();
vector<string>::iterator vp_2 = vs_2.begin();
cout<<"交换之前:"<<endl;
cout<<"vs_1:";
for(; vp_1 < vs_1.end(); vp_1++)
{
cout<< *vp_1<<" ";
}
cout<<endl;
cout<<"vs_2:";
for(; vp_2 < vs_2.end(); vp_2++)
{
cout<< *vp_2<<" ";
}
cout<<endl;
vs_1.swap(vs_2);//ba把vs_2拷贝给vs_1
cout<<"交换之后:"<<endl;
vp_1 = vs_1.begin();
vp_2 = vs_2.begin();
cout<<"vs_1:";
for(; vp_1 < vs_1.end(); vp_1++)
{
cout<< *vp_1 << " ";
}
cout<<endl;
cout<<"vs_2:";
for(; vp_2 < vs_2.end(); vp_2++)
{
cout<< *vp_2<<" ";
}
cout<<endl;
return 0;
}
运行结果:
交换之前:
vs_1:
vs_2:Dante Dante Dante Dante Dante Dante Dante Dante Dante Dante
交换之后:
vs_1:Dante Dante Dante Dante Dante Dante Dante Dante Dante Dante
vs_2:
2)赋值
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector <string> vs_1;
vector <string> vs_2(10,"Dante");
cout<<"赋值之前:";
vector <string>::iterator v_p = vs_1.begin();
for(; v_p<vs_1.end(); v_p++)
{
cout<<*v_p<<" ";
}
cout<<endl;
vs_1 = vs_2;//进行赋值
cout<<"赋值之后:";
v_p = vs_1.begin();
for(; v_p<vs_1.end(); v_p++)
{
cout<<*v_p<<" ";
}
cout<<endl;
return 0;
}
运行结果:
赋值之前:
赋值之后:Dante Dante Dante Dante Dante Dante Dante Dante Dante Dante