什么是Trailing Array Idiom?
P.S:谷歌搜索这个术语给出 vector 是使用尾随数组惯用语实现的,因此如果不改变 vector 对象本身的地址,它们就不能调整大小。
最佳答案
如果您指的是 GCC source code 中提到的尾随数组习语(你的引用来自哪里),它似乎指的是实现动态数组的旧 C 技巧:
typedef struct {
/* header */
size_t nelems;
/* actual array */
int a[1];
} IntVector;
创建数组的位置
IntVector *make_intvector(size_t n)
{
IntVector *v = malloc(sizeof(IntVector) + sizeof(int) * (n-1));
if (v != NULL)
v->nelems = n;
return v;
}
关于c++ - 尾随数组习语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4231953/