Vector indexing for short
Suppose I have a vector
std::vector<a> A;
I can get access to its member-functions through the .
operator and I can index it with the []
operator. If I have a pointer to a 开发者_开发百科vector, e.g.
std::vector<a> *A;
I can get to its members using the short ->
operator, but indexing is very inconvenient, i.e. (*A)[i]
. How can it be written more neatly? Note: I am not satisfied with A->at()
, because it does boundary check, which are slow, and for me speed is important.
Bind it to a reference is the easiest way if (*A)[i]
is a problem:
std::vector<a>& ref = *A;
ref[i] = 0; //use reference
Normally I'd prefer passing vectors by reference instead of pointer anyway unless you really want to allow NULL
values too.
精彩评论