std::vector 与 array[number] 相同吗? [英] is std::vector same as array[number]?
问题描述
可能的重复:
是否保证 std::vector 元素是连续的?
std::vector 是否总是将连续内存地址中的数据包含为数组[编号]?
does std::vector always contain the data in sequential memory addresses as array[number]?
推荐答案
对于除 bool 之外的所有类型,标准要求元素在内存中是连续的:
For all types except bool, the standard requires the elements are contiguous in memory:
23.2.4/1 ... 向量的元素是连续存储的,这意味着如果 v 是一个向量,其中 T 是 bool 以外的某种类型,那么它遵循恒等式 &v[n] == &v[0] + n 对于所有 0 <= n
23.2.4/1 ... The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size()
请记住,std::vector
有特殊要求,与 bool 数组不同.
Do keep in mind that std::vector<bool>
has special requirements and is not the same as an array of bool.
这篇关于std::vector 与 array[number] 相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!