对比array是静态空间一旦配置了就不能改变大小。 vector的动态增加大小的时候,并不是在原有的空间上持续新的空间(无法保证原空间的后面还有可供配置的空间),而是以原大小的两倍另外配置一块较大的空间,然后将原内容拷贝过来,并释放原空间。在VS下是1.5倍扩容,在GCC下是2倍扩容。 在原来空间不够存储新值时,每次...
//vector<int>::iterator it; //C++11之前用的,11之后可以用auto for(autoit=num.begin(); it!=num.end(); ++it) { cout << *it <<" "; } cout << endl; num.insert(num.begin()+2,3,10);//在num[2]之前加3个10 cout <<"Inserted array:\n"; for(autoit=num.begin(); it!=num....
std::array是封装固定大小数组的容器。 此容器是一个聚合类型,其语义等同于保有一个C 风格数组T[N]作为其唯一非静态数据成员的结构体。不同于 C 风格数组,它不会自动退化成T*。作为聚合类型,它能聚合初始化,只要有至多N个能转换成T的初始化器:std::array<int,3>a={1,2,3};。
array 数组 随机读改 O(1) 无序 可重复 支持随机访问 vector 数组 随机读改、尾部插入、尾部删除 O(1)头部插入、头部删除 O(n) 无序 可重复 支持随机访问 deque 双端队列 头尾插入、头尾删除 O(1) 无序 可重复 一个中央控制器 + 多个缓冲区,支持首尾快速增删,支持随机访问 forward_list 单向链表 插入、...
vector−deque−array(C++11) list−forward_list(C++11) map−multimap−set−multiset unordered_map(C++11) unordered_multimap(C++11) unordered_set(C++11) unordered_multiset(C++11) Container adaptors span(C++20)−mdspan(C++23) Iterators library ...
#include "benchmark/cppbenchmark.h" #include <algorithm> #include <vector> class SortFixture : public virtual CppBenchmark::Fixture { protected: std::vector<int> items; void Initialize(CppBenchmark::Context& context) override { items.resize(context.x()); std::generate(items.begin(), items...
are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array. ...
typedef array<int, 3> ARR; typedef vector<int> VI; typedef vector<double> VDB; typedef vector<string> VS; typedef vector<PII> VPII; typedef vector<ARR> VARR; typedef vector<VI> VVI; typedef vector<VDB> VVDB; typedef vector<VPII> VVPII; typedef unordered_map<int,int> ...
<cpp |container |vector Returns a pointer to the underlying array serving as element storage. The pointer is such that range[data(),data()+size())is always avalid range, even if the container is empty (data()is not dereferenceable in that case). ...
{ heapArray = new int[1]; heapArray[0] = flag; } }; class Heap { public: Heap(); Heap(std::vector<int> vals, enum HEAP_TYPE flag=MIN); ~Heap(); //1 入队,即插入1个值,O(logN) void enqueue(int val); //2 出队,即删除堆顶,并重新调整堆,O(logN) int dequeue(); //3 ...