Does this copy the vector?

前端 未结 3 2007
夕颜
夕颜 2021-01-14 12:38

If I have the following code, is the vector copied?

std::vector x = y.getTheVector();

or would it depend on whether the return t

3条回答
  •  轮回少年
    2021-01-14 13:13

    std::vector x = y.getTheVector();
    

    Your first example does indeed copy the vector, regardless of whether the "getTheVector" function returns a vector or a reference to a vector.

    std::vector& x = y.getTheVector();
    

    In your second example, however, you are creating a reference, so the vector will NOT be copied.

提交回复
热议问题