简体   繁体   English

数组中的向量

[英]Vectors in Arrays

如果我有一个向量数组,由于数组的连续存储特性,向量的大小调整能力是否受到限制?

Yes, but not in the way you're thinking. 是的,但不是您的思维方式。

Vectors have to find contiguous address space for their content. 向量必须为其内容找到连续的地址空间。 Memory fragmentation may cause the largest contiguous block to be smaller than total free memory. 内存碎片可能会导致最大的连续块小于总的可用内存。 And having many vectors makes fragmentation more likely. 拥有多个向量使碎片的可能性更大。

No; 没有; internally, vectors hold pointers to the memory blocks, not the block themselves. 在内部,向量保存指向存储块的指针 ,而不是存储块本身的指针

resize won't affect the array's memory at all. resize完全不会影响阵列的内存。 The vectors have a pointer to the actual storage, so resizing affects some other memory that has nothing to do with the array's. 向量具有指向实际存储的指针,因此调整大小会影响与该数组无关的其他一些内存。 All that will be in the array is basically just pointers pointing to possibly different lengths of memory blocks. 数组中的所有内容基本上只是指向可能不同长度的存储块的指针。

Furthermore, if you have something like this: 此外,如果您有这样的事情:

std::vector<int> arr [5];

The array's memory will be on the stack, and the vectors' memory will be on the heap. 数组的内存将在堆栈上,向量的内存将在堆上。 Totally different! 完全不同!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM