C++ stl 学习2-- vector
vector是能动态增加的顺序容器,因为能动态增加,所以当容量快满时会扩容,原来的元素会移动到新分配的空间那里。
vector<int> a
a.size() // 实际元素个数
Return size (public member function)
a.max_size() // 能容纳的最大的元素个数,基本不会用到
Return maximum size (public member function)
a.resize() // 扩大或者减小元素个数,扩大后的数值为0,减小时多余元素实际还存在,不过访问不到
Change size (public member function)
a.capacity() // 现在分配的元素个数,相比较于size,可能分配了没用,基本是不够就扩大2倍
Return size of allocated storage capacity (public member function)
a.empty() // 判断是否为空
Test whether vector is empty (public member function)
a.reserve() // 改变capacity,输入参数比capacity大就扩大到对应数值或者更大,小capacity无变化
Request a change in capacity (public member function)
a.shrink_to_fit() // 当capacity比size大时,使capacity减小到和size一样大
Shrink to fit (public member function)
#include <iostream> #include <vector> using namespace std; int main(void) { vector<int> a; a.push_back(1); a.push_back(4); a.push_back(2); a.push_back(8); a.push_back(5); a.push_back(7); for (auto i : a) { // 基于范围的for循环 cout << i << endl; } for (vector<int>::iterator it = a.begin(); it != a.end(); it++) { cout << *it << endl; } cout << a.size() << endl; // 大小是6 cout << a.max_size() << endl; // 很大的数2305843009213693951,没什么意义 cout << a.capacity() << endl; // 大小是8,分配的空间是8,实际用了6 a.resize(3); // 1 4 2 cout << a.capacity() << endl; // 大小是8,resize数值比原来capacity小,这里不会扩容 for (vector<int>::iterator it = a.begin(); it != a.end(); it++ ) { cout << *it << endl; // 1 , 4 ,2 } cout << a[3] << endl; // 还能访问,是8 cout << a[4] << endl; // 还能访问,是5 cout << a[5] << endl; // 还能访问,是7 a.resize(12); // 扩容后是 1 4 2 0 0 0 0 0 0 0 0 0 ,后面默认填0,原来能访问的 8 5 7 被替换为 0 0 0 cout << a.capacity() << endl; // 大小是12,resize数值比原来capacity大,扩容到和resize大小一样 a.push_back(3); cout << a.capacity() << endl; // 加1 后capacity翻倍,变成24 a.reserve(6); // 有13个元素,想把capacity减小到6,怎么可能呢 cout << a.size() << endl; // 13 cout << a.capacity() << endl; // 24 a.shrink_to_fit(); //缩减capacity到size大小 cout << a.size() << endl; // 13 cout << a.capacity() << endl; // 13 }
a.front() 获取第一个元素
a.back()获取最后一个元素
a.data() 获取第一个元素的地址
a.assign() 给vector 里面元素赋值
a.push_back() // 尾插
a.pop_back() // 弹出最后元素
a.insert() // 在指定的位置插入元素,原来处于该位置的元素,如果有的话,往后移动
返回是指向新插入的首元素的的迭代器(An iterator that points to the first of the newly inserted elements)
The vector is extended by inserting new elements before the element at the specified position
a.erase() // 删除元素,迭代器会失效,返回指向删除元素的下一个元素的迭代器
An iterator pointing to the new location of the element that followed the last element erased by the function call
a.swap() // 与另一个vector 交换元素,要求类型相同,大小可以不同
a.clear() // 删除所有的元素
a.emplace() // 在指定的位置生成元素,原来处于该位置的元素,如果有的话,往后移动
返回指向首个新元素的迭代器 An iterator that points to the newly emplaced element
a.emplace_back() // 尾插新元素
Inserts a new element at the end of the vector
insert 与emplace的不同
emplace在插入元素时,是在容器的指定位置直接构造元素
而insert是先构造元素(调用构造函数),然后再拷贝或者移动过去(调用复制构造或者移动构造函数)
emplace_back 与push_back的不同
emplace_back在插入元素时,是在容器的尾部直接构造元素
而push_back是先构造元素(调用构造函数),然后再拷贝或者移动过去(调用复制构造或者移动构造函数)
#include <iostream> #include <vector> using namespace std; int main () { vector<int> a; a.push_back(1); a.push_back(4); a.push_back(2); a.push_back(8); a.push_back(5); a.push_back(7); int b[] = {1,2,3,4}; cout << *a.data() << endl; // 1,获取第一个元素地址 cout << a.front() << endl; // 1 ,第一个元素的值 cout << a.back() << endl; // 7,最后一个元素的值 a.assign(3 ,10); // 10, 10, 10 a.assign(begin(b), end(b)); // 1, 2, 3, 4 a.pop_back(); // 4被移除了,1 2 3 a.insert(begin(a), 300); // 300, 1, 2, 3 在begin 位置处插入300, 1往后移 a.insert(end(a) - 1, 300); // 300, 1, 2, 300, 3, 在end() - 1处,即最后一个元素这里插入300, 3往后移
a.insert(end(a), 400); // 300, 1, 2, 300, 3, 300
return 0; }
#include <iostream> #include <vector> usnig namespace std; int main() { vector<int> c = {1,3,1,3,1,3}; auto it = begin(c); cout << "======" << endl; for (; it != end(c); ++it) { if (*it == 3) { c.insert(it , 2); // 迭代器失效,可能插入元素后重新分配内存空间 break; } } // 迭代器失效后,这里会多很多莫名奇妙的值 for (; it != end(c); ++it) { cout << *it << endl; } return 0; }
牛客网的一道题
#include <iostream> #include <vector> using namespace std; int main(void) { vector<int>array; array.push_back(100); array.push_back(300); array.push_back(300); array.push_back(300); array.push_back(300); array.push_back(500); vector<int>::iterator itor; for(itor=array.begin();itor!=array.end();itor++) { if(*itor==300) { itor=array.erase(itor); // 删除了300后指向下一个元素300,然后for循环又加了一次,所以下一次直接指到第3个300那里去了 } } for(itor=array.begin();itor!=array.end();itor++) { cout<<*itor<<""; } return 0; }
A 100 300 300 300 300 500
B 100 300 300 300 500
C 100 300 300 500
D 100 300 500
E 100 500
F 程序错误

浙公网安备 33010602011771号