C++ stl 学习6-- deque
deque 双端队列,不同于vector是一块连续的区间,deque是一组独立区块, 不是所有元素像vector那样都连续,中间有部分不是相邻的。所以不建议用指针访问,建议用迭代器。

resize() // 改变deque的大小,n小于当前size 则保留前n个,n大于当前size则将其它的元素设置为0或者指定值
If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).
If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.
#include <iostream> #include <deque> using namespace std; int main() { deque<int> a; a.assign({1,3,5,7,8}); a.push_back(12); // 1 3 5 7 8 12 a.resize(3); // 1 3 5 后面的元素还在但是不属于容器了 a.emplace(begin(a), 6); // 6 1 3 5 cout << a[4] << endl; // 7 auto it = a.insert(end(a), 66); // 6 1 3 5 66 it 指向66 it = a.erase(it); // it 指向end for (auto i : a) { cout << i << endl; } return 0; }

浙公网安备 33010602011771号