创建vector对象的方式
#include<stdio.h>
#include<iostream>
#include<string>
#include<assert.h>
#include<vector>
using namespace std;
struct A
{
A(int a = 1)
:_a(a)
{}
int _a = 1;
};
void test()
{
//空vector
vector<int> v1;
vector<char> v2;
vector<A> v3;
//vector(n,value)
//内置类型,缺省的值为0
//int:0
//char:\0
//float/double:0.0
//指针:nullptr
vector<int> v4(5);
vector<char> v5(5);
vector<int*> v6(6);
vector<float> v7(5);
//vector<自定义类型>变量名(num,构造函数(参数)
//自定义类型:缺省值为默认构造创建的对象
vector<A>v8(5);
vector<A>v9(5, A(10));
int arr[] = { 1,2,3 };
vector<int>v10(arr,arr+sizeof(arr)/sizeof(arr[0]));
string str("12345");
vector<char>v11(str.begin(), str.end());
};
int main()
{
test();
return 0;
}
迭代器的使用
#include<stdio.h>
#include<iostream>
#include<string>
#include<assert.h>
#include<vector>
using namespace std;
void test()
{
string str("12345");
vector<char>v(str.begin(), str.end());
vector<char>::iterator it = v.begin();//迭代器
//vector<char>::const_iterator it = v.begin();只读迭代器
int idx = 0;
while (it != v.end())
{
cout << *it << " ";//迭代器和指针类似
*it = 'a' + idx;
++it;
idx++;
}
cout << endl;
vector<char>::reverse_iterator rit = v.rbegin();//反向迭代器
while (rit != v.rend())
{
cout << *rit << " ";
++rit;
}
}
int main()
{
test();
return 0;
}
空间增长接口
#include<stdio.h>
#include<iostream>
#include<string>
#include<assert.h>
#include<vector>
using namespace std;
struct A
{
int _a;
char _c;
double _b;
int* _p;
};
void test()
{
vector<char>v1;
vector<int>v2;
vector<int*>v3;
vector<double>v4;
vector<A>v5;
cout << v1.max_size() << endl;
cout << v2.max_size() << endl;
cout << v3.max_size() << endl;
cout << v4.max_size() << endl;
cout << v5.max_size() << endl;
cout << v1.size() << endl;
cout << v2.size() << endl;
cout << v3.size() << endl;
cout << v4.size() << endl;
cout << v5.size() << endl;
cout << v1.capacity() << endl;
cout << v2.capacity() << endl;
cout << v3.capacity() << endl;
cout << v4.capacity() << endl;
cout << v5.capacity() << endl;
}
int main()
{
test();
return 0;
}
resize接口
如果size大于容量,容量的值也会更改
v1.resize(10);
v2.resize(3);
v5.resize(4);
reserve接口
v3.reserve(5);
v4.reserve(2);
shrink_to_fit接口
v1.resize(10);
v2.resize(5);
v5.resize(4);
v3.reserve(5);
v4.reserve(2);
v2.reserve(10);
v2.shrink_to_fit();
将v2的容量与size大小匹配。
遍历接口
#include<stdio.h>
#include<iostream>
#include<string>
#include<assert.h>
#include<vector>
using namespace std;
void test()
{
string str = "123456";
vector<char>v(str.begin(), str.end());
for (size_t i = 0; i < str.size(); i++)
{
cout << v.operator[](i) << " ";
v.operator[](i) = i + 'a';
cout << v[i] << " ";//简写形式
}
}
int main()
{
test();
return 0;
}
at、front、back接口
#include<stdio.h>
#include<iostream>
#include<string>
#include<assert.h>
#include<vector>
using namespace std;
void test()
{
string str = "123456";
vector<char>v(str.begin(), str.end());
for (size_t i = 0; i < str.size(); i++)
{
cout << v.operator[](i) << " "<<endl;
}
cout<<v.at(1)<<endl;
cout << v.front() << endl;
cout << v.back() << endl;
}
int main()
{
test();
return 0;
}
data接口
返回v的地址
char* ptr = v.data();
assign接口
赋值操作
#include<stdio.h>
#include<iostream>
#include<string>
#include<assert.h>
#include<vector>
using namespace std;
void test()
{
string str = "123456";
vector<char>v(str.begin(), str.end());//123456
v.assign(5, 'a');//aaaaa
string str2("abcdeg");
v.assign(str2.begin(), str2.end());//abcdeg
}
int main()
{
test();
return 0;
}
push_back、pop_back接口
尾插尾删操作
void test()
{
string str = "123456";
vector<char>v(str.begin(), str.end());
v.assign(5, 'a');
string str2("abcdeg");
v.assign(str2.begin(), str2.end());
v.push_back('1');//abcdeg1
v.push_back('2');//abcdeg12
v.pop_back();//abcdeg1
}
insert接口
插入操作
void test()
{
string str = "123456";
vector<char>v(str.begin(), str.end());
v.assign(5, 'a');
string str2("abcdeg");
v.assign(str2.begin(), str2.end());
v.push_back('1');
v.push_back('2');
v.pop_back();
v.insert(v.begin(), '3');//3abcdeg1
v.insert(--v.end(), 5, 'a');//3abcdegaaaaa1
v.insert(++v.begin(), str2.begin(), str2.end());//3abcdegabcdegaaaaa1
}
void test()
{
vector<A> v;
A a1(1,1);
v.push_back(a1);
v.emplace_back(2,2);//只需要传构造函数需要的参数,接口内部直接创建对象
}
void test()
{
vector<A> v;
A a1(1,1);
v.push_back(a1);
v.emplace_back(2,2);//只需要传构造函数需要的参数,接口内部直接创建对象
v.insert(v.begin(), A(2, 3));
v.emplace(v.begin(), 5, 6);
}
erase接口
删除操作
void test()
{
string str = "123456";
vector<char>v(str.begin(), str.end());
v.assign(5, 'a');
string str2("abcdeg");
v.assign(str2.begin(), str2.end());
v.push_back('1');
v.push_back('2');
v.pop_back();
v.erase(v.begin());//bcdeg1
v.erase(++v.begin(), --v.end());//b1;左闭右开
}
vector的增容
#include<stdio.h>
#include<iostream>
#include<string>
#include<assert.h>
#include<vector>
using namespace std;
void test()
{
vector<int>v;
size_t c = v.capacity();
cout << c << endl;
for (size_t i = 0; i < 200; i++)
{
v.push_back(i);
if(c != v.capacity())
{
c = v.capacity();
cout << c << endl;
}
}
}
int main()
{
test();
return 0;
}
迭代器失效的一些情况
void test1()
{
vector<int>v(3, 10);
vector<int>::iterator it = v.begin();
cout << *it << endl;
v.push_back(4);
//cout << *it << endl;
//错误,迭代器失效,此时迭代器指向的空间已被释放,指向的位置是非法的位置,如果增容了迭代器就会失效。
//resize,reserve,push_back,insert,emplace,emplace_back等操作都有可能造成迭代器失效
it = v.begin();
cout << *it << endl;
v.erase(it);
//迭代器失效,迭代器指向的位置发生了变化
//cout << *it << endl;
}
迭代器失效解决办法:在使用前,对迭代器重新赋值即可。