vector的重点构造函数
- vector就是一个顺序表
- vector是一个顺序容器,是一个大小(size)可以改变的数组
遍历
void test_vector1()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
//遍历
// 1、下标+[]
for (size_t i = 0; i < v.size(); ++i)
{
v[i] -= 1;
cout << v[i] << " ";
}
cout << endl;
// 2、迭代器
vector<int>::iterator it = v.begin();
while (it != v.end())
{
*it += 1;
cout << *it << " ";
++it;
}
cout << endl;
// 3、范围for
for (auto e : v)//加了引用修改e就是修改*it
{
cout << e << " ";
}
cout << endl;
//迭代器区间进行构造
vector<int> v2(++v.begin(), --v.end());
for (auto e : v2)
{
cout << e << " ";
}
cout << endl;
// 其他容器迭代器初始化,只要数据类型可以匹配上(*iterator对象的类型跟vector中存的数据类型是一致)
string s("hello world");
vector<char> v3(s.begin(), s.end());
for (auto e : v3)
{
cout << e << " ";
}
cout << endl;
//拷贝构造
vector<int> v4(v);
for (auto e : v4)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
test_vector1();
return 0;
}
迭代器(普通 / 反向)
- 类似指针的东西
- 普通和反向的const迭代器(只读)和string的一样
void test_vector2()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
vector<int>::iterator it = v.begin();
while (it != v.end())
{
//*it += 1;
cout << *it << " ";
++it;
}
cout << endl;
vector<int>::reverse_iterator rit = v.rbegin();
while (rit != v.rend())
{
cout << *rit << " ";
++rit;
}
cout << endl;
}
int main()
{
test_vector2();
return 0;
}
reserve、resize
void test_vector3()
{
vector<int> v;
//cout << v.max_size() << endl;
// 开空间,改变容量
v.reserve(10);
// 错误访问 -> operator[] 中回去检查i是否小于size
//for (size_t i = 0; i < 10; ++i)
//{
// v[i] = i;
//}
for (size_t i = 0; i < 10; ++i)
{
v.push_back(i);//开间已经开好了,避免增容
}
// 开空间,+初始化
v.resize(20);
//v.resize(20, 1);
for (size_t i = 0; i < 20; ++i)
{
v[i] = i;
}
}
int main()
{
test_vector3();
return 0;
}
assign
void test_vector4()
{
int a[] = { 1, 2, 3, 4, 5 };
//vector<int> v(a, a+4);
vector<int> v;
v.assign(a, a + 4);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
test_vector4();
return 0;
}
insert 、排序
#include <iostream>
#include <vector>
#include <algorithm>//sort
#include <functional>//greater
using namespace std;
void test_vector5()
{
int a[] = { 1, 2, 3, 4, 5 };
vector<int> v(a, a + 5);
// 头插
v.insert(v.begin(), 0);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
// 在2的前面插入
vector<int>::iterator pos = find(v.begin(), v.end(), 2);
if (pos != v.end())
{
v.insert(pos, 20);
}
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
//排序--默认是排升序
sort(v.begin(), v.end());
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
// 排降序 -- 关于greater<int>是一个仿函数类
//有名对象
//greater<int> gt;
//sort(v.begin(), v.end(), gt);
//匿名对象
sort(v.begin(), v.end(), greater<int>());
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
//对数组排序
// 指向数组空间的指针是天然的迭代器
int a1[] = { 30, 4, 50, 6, 7 };
sort(a1, a1 + 5);
for (auto e : a1)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
test_vector5();
return 0;
}
erase
#include<iostream>
#include <vector>
using namespace std;
void test_vector6()
{
int a[] = { 0, 1, 2, 3, 4, 5 };
vector<int> v(a, a + 5);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
// 头删
v.erase(v.begin());
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
//删除2
vector<int>::iterator pos = find(v.begin(), v.end(), 2);
if (pos != v.end())
{
v.erase(pos);
}
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
test_vector6();
return 0;
}