C ++向量是否总是连续的? [英] Are C++ Vectors always contiguous?
问题描述
可能重复:
std :: vector元素是否保证是连续的?
我遇到过一种技术,人们可以使用C ++中的向量来接收或发送MPI操作数据,因为据说它可以将元素连续存储在内存中.
I have come across a technique in which people use a vector in C++ to receive or send data for MPI operations as it is said to store elements contiguously in memory.
但是,我仍然怀疑这种方法是否适用于任何大小的向量,尤其是当向量增长到一定大小时,这种假设可能会破裂.
However, I remain skeptical of whether this approach would remain robust for a vector of any size, especially when the vector grows to a certain size, where this assumption could break down.
下面是我正在谈论的一个例子:
Below is an example of what I am talking about :
MPI_Recv( &partials[0] , partials.size() , mpi_partial , 0,
DALG_ELIMINATE_REQ_MSG ,MPI_COMM_WORLD , &status );
推荐答案
是的,无论大小如何,C ++向量始终是连续的.
Yes, C++ vectors are always contiguous, regardless of size.
但是并不意味着它们在您缩小或扩展它们时不会在内存中四处移动.
But that doesn't mean that they don't move around in memory as you shrink or expand them...
这篇关于C ++向量是否总是连续的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!