开发者

Vector indexing for short

开发者 https://www.devze.com 2023-02-11 12:01 出处:网络
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 开发者_开发百科vecto

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号