Is std::vector copying the objects with a push_back?

后端 未结 8 1753
夕颜
夕颜 2020-11-28 00:46

After a lot of investigations with valgrind, I\'ve made the conclusion that std::vector makes a copy of an object you want to push_back.

Is that really true ? A vect

8条回答
  •  一个人的身影
    2020-11-28 01:30

    Yes, std::vector::push_back() creates a copy of the argument and stores it in the vector. If you want to store pointers to objects in your vector, create a std::vector instead of std::vector.

    However, you need to make sure that the objects referenced by the pointers remain valid while the vector holds a reference to them (smart pointers utilizing the RAII idiom solve the problem).

提交回复
热议问题