C++ 取消引用指向访问元素的向量指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1910712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dereference vector pointer to access element
提问by Mat
If i have in C++ a pointer to a vector:
如果我在 C++ 中有一个指向向量的指针:
vector<int>* vecPtr;
And i'd like to access an element of the vector, then i can do this by dereferncing the vector:
我想访问向量的一个元素,然后我可以通过取消引用向量来做到这一点:
int a = (*vecPtr)[i];
but will this dereferencing actually create a copy of my vector on the stack? let's say the vector stores 10000 ints, will by dereferencing the vecPtr 10000 ints be copied?
但是这种取消引用实际上会在堆栈上创建我的向量的副本吗?假设向量存储 10000 个整数,是否会通过取消引用 vecPtr 来复制 10000 个整数?
Thanks!
谢谢!
回答by sergtk
10000 int
s will not be copied. Dereferencing is very cheap.
10000int
秒不会被复制。取消引用非常便宜。
To make it clear you can rewrite
为了清楚起见,您可以重写
int a = (*vecPtr)[i];
as
作为
vector<int>& vecRef = *vecPtr; // vector is not copied here
int a = vecRef[i];
In addition, if you are afraid that the whole data stored in vector
will be located on the stack and you use vector<int>*
instead of vector<int>
to avoid this: this is not the case.
Actually only a fixed amount of memory is used on the stack (about 16-20 bytes depending on the implementation), independently of the number of elements stored in the vector
.
The vector
itself allocates memory and stores elements on the heap.
此外,如果您担心存储的整个数据vector
将位于堆栈上,而您使用vector<int>*
代替vector<int>
来避免这种情况:情况并非如此。实际上,堆栈上只使用了固定数量的内存(大约 16-20 字节,具体取决于实现),与vector
. 它vector
本身分配内存并将元素存储在堆上。
回答by Todd Gamblin
No, nothing will be copied; dereferencing just tells C++ that you want to invoke operator[] on the vector, not on your pointer, vecPtr
. If you didn't dereference, C++ would try to look for an operator[] defined on the std::vector<int>*
type.
不,不会复制任何内容;取消引用只是告诉 C++ 你想在vector上调用 operator[] ,而不是在你的指针, 上vecPtr
。如果您没有取消引用,C++ 将尝试查找std::vector<int>*
类型上定义的 operator[] 。
This can get really confusing, since operator[]
is defined for all pointer types, but it amounts to offsetting the pointer as though it pointed to an array of vector<int>
. If you'd really only allocated a single vector there, then for any index other than 0
, the expression evaluates to a reference to garbage, so you'll get either a segfault or something you did not expect.
这可能会变得非常混乱,因为它operator[]
是为所有指针类型定义的,但它相当于偏移指针,就好像它指向一个vector<int>
. 如果您真的只在那里分配了一个向量,那么对于除 之外的任何索引0
,表达式的计算结果为对垃圾的引用,因此您将得到段错误或您没有预料到的东西。
In general, accessing vectors through a pointer is a pain, and the (*vecPtr)[index]
syntax is awkward (but better than vecPtr->operator[](index)
). Instead, you can use:
一般来说,通过指针访问向量很痛苦,而且(*vecPtr)[index]
语法很笨拙(但比 好vecPtr->operator[](index)
)。相反,您可以使用:
vecPtr->at(index)
This actually checks ranges, unlike operator[]
, so if you don't want to pay the price for checking if index is in bounds, you're stuck with (*vecPtr)[]
.
这实际上检查范围,与 不同operator[]
,因此如果您不想为检查索引是否在边界内付出代价,那么您将被困在(*vecPtr)[]
.