3.2 vector容器
3.2.1 vector基本概念
功能:
- vector数据结构和数组非常相似,也称为单端数组
vector与普通数组区别:
- 不同之处在于数组是静态空间,而vector可以动态扩展
动态扩展:
- 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间
- vector容器的迭代器是支持随机访问的迭代器
3.2.2 vector构造函数
功能描述:
- 创建vector容器
函数原型:
vector<T> v;
//采用模板实现类实现,默认构造函数vector(v.begin(), v.end());
//将v [begin(), end()) 区间中的元素拷贝给本身。vector(n, elem);
//构造函数将n个elem拷贝给本身。vector(const vector &vec);
//拷贝构造函数。
示例:
#include<iostream>
using namespace std;
#include<vector>
//vector容器构造
void myprintf(vector<int>v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
vector<int>v1;//默认构造无参构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
myprintf(v1);
//通过区间构造方式
vector<int>v2(v1.begin(), v1.end());
myprintf(v2);
//n个elem方式构造
vector<int>v3(10, 100);
myprintf(v3);
//拷贝构造
vector<int>v4(v3);
myprintf(v4);
}
int main()
{
test1();
system("pause");
return 0;
}
**总结:**vector的多种构造方式没有可比性,灵活使用即可
3.2.3 vector赋值操作
功能描述:
- 给vector容器进行赋值
函数原型:
-
vector& operator=(const vector &vec);
//重载等号操作符 -
assign(beg, end);
//将[beg, end)区间中的数据拷贝赋值给本身。 -
assign(n, elem);
//将n个elem拷贝赋值给本身。
示例:
#include<iostream>
using namespace std;
#include<vector>
//vector 赋值
void myprintf(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
myprintf(v1);
//赋值 //operator=
vector<int>v2;
v2 = v1;
myprintf(v2);
//assign
vector<int>v3;
v3.assign(v1.begin(), v1.end());
myprintf(v3);
//n个elem方式赋值
vector<int>v4;
v4.assign(10, 100);
myprintf(v4);
}
int main()
{
test1();
system("pause");
return 0;
}
总结: vector赋值方式比较简单,使用operator=,或者assign都可以
3.2.4 vector容量和大小
功能描述:
- 对vector容器的容量和大小操作
函数原型:
-
empty();
//判断容器是否为空 -
capacity();
//容器的容量 -
size();
//返回容器中元素的个数 -
resize(int num);
//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。 //如果容器变短,则末尾超出容器长度的元素被删除。
-
resize(int num, elem);
//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。 //如果容器变短,则末尾超出容器长度的元素被删除
示例:
#include<iostream>
using namespace std;
#include<vector>
//vector 的容量与大小
void myprintf(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
myprintf(v1);
//判断是否为空
if (v1.empty())//为真 代表容器为空
{
cout << "empty" << endl;
}
else
{
cout << "not empty" << endl;
cout << "v1's capacity= " << v1.capacity() << endl;
cout << "v1's size = " << v1.size() << endl;
}
//重新指定大小
v1.resize(15);
myprintf(v1);//如果重新指定比原来的长了,默认用0来填充新的位置
v1.resize(5);//变短删除
myprintf(v1);
v1.resize(15, 3);//可以自定义填充的数
myprintf(v1);
}
int main()
{
test1();
system("pause");
return 0;
}
总结:
- 判断是否为空 — empty
- 返回元素个数 — size
- 返回容器容量 — capacity
- 重新指定大小 — resize
3.2.5 vector插入和删除
功能描述:
- 对vector容器进行插入、删除操作
函数原型:
push_back(ele);
//尾部插入元素elepop_back();
//删除最后一个元素insert(const_iterator pos, ele);
//迭代器指向位置pos插入元素eleinsert(const_iterator pos, int count,ele);
//迭代器指向位置pos插入count个元素eleerase(const_iterator pos);
//删除迭代器指向的元素erase(const_iterator start, const_iterator end);
//删除迭代器从start到end之间的元素clear();
//删除容器中所有元素
示例:
#include<iostream>
using namespace std;
#include<vector>
//vector 的插入与删除
/*
push_back(ele);` //尾部插入元素ele
pop_back();` //删除最后一个元素
insert(const_iterator pos, ele);` //迭代器指向位置pos插入元素ele
insert(const_iterator pos, int count,ele);` //迭代器指向位置pos插入count个元素ele
erase(const_iterator pos);` //删除迭代器指向的元素
erase(const_iterator start, const_iterator end);` //删除迭代器从start到end之间的元素
clear();` //删除容器中所有元素
*/
void myprintf(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
vector<int>v1;
//尾插
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//遍历
myprintf(v1);
//尾删
v1.pop_back();
myprintf(v1);
//插入 //第一个参数必须是迭代器
v1.insert(v1.begin(), 100);
myprintf(v1);
v1.insert(v1.begin()+1, 2, 1000);//可以在v1.begin()后面加减偏移量,就在第几个位置插入
myprintf(v1);
//删除 //参数为迭代器
v1.erase(v1.begin());
myprintf(v1);
v1.erase(v1.begin(), v1.end()-1);//可以在后面加减偏移量
myprintf(v1);
//清空
v1.clear();
myprintf(v1);
}
int main()
{
test1();
system("pause");
return 0;
}
总结:
- 尾插 — push_back
- 尾删 — pop_back
- 插入 — insert (位置迭代器)
- 删除 — erase (位置迭代器)
- 清空 — clear
3.2.6 vector数据存取
功能描述:
- 对vector中的数据的存取操作
函数原型:
at(int idx);
//返回索引idx所指的数据operator[];
//返回索引idx所指的数据front();
//返回容器中第一个数据元素back();
//返回容器中最后一个数据元素
示例:
#include<iostream>
using namespace std;
#include<vector>
//vector数据存取
void test1()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
//利用中括号来访问数组元素
for (int j = 0; j < v1.size(); j++)
{
cout << v1[j] << " ";
}
cout << endl;
//利用at来访问
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
//获取第一个元素
cout << v1.front() << endl;
//获取最后一个元素
cout << v1.back() << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
总结:
- 除了用迭代器获取vector容器中元素,[ ]和at也可以
- front返回容器第一个元素
- back返回容器最后一个元素
3.2.7 vector互换容器
功能描述:
- 实现两个容器内元素进行互换
函数原型:
swap(vec);
// 将vec与本身的元素互换
示例:
#include<iostream>
using namespace std;
#include<vector>
//vector容器
void printvector(vector<int> v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//1.基本使用
void test1()
{
cout << "交换前" << endl;
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printvector(v1);
vector<int>v2;
for (int i = 10; i > 0; i--)
{
v2.push_back(i);
}
printvector(v2);
cout << "交换后" << endl;
v1.swap(v2);
printvector(v1);
printvector(v2);
}
//2.实际用途
//巧用swap可以收缩内存空间
void test2()
{
vector<int>v;
for (int i = 0; i < 10000; i++)
{
v.push_back(i);
}
cout << "v的容量为 :" << v.capacity() << endl;
cout << "v的大小 :" << v.size() << endl;
v.resize(5);
cout << "v的容量为 :" << v.capacity() << endl;
cout << "v的大小 :" << v.size() << endl;
vector<int>(v).swap(v);//vector<int>(v)匿名对象,容量大小都为5,执行完直接释放
cout << "v的容量为 :" << v.capacity() << endl;
cout << "v的大小 :" << v.size() << endl;
}
int main()
{
test1();
test2();
system("pause");
return 0;
}
总结:swap可以使两个容器互换,可以达到实用的收缩内存效果
3.2.8 vector预留空间
功能描述:
- 减少vector在动态扩展容量时的扩展次数
函数原型:
reserve(int len);
//容器预留len个元素长度,预留位置不初始化,元素不可访问。
示例:
#include<iostream>
using namespace std;
#include<vector>
//vector容器 预留空间
void test1()
{
vector<int>v1;
//利用reserve预留空间
v1.reserve(100000);
int num = 0;
int* p = NULL;
for (int i = 0; i < 100000; i++)
{
v1.push_back(i);
if (p != &v1[0])
{
p = &v1[0];
num++;
}
}
cout << "num= " << num << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
总结:如果数据量较大,可以一开始利用reserve预留空间