[英]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, eg 如果我有一个指向矢量的指针,例如
std::vector<a> *A;
I can get to its members using the short ->
operator, but indexing is very inconvenient, ie (*A)[i]
. 我可以使用short
->
运算符到达其成员,但索引非常不方便,即(*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. 注意:我对
A->at()
不满意,因为它进行边界检查,这很慢,对我来说速度很重要。
Bind it to a reference is the easiest way if (*A)[i]
is a problem: 如果
(*A)[i]
是一个问题,将它绑定到引用是最简单的方法:
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. 通常我宁愿通过引用而不是指针传递向量,除非你真的想要允许
NULL
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.