vector类
vector类简介
1、vector表示变长数组的序列容器
2、vector类采用连续空间存储,大小可变,可以对元素像数组一样下标访问
3、vector使用动态分配数组来存储它的元素
4、vector在访问元素的时候更加高效,尾插尾删的效率高效。
1、vector类的构造
(constructor)构造函数声明 | 接口说明 |
---|---|
vector() | 无参构造 |
vector(size_type n, const value_type& val = value_type()) | 构造并初始化n个val |
vector (const vector& x); | 拷贝构造 |
vector (InputIterator first, InputIterator last); | 使用迭代器进行初始化构造 |
#include <iostream>
#include <vector>
using namespace std;
void TestVector()
{
vector<int> first;
vector<int> second(4, 100);
vector<int> third(second.begin(), second.end());
vector<int> fourth(third);
int array[] = { 9, 5, 2, 7 };
vector<int> fifth(array, array + sizeof(array) / sizeof(array[0]));
for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
TestVector();
system("pause");
return 0;
}
2、vector类的访问及遍历
iterator的使用 | 接口说明 |
---|---|
begin + end | 获取第一个数据位置的iterator/const_iterator, 获取最后一个数据的下一个位置的iterator/const_iterator |
rbegin + rend | 获取最后一个数据位置的reverse_iterator,获取第一个数据前一个位置的 |
reverse_iterator
#include <iostream>
#include <vector>
using namespace std;
void PrintVector(const vector<int>& v)
{
vector<int>::const_iterator it = v.begin();
for (; it != v.end(); it++)
{
cout << *it << ' ';
}
cout << endl;
}
int main()
{
vector<int> v;
v.push_back(9);
v.push_back(5);
v.push_back(2);
v.push_back(7);
vector<int>::iterator it1 = v.begin();
for (; it1 != v.end(); it1++)
{
cout << *it1 << ' ';
}
cout << endl;
vector<int>::iterator it2 = v.begin();
for (; it2 != v.end(); it2++)
{
*it2 *= 2;
cout << *it2 << ' ';
}
cout << endl;
vector<int>::reverse_iterator rit = v.rbegin();
for (; rit != v.rend(); rit++)
{
cout << *rit << ' ';
}
cout << endl;
PrintVector(v);
system("pause");
return 0;
}
3、vector类的容量操作及空间增长问题
容量空间 | 接口说明 |
---|---|
size | 获取数据个数 |
capacity | 获取容量大小 |
empty | 判断是否为空 |
resize | 改变vector的size |
reserve | 改变vector放入capacity |
#include <iostream>
#include <vector>
using namespace std;
void TestCapacity()
{
size_t size;
vector<int> foo;
size = foo.capacity();
cout << "making foo grow:" << endl;
for (int i = 0; i < 100; i++)
{
foo.push_back(i);
if (size != foo.capacity())
{
size = foo.capacity();
cout << "capacity changed: " << size << endl;
}
}
}
void TestReserve()
{
size_t size;
vector<int> bar;
size = bar.capacity();
bar.reserve(100);
cout << "making bar grow" << endl;
for (int i = 0; i < 100; i++)
{
bar.push_back(i);
if (size != bar.capacity())
{
size = bar.capacity();
cout << "capacity changed: " << size << endl;
}
}
}
void TestResize()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
v.resize(5);
v.resize(8, 100);
v.resize(12);
cout << "v contains:" << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << ' ';
}
cout << endl;
}
int main()
{
TestCapacity();
TestReserve();
TestResize();
system("pause");
return 0;
}
1、capacity在vs下按1.5倍增长,g++是按2倍增长
2、reserve负责开辟空间,预知空间大小,可以使用reserve缓解扩容缺陷
3、resize在开辟空间的同时会初始化,影响size
4、vector类的增删改查
vector增删查改 | 接口说明 |
---|---|
push_back | 尾插 |
pop_back | 尾删 |
find | 查找。(注意这个是算法模块实现,不是vector的成员接口) |
insert | 在position之前插入val |
erase | 删除position位置的数据 |
swap | 交换两个vector的数据空间 |
operator[] | 像数组一样访问 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void TestPushAndPop()
{
int a[] = { 1, 2, 3, 4 };
vector<int> v(a, a + sizeof(a) / sizeof(a[0]));
vector<int>::iterator it = v.begin();
for (; it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
v.pop_back();
v.pop_back();
it = v.begin();
for (; it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void TestFindInsertErase()
{
int a[] = { 1, 2, 3, 4 };
vector<int> v(a, a + sizeof(a) / sizeof(a[0]));
vector<int>::iterator pos = find(v.begin(), v.end(), 3);
v.insert(pos, 30);
vector<int>::iterator it = v.begin();
for (; it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
pos = find(v.begin(), v.end(), 3);
v.erase(pos);
it = v.begin();
for (; it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void TestIndexVisit()
{
int a[] = { 1, 2, 3, 4 };
vector<int> v(a, a + sizeof(a) / sizeof(a[0]));
v[0] = 10;
cout << v[0] << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << ' ';
}
cout << endl;
vector<int> tmp;
tmp.swap(v);
for (int i = 0; i < tmp.size(); i++)
{
cout << tmp[i] << ' ';
}
cout << endl;
for (auto & e : v)
{
cout << e << ' ';
}
cout << endl;
}
int main()
{
TestPushAndPop();
TestFindInsertErase();
TestIndexVisit();
system("pause");
return 0;
}
5、vector类的迭代器失效场景
#include <iostream>
#include <vector>
using namespace std;
void TestInsertErase()
{
int a[] = { 1, 2, 3, 4 };
vector<int> v(a, a + sizeof(a) / sizeof(a[0]));
//删除导致迭代器非法访问的失效
vector<int>::iterator pos = find(v.begin(), v.end(), 3);
v.erase(pos);
cout << *pos << endl;
//插入导致增容,增容后pos指向新的空间,原来的空间已经被释放
pos = find(v.begin(), v.end(), 3);
v.insert(pos, 30);
cout << *pos << endl;
}
void TestVector()
{
int a[] = { 1, 2, 3, 4 };
vector<int> v(a, a + sizeof(a) / sizeof(a[0]));
vector<int>::iterator it = v.begin();
while (it != v.end())
{
if (*it % 2 == 0)
{
it = v.erase(it);
}
else
{
it++;
}
}
cout << endl;
//失效
/*it = v.begin();
for (; it != v.end(); it++)
{
if (*it % 2 == 0)
{
v.erase(it);
}
}
cout << endl;*/
}
int main()
{
//TestInsertErase();
TestVector();
system("pause");
return 0;
}
迭代器失效场景:
1、删除导致迭代器非法访问的失效
2、插入导致增容,增容后pos指向新的空间,原来的空间已经被释放