C++中的矢量——【vector】
1.初遇矢量
- c++标准模板库(Standard Template Library,STL)包含一个矢量的数据类型,在STL中定义的数据类型,通常称之为容器,STL中z有两个容器:顺序容器和关联容器。
- vector数据类型是一个序列容器,他很像一个一维数组;其表现如下:
- 矢量包含一系列值和元素;
- 矢量将元素储存在连续的内存中;
- 可以使用数组下标运算符[]访问矢量中的各个元素;
但是,矢量有相比于数组的几个优点:
- 不必声明矢量将具有的元素的数量;
- 可以向已满的矢量添加值,矢量将自动增加其大小以适应新值;
- 矢量可以报告他们包含元素的数量;
2.定义和初始化矢量
要使用矢量必须包含下列头文件:
#include<vector>
下面是创一些矢量的常例:
vector<int> num //创建一个整型的名为num的矢量
vector<int> num(10) //创建一个整型的名为num的矢量其中有十个整数的矢量
vector<int> num(10,2) /*创建一个整型的名为num的矢量其中有十个整数的矢量,并且十个整数都被初始化为2*/
vector<int> set1(set2) //创建了一个set1的矢量,其是矢量set2的精确副本
初始化:
//第一种源代码
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> num(5);
for(int i=0;i<5;i++)
cin>>num[i];
for(int i=0;i<5;i++)
cout<<num[i]<<" ";
return 0;
}
//运行结果:
/*
1 2 3 4 5
1
2
3
4
5
Process returned 0 (0x0) execution time : 3.516 s
Press any key to continue.
*/
//第二种源代码:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> num (5);
for(int i=0;i<5;i++)
cin>>num[i];
//向数组中插入数字
num.push_back(6);//在向量尾部插入6
//遍历打印
for(int i=0;i<num.size();i++) //.size可以测矢量的长度
cout<<num[i]<<endl;
return 0;
}
/* 运行结果:
1 2 3 4 5
1
2
3
4
5
6
Process returned 0 (0x0) execution time : 5.465 s
Press any key to continue.
*/
// 第三种:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> num (5);
for(int i=0;i<5;i++)
cin>>num[i];
//向数组中插入数字
num.push_back(6);//在向量尾部插入6
//遍历打印
//使用迭代器 iterator
//以下是迭代器的基本用法,高能慎用!!!!
vector<int>::iterator it; //得到迭代对象,实际上是一个指针
//it.begin
//从第一个元素迭代
for(it=num.begin();it!=num.end();++it) //如果使用it++,则会有缓存现象,导致效率不高
{
cout<<* it<<endl;
}
return 0;
}
// 运行结果:
/*
2 5 7 9 3
2
5
7
9
3
6
Process returned 0 (0x0) execution time : 4.587 s
Press any key to continue.
*/
//下面演示一下使用快排
#include<iostream>
#include<vector>
#include<algorithm> //算法
using namespace std;
int main()
{
vector<int> num (5);
for(int i=0;i<5;i++)
cin>>num[i];
//向数组中插入数字
num.push_back(6);//在向量尾部插入6
//遍历打印
//使用迭代器 iterator
//以下是迭代器的基本用法,高能慎用!!!!
vector<int>::iterator it; //得到迭代对象,实际上是一个指针
//it.begin
//从第一个元素迭代
for(it=num.begin();it!=num.end();++it) //如果使用it++,则会有缓存现象,导致效率不高
{
cout<<* it<<endl;
}
sort(num.begin(),num.end());
cout<<endl<<endl;
for(it=num.begin();it!=num.end();++it) //如果使用it++,则会有缓存现象,导致效率不高
{
cout<<* it<<endl;
}
return 0;
}
//输出结果:
/*
1 7 9 3 4
1
7
9
3
4
6
1
3
4
6
7
9
Process returned 0 (0x0) execution time : 7.554 s
Press any key to continue.
*/
// 逆序
#include<iostream>
#include<vector>
#include<algorithm> //算法
using namespace std;
int main()
{
vector<int> num (5);
for(int i=0;i<5;i++)
cin>>num[i];
//向数组中插入数字
num.push_back(6);//在向量尾部插入6
//遍历打印
//使用迭代器 iterator
//以下是迭代器的基本用法,高能慎用!!!!
vector<int>::iterator it; //得到迭代对象,实际上是一个指针
//it.begin
//从第一个元素迭代
for(it=num.begin();it!=num.end();++it) //如果使用it++,则会有缓存现象,导致效率不高
{
cout<<* it<<endl;
}
reverse(num.begin(),num.end()); //逆序排序
cout<<endl<<endl;
for(it=num.begin();it!=num.end();++it) //如果使用it++,则会有缓存现象,导致效率不高
{
cout<<* it<<endl;
}
return 0;
}
//输出结果:
1 2 3 4 5
1
2
3
4
5
6
6
5
4
3
2
1
Process returned 0 (0x0) execution time : 8.155 s
Press any key to continue.
*/
3.矢量中的常见函数
clear() | 清除容器中所有数据 |
empty() | 判断容器是否为空 |
size() | 返回容器元素的个数 |
front() | 返回第一个元素 |
insert(pos,num) | 在POS位置插入一个数字 |
pop_back() | 在容器删除最后一个元素 |
push_back() | 在容器末尾加入一个元素 |
resize(num) | 重新设置容器大小 |
begin(),end() | 返回容器首尾元素的迭代器 |
4.从矢量中删除元素
用法示例:
#include<iostream>
#include<vector>
#include<algorithm> //算法
using namespace std;
int main()
{
vector<int> num (5);
for(int i=0;i<5;i++)
cin>>num[i];
num.pop_back();
//遍历打印
//使用迭代器 iterator
//以下是迭代器的基本用法,高能慎用!!!!
vector<int>::iterator it; //得到迭代对象,实际上是一个指针
//it.begin
//从第一个元素迭代
for(it=num.begin();it!=num.end();++it) //如果使用it++,则会有缓存现象,导致效率不高
{
cout<<* it<<endl;
}
return 0;
}
//输出结果:
1 3 5 6 7
1
3
5
6
Process returned 0 (0x0) execution time : 4.096 s
Press any key to continue.
5.清理矢量
用法示例:
#include<iostream>
#include<vector>
#include<algorithm> //算法
using namespace std;
int main()
{
vector<int> num (5);
for(int i=0;i<5;i++)
cin>>num[i];
//遍历打印
//使用迭代器 iterator
//以下是迭代器的基本用法,高能慎用!!!!
vector<int>::iterator it; //得到迭代对象,实际上是一个指针
//it.begin
//从第一个元素迭代
for(it=num.begin();it!=num.end();++it) //如果使用it++,则会有缓存现象,导致效率不高
{
cout<<* it<<endl;
}
num.clear();
cout<<endl<<endl;
for(it=num.begin();it!=num.end();++it) //如果使用it++,则会有缓存现象,导致效率不高
{
cout<<* it<<endl;
}
return 0;
}
// 输出结果
1 5 6 7 8
1
5
6
7
8
Process returned 0 (0x0) execution time : 4.650 s
Press any key to continue.
6.检测一个空矢量
使用empty成员函数,如果矢量为空则返回true,反之返回false;
用法示例:
#include<iostream>
#include<vector>
#include<algorithm> //算法
using namespace std;
int main()
{
vector<int> num (5);
for(int i=0;i<5;i++)
cin>>num[i];
//遍历打印
//使用迭代器 iterator
//以下是迭代器的基本用法,高能慎用!!!!
vector<int>::iterator it; //得到迭代对象,实际上是一个指针
//it.begin
//从第一个元素迭代
for(it=num.begin();it!=num.end();++it) //如果使用it++,则会有缓存现象,导致效率不高
{
cout<<* it<<endl;
}
num.clear();
if(num.empty())
cout<<"num is empty!"<<endl;
else cout<<"num is not empty!"<<endl;
return 0;
}
//输出结果
1 2 8 6 4
1
2
8
6
4
num is empty!
Process returned 0 (0x0) execution time : 3.570 s
Press any key to continue.
好了,小飞就总结到这里了。如果喜欢就点个赞吧!