为什么以及何时通过引用传递向量?
我有一个问题,为什么要通过引用传递向量或字符串。为什么要通过引用传递值? 让我们举个例子:- 假设我想在函数中传递一个向量,那么我会编写下面的代码:-
void fun(vector<string/int>v){
......
......
}
所以有时它会给出正确的输出,但有时它会给出错误的输出。
但是当我使用:-
void fun(vector<string/int> & v){
......
......
}
那么错误的输出就会变成正确的 那么请告诉我何时使用 & 以及为什么?
提前谢谢你...
I have a question that why to pass vector or string by reference. simply why to pass a value by reference?
let's take a example :-
suppose I want pass a vector in function then I'd write below code:-
void fun(vector<string/int>v){
......
......
}
so sometime it gives correct output but sometime it gives wrong.
but when I use:-
void fun(vector<string/int> & v){
......
......
}
then wrong output turns right
so tell me when to use & and why ?
thank u in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当需要间接寻址并且不需要指针时,应该使用引用。
您可能需要间接
因为:
You should use a reference when you need indirection, and you don't need a pointer.
You may need indirection if
Because: