前言
vector是平时C++中用的最多的序列式容器,下面我们就一起来了解下它的用法。其中会用到一个辅助函数print 帮助我们进行打印。
- 辅助函数:print 函数
template <typename T>
void print(vector<T>& vec) {
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << ",";
cout << "vec.size():" << vec.size();
cout << " vec.capacity():" << vec.capacity() << endl;
}
一.constructor
- constructor/operator =
void test1() {
//1.default
vector<int> vec1;
//2.fill
vector<int> vec2(10);//分配10个0的空间
cout << "vec2:" << endl;
print(vec2);
vector<int> vec3(10, 1);//分配10个1的空间
vec1 = vec3;
cout << "vec1:" << endl;
print(vec1);
//3.range
vector<int> vec4(vec1.begin(), vec1.begin()+1);//注意range遵循[,) 即左闭右开原则
cout << "vec4:" << endl;
print(vec4);
//4.copy
vector<int> vec5(vec4);
cout << "vec5:" << endl;
print(vec5);
}
二.Iterators
- begin/end/rbegin/rend/crbegin/crend
void test2() {
vector<int> vec1{ 1,2,3,4,5 };
cout << "from begin to end:" << endl;
for (vector<int>::iterator it = vec1.begin(); it != vec1.end(); it++) {
cout << *it << endl;
}
cout << "from rbegin to end:" << endl;
for (vector<int>::reverse_iterator it = vec1.rbegin(); it != vec1.rend(); it++) {
cout << *it << endl;
}
cout << "from crbegin to crend:" << endl;
for (vector<int>::const_reverse_iterator it = vec1.crbegin(); it != vec1.crend(); it++) {
cout << *it << endl;
}
}
三. Capacity
- size/max_size/capacity/shrink_to_fit
void test3() {
vector<int> vec1{ 1,2,3 };
cout << "vec1.size():" << vec1.size() << " vec1.capacity():" << vec1.capacity() << endl;
cout << "vec1.max_size():" << vec1.max_size() << endl;
for (int i = 0; i < 100; i++) {
vec1.push_back(i);
cout << "vec1.size():" << vec1.size() << " vec1.capacity():" << vec1.capacity() << endl;//从此处可以看出capacity是无规律增长的
}
cout << "sizeof(vec1):"<< sizeof(vec1) << endl;
vec1.shrink_to_fit();
cout << "vec1.size():" << vec1.size() << " vec1.capacity():" << vec1.capacity() << endl;
cout << "vec1.max_size():" << vec1.max_size() << endl;//特定类型容器的max_size是不会变化的
}
- resize/reserve resize是重新申请size大小,超过当前size则补0;reserve是申请capacity大小
void test4() {
vector<int> vec1{ 1,2,3 };
print(vec1);
vec1.resize(2);
print(vec1);
vec1.resize(5);
print(vec1);
vec1.reserve(8);
print(vec1);
//申请二维vector 3*4数组,比new/delete malloc/free 更为好用,因为vector本来就是申请好的空间
vector<vector<int>> vec2;
vec2.resize(3);
for (int i = 0; i < 3; i++) {
vec2[i].resize(4);
}
for (auto&i : vec2) {
for (auto& j : i)
cout << j << endl;
cout << endl;
}
}
四.Element access
- []/at/front/back/data []与at在vector中的作用是相似的,data则返回的是指针
void test5() {
vector<int> vec{ 1,2,3 };
cout << "vec[0]:" << vec[0] << endl;
cout << "vec.at(0):" << vec.at(2) << endl;
cout << "vec.front():" << vec.front() << endl;
cout << "vec.back():" << vec.back() << endl;
int *p = vec.data();
cout << *p << endl;
p++;
cout << *p << endl;
p++;
cout << *p << endl;
p++;
cout << *p << endl;
}
五.Modifiers
- assign/insert/erase assign的作用类似于重新初始化, insert用于插入,erase用于删除
void test6() {
vector<int> vec1;
vec1.assign(3, 1);
print(vec1);
vector<int> vec2{ 2,3 };
vec1.assign(vec2.begin(), vec2.end());
print(vec1);
vec1.assign(1,0);
vec1.insert(vec1.end(), 1);
vec1.insert(vec1.end(), vec2.begin(), vec2.end());
vec1.insert(vec1.end(), 5, 5);
print(vec1);
vec1.erase(vec1.begin());
vec1.erase(vec1.end() - 5, vec1.end());
print(vec1);
}