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

后端 未结 8 1724
夕颜
夕颜 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:22

    Yes, std::vector stores copies. How should vector know what the expected life-times of your objects are?

    If you want to transfer or share ownership of the objects use pointers, possibly smart pointers like shared_ptr (found in Boost or TR1) to ease resource management.

提交回复
热议问题