Does std::vector Copy Objects with push_back?
After conducting extensive investigations using Valgrind, it has been suggested that std::vector creates a copy of any object pushed back into the vector. Is this assertion accurate? Can a vector not maintain a reference or pointer to an object without copying it?
Answer:
Yes, std::vector
However, you must guarantee that the objects referenced by the pointers remain valid while the vector maintains a reference to them. Smart pointers based on the RAII idiom offer a solution to this problem.
The above is the detailed content of Does `std::vector::push_back()` Copy Objects?. For more information, please follow other related articles on the PHP Chinese website!