vector使用了类模板,需要显示的实例化
单参数的构造函数支持隐式类型转换
vector<char>能否替代string?
答案当然当然是不可以
1.string要求最后有'\0',可以很好的与C语言兼容
vector没有要求,不能很好与C语言兼容
2.string的接口比vector更丰富,string有很多专用接口,例如:+=,比较大小(vector之间比较大小意义不大)
单参数的构造函数支持隐式类型转换
vector
Alloc:空间配置器,内存池(T提高内存申请效率)
后面的是模板参数
构造
无参构造
n个T(模板)构造 vector<int>s(10,1); vector<string>str(10,"***");
一段迭代器构造,在这里写成了模板,因此不限于vector的迭代器初始化,还可以用其它容器的迭代器
拷贝构造
void test_vector3()
{
//n个T初始化
vector<int> v1(10, 1);
vector<string> v2(10, "***");
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
for (auto e : v2)
{
cout << e << " ";
}
cout << endl;
// 自己类型的迭代器
vector<int> v3(v1.begin(), v1.end());
for (auto e : v3)
{
cout << e << " ";
}
cout << endl;
//其它类型的迭代器
string str("hello world");
vector<char> v4(str.begin(), str.end());
for (auto e : v4)
{
cout << e << " ";
}
cout << endl;
//指针
int a[] = { 16,2,77,29 };
vector<int> v5(a, a+4);
for (auto e : v5)
{
cout << e << " ";
}
cout << endl;
}
容器与算法
算法头文件
#include<algorithm>
这些区间一般都是左闭右开。
通过迭代器访问容器中的数据
#include<algorithm>
int a[]={16,2,77,29};
vector<int>vint(a,a+4);
//升序
//默认less
sort(vint.begin(),vint.end());
for(auto e:vint)
cout<<e<<" ";
cout<<endl;
//降序
//greater
//greater<int>gt;
//sort(vint.begin(),vint.end(),gt);
//匿名对象
sort(vint.begin(),vint.end(),greater<int>());
for(auto e:vint)
cout<<e<<" ";
cout<<endl;
反向迭代器也可以排降序。相当于反过来排,倒着遍历
reserve(开空间,不改变size)和resize(开空间+初始化,改变size)
reserve的一个常见错误
这个代码会报错,越界访问,因为reserve不会改变size。
reserve要配合push_back来使用插入数据
解决办法就是用resize来开
resize会初始化,把size也增上去
void test_vector4()
{
//resize单独使用
vector<int> v1;
//cout << v1.max_size() << endl;
//v1.reserve(10);
v1.resize(10);
for (size_t i = 0; i < 10; i++)
{
v1[i] = i;
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
//reserve配合push_back使用
vector<int> v2;
v2.reserve(10);
for (size_t i = 0; i < 10; i++)
{
v2.push_back(i);
}
for (auto e : v2)
{
cout << e << " ";
}
cout << endl;
}
at和[]
at越界是抛异常,抛异常用来记录日志
[]越界是断言,断言只在debug生效,release不起作用
data相当于string中的c_str
push_back和pop_back
尾插,尾删
头插,头删效率低下没有提供相应接口
erase和insert
任意位置删除或插入(可以用来头插,头删)
如果我们需要指定删除某个数据,怎么查找它的位置呢?
vector并没有提供查找接口
但是在算法中提供了查找接口
找到返回相应位置的迭代器,找不到返回最后一个数据的下一个位置
void test_vector5()
{
int a[] = { 16,2,77,29,3,33,43,3,2,3,3,2 };
vector<int> v1(a, a + sizeof(a)/sizeof(int));
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
// 头删
v1.erase(v1.begin());
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
// 头插
v1.insert(v1.begin(), 100);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
// 删除第3个数据
v1.erase(v1.begin()+2);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
// 删除3,但是不知道3在哪个位置,怎么办?
//vector<int>::iterator pos = find(v1.begin(), v1.end(), 3);
auto pos = find(v1.begin(), v1.end(), 3);
if (pos != v1.end())
{
v1.erase(pos);
}
}
那么我们需要删除数组中所有的某个值呢?
我们可以删一个找一个,但是当我们从删除的下一个位置开始找时,涉及到了迭代器失效问题,程序直接崩溃,当然如果我们又重头开始找,就没有这个问题,但是效率过于低下。
void test_vector6()
{
// 删除所有的3 -- 涉及迭代器失效!
while(pos != v1.end())
{
v1.erase(pos);
//pos = find(pos+1, v1.end(), 3);
pos = find(v1.begin(), v1.end(), 3);
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
v1.assign(10, 1);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
}
assign
清掉原先数据,重新赋值
vector的扩容逻辑
g++采用STL的sgi版本
vs采用pj版
全排序
vector OJ题
电话号码字母组合