一、简介
vector是C++标准模板库,是一个容器,底层是数组,为连续内存,所属头文件为<vector>。vector存储数据时,会分配一个存储空间,如果继续存储,该分配的空间已满,就会分配一块更大的内存,把原来的数据复制过来。
二、操作
1. 初始化
vector<int> a(10); //定义了10个整型元素的向量(尖括号中为元素类型名,它可以是任何合法的数据类型),但没有给出初值,其值是不确定的。
vector<int> a(10,1); //定义了10个整型元素的向量,且给出每个元素的初值为1
vector<int> a(b); //用b向量来创建a向量,整体复制性赋值
vector<int> a(b.begin(),b.begin+3); //定义了a值为b中第0个到第2个(共3个)元素
int b[7]={1,2,3,4,5,9,8}; vector<int> a(b,b+7); //从数组中获得初值
2. 容量大小
vector.size():vector大小 //a.size(); //返回a中元素的个数
vector.capacity():vector所占内存实际大小 //a.capacity(); //返回a在内存中总共可以容纳的元素个数
3. 添加删除
尾部添加元素:vector.push_back() //a.push_back(5); 在a的最后一个向量后插入一个元素,其值为5
尾部删除元素:vector.pop_back() //a.pop_back(); 删除a向量的最后一个元素
交换两个vector元素:vector.swap() //a.swap(b); b为向量,将a中的元素和b中的元素进行整体性交换
清空vector元素:vector.clear() //a.clear(); 清空a中的元素
删除指定元素:vector.erase(it) //a.erase(a.begin()+1,a.begin()+3); 删除a中第1个(从第0个算起)到第2个元素,也就是说删除的元素从a.begin()+1算起(包括它)一直到a.begin()+ 3(不包括它)
4. 访问
下标访问:vector[1] //不检查是否越界
at方法访问:vector.at(1) //自动检查是否越界,如越界会抛出异常
访问第一个元素:vector.front()
访问最后一个元素:vector.back()
三、存储
//存储方式1
vector<int> v1(10);
for (int i=0; i<10; i++)
{
v1[i] = i;
}
//存储方式2
vector<int> v2;
for (int i=0; i<10; i++)
{
v2.push_back(i);
}
struct Student
{
char name[32];
int age;
};
//存储结构体
vector<Student> vStu1;
for (int i=0; i<10; i++)
{
Student stu;
strcpy(stu.name, "woniu201");
stu.age = 30 + i;
vStu1.push_back(stu);
}
//存储结构体指针
vector<Student*> vStu2;
for (int i=0; i<10; i++)
{
Student* pStu = (Student*)malloc(sizeof(Student));
strcpy(pStu->name, "woniu201");
pStu->age = 30 + i;
vStu2.push_back(pStu);
}
四、遍历
vector<int> v;
for (int i=0; i<100; i++)
{
v.push_back(i);
}
//遍历方式1
for (int i=0; i<100; i++)
{
printf("%d ", v[i]);
}
//遍历方式2
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
int& a = *it;
printf("%d ", a);//等价于printf("%d ", *it)
}
五、排序
1. 普通排序
#include "stdlib.h"
#include <vector>
#include <algorithm>
using namespace std;
//升序比较函数
int compare1(const int &a, const int &b)
{
return a < b;
}
//降序比较函数
int compare2(const int &a, const int &b)
{
return a > b;
}
int main()
{
vector<int> v;
for (int i=0; i<10; i++)
{
v.push_back(rand() % 10);
}
//遍历输出
printf("排序前数据:");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
}
//升序排序
sort(v.begin(), v.end(), compare1);
//遍历输出
printf("\n升序后数据:");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
}
//降序排序
sort(v.begin(), v.end(), compare2);
//遍历输出
printf("\n降序后数据:");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
}
getchar();
return 1;
}
2. 对类成员变量排序
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
Student(string n, int c) :name(n), core(c) {}
string name;
int core;
};
//升序比较函数
bool compare1(const Student& s1, const Student& s2)
{
return s1.core < s2.core;
}
//降序比较函数
bool compare2(const Student& s1, const Student& s2)
{
return s1.core > s2.core;
}
int main()
{
vector<Student> v;
Student s1("aaaa", 97);
Student s2("bbbb", 99);
Student s3("cccc", 95);
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
//升序排序
sort(v.begin(), v.end(), compare1);
printf("\n升序后的数据:\n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
}
//降序排序
sort(v.begin(), v.end(), compare2);
printf("\n降序后的数据:\n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
}
getchar();
return 1;
}
六、查找
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if(it != v.end())
{
cout << "found";
}
else
{
cout << "not found";
}
七、删除
for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
{
if(*it == 8)
{
it = v.erase(it);//it会++一次
it--; //删除完后需要--,否则最终循环越界
}
}
八、释放内存
.clear();清空数据;.swap();函数交换
vector输入完一组数据处理完后,调用clear();然后输入下一组数据,但是如果在输入之前,输出之前vector所存的内容,会发现仍然存在,但是如果输出.empty();它会返回1,告诉我们这个容器现在是空的。
为什么呢?这是因为使用.clear()清空内容,但是没有释放内存的原因。
那么如何释放内存呢?我们用swap交换到一个新的类型的vector,将原来的a拷贝出去,然后自然销毁,而新的到的a是全新的没有存任何数据的。 【a.swap(b); //b为向量,将a中的元素和b中的元素进行整体性交换;也可以a.swap()】
vector<int> ().swap(a);
//a.swap();
cout<<a.size()<<endl;//输出 0
cout<<a.capacity()<<endl;.// 输出 0
1. 存放为普通数据的释放
//存放整型
vector<int> v;
for (int i=0; i<100; i++)
{
v.push_back(i);
}
//释放内存
vector<int> (v).swap(v);
2. 存放为结构体数据的释放
//存储结构体
vector<Student> vStu1;
for (int i=0; i<10; i++)
{
Student stu;
strcpy(stu.name, "wangpengfei");
stu.age = 30 + i;
vStu1.push_back(stu);
}
//释放内存
vector<Student> (vStu1).swap(vStu1);
3. 存放为指针数据的释放
//存储结构体指针
vector<Student*> vStu2;
for (int i=0; i<10; i++)
{
Student* pStu = (Student*)malloc(sizeof(Student));
strcpy(pStu->name, "wangpengfei");
pStu->age = 30 + i;
vStu2.push_back(pStu);
}
//释放内存
for (vector<Student*>::iterator it = vStu2.begin(); it != vStu2.end(); it++)
{
if (NULL != *it)
{
delete *it;
*it = NULL;
}
}
释放内存的一些说明:
vector本身并不释放任何内存,如果你想同时做到清空vector的元素和释放vector的容量, 你可以使用swap技巧。 这样做会创建一个临时的空vector, 它将替换希望清空的vector。“vector 的 clear 不影响 capacity , 你应该 swap 一个空的 vector。”:vector<type>(v).swap(v); //对于string则可能像下面这样 string(s).swap(s); 即先创建一个临时拷贝与原先的vector一致,值得注意的是,此时的拷贝 其容量是尽可能小的符合所需数据的。紧接着将该拷贝与原先的vector v进行 交换。好了此时,执行交换后,临时变量会被销毁,内存得到释放。此时的v即为原先 的临时拷贝,而交换后的临时拷贝则为容量非常大的vector(不过已经被销毁)
#include <iostream>
#include <vector>
using namespace std;
vector <string> v;
char ch;
int main()
{
for (int i = 0; i<1000000; i++)
v.push_back("hello vector");
cin >> ch;
// 此时检查内存情况占用54M
v.clear();
cin >> ch;
// 此时再次检查,仍然占用54M
cout << "Vector的容量为" << v.capacity() << endl;
// 此时容量为1048576
vector<string>(v).swap(v);
cout << "Vector 的 容量为" << v.capacity() << endl;
// 此时容量为0
cin >> ch;
// 检查内存,释放了10M+即为数据内存
return 0;
}
九、误区和算法说明
1. 误区
vector<int> a;
for(int i=0;i<10;i++)
a[i]=i;
//这种做法以及类似的做法都是错误的,因为下标只能用于获取已存在的元素,而现在的a[i]还是空的对象
2. 算法
几种重要的算法,使用时需要包含头文件:
#include<algorithm>
//对a中的从a.begin()到a.end()的元素进行从小到大排列
sort(a.begin(),a.end());
//对a中的从a.begin()到a.end()的元素倒置,但不排列,如a中元素为1,3,2,4,倒置后为4,2,3,1
reverse(a.begin(),a.end());
//把a中的从a.begin()到a.end()的元素复制到b中,从b.begin()+1的位置(包括它)开始复制,覆盖掉原有元素
copy(a.begin(),a.end(),b.begin()+1);
//在a中的从a.begin()到a.end()的元素中查找10,若存在返回其在向量中的位置
find(a.begin(),a.end(),10);