The shown code snippet, defines a member function named twoSum that has the return type of vector<int> and has 2 parameters. The first parameter named nums is an lvalue reference to a non-const vector<int> while the second parameter named target is an int.
I was wondering what the meaning of the & is
The & in the first parameter nums of the member function means that nums is an lvalue reference to a non-const vector<int>. Meaning, the argument that will be passed to the nums parameter, will be passed by reference instead of passed by value. That is, inside the member function twoSums, the nums refer to the original vector<int> that is passed as an argument.
Also note that there should be a return statement inside the member function since the return type of the member function is non-void.
发布评论
评论(1)
显示的代码片段定义了一个名为
twoSum
的成员函数,其返回类型为vector
并具有2 个参数< /强>。第一个名为nums
的参数是对非常量vector
的左值引用,而第二个参数名为target code> 是一个
int
。成员函数的第一个参数 。这意味着,将传递给
nums
中的&
表示nums
是一个对非常量的左值 代码>向量nums
参数的参数将通过引用传递,而不是通过值传递。也就是说,在成员函数twoSums
内,nums
引用作为参数传递的原始vector
。另请注意,成员函数内部应该有 return 语句,因为成员函数的返回类型是非 void。
The shown code snippet, defines a member function named
twoSum
that has the return type ofvector<int>
and has 2 parameters. The first parameter namednums
is an lvalue reference to a non-constvector<int>
while the second parameter namedtarget
is anint
.The
&
in the first parameternums
of the member function means thatnums
is an lvalue reference to a non-constvector<int>
. Meaning, the argument that will be passed to thenums
parameter, will be passed by reference instead of passed by value. That is, inside the member functiontwoSums
, thenums
refer to the originalvector<int>
that is passed as an argument.Also note that there should be a return statement inside the member function since the return type of the member function is non-void.