Are vectors passed to functions by value or by reference in C++

前端 未结 4 1420
时光取名叫无心
时光取名叫无心 2020-12-04 06:15

I\'m coding in C++. If I have some function void foo(vector test) and I call it in my program, will the vector be passed by value or reference? I\'m

4条回答
  •  半阙折子戏
    2020-12-04 06:35

    void foo(vector test)

    vector would be passed by value in this.

    You have more ways to pass vectors depending on the context:-

    1) Pass by reference:- This will let function foo change your contents of the vector. More efficient than pass by value as copying of vector is avoided.

    2) Pass by const-reference:- This is efficient as well as reliable when you don't want function to change the contents of the vector.

提交回复
热议问题