class template std::vector

本文详细介绍了动态数组(vector)的工作原理及内部实现机制。与普通数组相比,动态数组能自动调整大小,但会消耗更多内存。文章还对比了动态数组与其他容器如双向队列、链表等在元素访问和插入删除操作上的性能差异。
Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Compared to the other dynamic sequence containers ( deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.
在实例化 `std::vector<int, std::unordered_map<int, int>>` 时出现问题,是因为 `std::vector` 的模板参数使用有误。`std::vector` 模板有两个参数,第一个参数是存储的元素类型,第二个参数是分配器类型,而 `std::unordered_map<int, int>` 并非有效的分配器类型,所以会导致实例化错误。 要解决这个问题,需要根据实际需求来修改模板参数。如果想要存储 `int` 类型的元素,只需要指定一个模板参数即可;如果想要存储 `std::unordered_map<int, int>` 类型的元素,将其作为第一个模板参数传入。 以下是两种正确的实例化方式: #### 存储 `int` 类型的元素 ```cpp #include <iostream> #include <vector> int main() { // 正确实例化存储 int 类型元素的 vector std::vector<int> intVector; intVector.push_back(1); intVector.push_back(2); intVector.push_back(3); for (int num : intVector) { std::cout << num << " "; } std::cout << std::endl; return 0; } ``` #### 存储 `std::unordered_map<int, int>` 类型的元素 ```cpp #include <iostream> #include <vector> #include <unordered_map> int main() { // 正确实例化存储 std::unordered_map<int, int> 类型元素的 vector std::vector<std::unordered_map<int, int>> mapVector; std::unordered_map<int, int> map1 = {{1, 100}, {2, 200}}; std::unordered_map<int, int> map2 = {{3, 300}, {4, 400}}; mapVector.push_back(map1); mapVector.push_back(map2); for (const auto& map : mapVector) { for (const auto& pair : map) { std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl; } } return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值