std::vector of std::vectors contiguity(std::vectors 的 std::vectors 连续性)
问题描述
我知道 std::vector 在内部连续地存储它的数据(除非它是 std::vector),都在旧的 中>C++03 标准和新的C++11.
I know that std::vector<T> internally stores it's data contiguously (unless it is std::vector<bool>) both in the old C++03 standard and the new C++11.
处理这个问题并引用标准的不错的 stackoverflow 问题:answer、答案.
Nice stackoverflow questions that deal with this and quote the standard: answer, answer.
嵌套向量中的数据怎么样 std::vector ?它是如何存储的?
What about the data inside nested vectors std::vector <std::vector <T> >? How is that stored?
如果每个内部向量都需要连续存储它的数据,那么 &v[n] == &v[0] + n for all 0 <= n
If every internal vector needs to store it's data contiguously, how can it be true that &v[n] == &v[0] + n for all 0 <= n < v.size().
用稍微不同的话说,是否可以简单地"并顺序地(通过指针或类似物)访问存储在这种嵌套结构中的所有元素一维向量?
To phrase this slightly different, is it possible to access all the elements stored in such nested structure "simply" and sequentially (via a pointer or similar) the same way it can be done for a 1-D vector?
推荐答案
没有.vector 的元素存储在动态分配的内存块中;否则,vector 的容量无法增加.vector 对象只包含一个指向该块的指针.
No. The elements of a vector are stored in a dynamically allocated block of memory; otherwise, the capacity of the vector could not increase. The vector object just holds a pointer to that block.
元素顺序存储的要求仅适用于元素本身,而不适用于这些元素的任何动态分配的成员.
The requirement that the elements be stored sequentially applies only to the elements themselves, and not to any dynamically allocated members of those elements.
这篇关于std::vectors 的 std::vectors 连续性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::vectors 的 std::vectors 连续性
基础教程推荐
- c++ STL设置差异 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
