具体的C++函数参数的符号?

发布于 2025-01-20 16:37:19 字数 1399 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像你 2025-01-27 16:37:19

显示的代码片段定义了一个名为 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文