[英]Can someone explain what is happening on line 3?
vector<long> v = {1,2,3};
long i = v.size();
const long* w = (i != 0) ? &v.front() : NULL;
Can someone explain what is happening on line 3?有人可以解释第 3 行发生了什么吗? To me, it feels like v = w.
对我来说,感觉就像 v = w。 Am I understanding it right?
我理解对了吗?
e1? v1: v2
That's ?:
expression where when expression ( e1
) has true, it returns v1
, else returns v2
.那是
?:
表达式,当表达式 ( e1
) 为真时,它返回v1
,否则返回v2
。
Here it means point constant pointer (cannot change address once assigned address) to NULL
if v
has no value inside.这里它表示指向
NULL
的常量指针(一旦分配地址就不能更改地址)如果v
内部没有值。 Or the first element's address if v
has at least one element inside.或者如果
v
内部至少有一个元素,则为第一个元素的地址。
it feels like v = w.
感觉就像 v = w。
No, not at all.一点都不。
v
is a std::vector<long>
(assuming vector
is std::vector
) and w
is a const long*
. v
是std::vector<long>
(假设vector
是std::vector
), w
是const long*
。 Thats two completely unrelated types.那是两种完全不相关的类型。 You cannot assign one to the other.
您不能将一个分配给另一个。
Actually your code is similar to:实际上您的代码类似于:
vector<long> v = {1,2,3};
const long* w = &v[0];
w
is a pointer to the first element in the vector. w
是指向向量中第一个元素的指针。 v.front
is just a different way to get a reference to the first element. v.front
只是获取对第一个元素的引用的另一种方式。 And because in general we cannot know if v
has an element at index 0
, the author added a check:而且因为通常我们不知道
v
是否在索引0
处有一个元素,所以作者添加了一个检查:
vector<long> v = {1,2,3};
long i = v.size();
const long* w;
if ( v.size() != 0) w = &v.front();
else w = nullptr;
And a less verbose way of writing the same is using the conditional operator:一个不太冗长的写法是使用条件运算符:
const long* w = ( v.size() != 0 ) ? &v.front() : nullptr;
Depending on the condition, only one side of :
is evaluated, hence out-of-bounds access to element 0
in an empty vector can be avoided.根据条件,仅评估
:
的一侧,因此可以避免对空向量中的元素0
的越界访问。
"const long* w = (i?= 0). &v:front(); NULL. " It checks whether vector has elements, If vector has elements. “const long* w = (i?= 0). &v:front(); NULL.” 检查向量是否有元素,如果向量有元素。 it assigns first elements address to long type pointer?
它将第一个元素地址分配给长类型指针? .
. is ternary operator;
是三元运算符; example int a=1;int b=2?
例如 int a=1;int b=2? (a<b): "if true do something";
(a<b): "如果是真的做某事"; "if false do something" ;
“如果假做某事”;
const long* w = (i?= 0). &v:front(); NULL;
w
is a pointer to a long
constant - not to be confused with a const
pointer to a long
. w
是指向long
常量的指针 - 不要与指向long
的const
指针混淆。 The value of the pointer is being initialized to the result of the ternary expression: (i?= 0). &v:front(); NULL;
指针的值被初始化为三元表达式的结果:
(i?= 0). &v:front(); NULL;
(i?= 0). &v:front(); NULL;
The?: operator returns one of two values depending on the result of an expression.
?: 运算符根据表达式的结果返回两个值之一。
Source: http://www.cplusplus.com/articles/1AUq5Di1/来源: http://www.cplusplus.com/articles/1AUq5Di1/
If (i != 0)
is true then the result will be the address of the reference returned by v.front()
(ie: the first element in the vector v
).如果
(i != 0)
为真,那么结果将是v.front()
返回的引用地址(即:向量v
中的第一个元素)。 If the expression evaluates false then w
will be a null pointer.如果表达式计算结果为 false,则
w
将是 null 指针。
A good rule to follow to ensure you understand any declaration properly is The "Clockwise/Spiral Rule" - understanding this rule can save you some headaches.确保您正确理解任何声明的一个很好的规则是“顺时针/螺旋规则” - 理解这条规则可以为您省去一些麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.