Does std::vector Copy Objects When Appending Using push_back?
After meticulous investigation using Valgrind, some users have reached the conclusion that std::vector creates copies of objects pushed into the container using the push_back() method. This has raised questions about the vector's ability to hold references or pointers without making copies.
std::vector Behavior
The assumption is correct. When an object is pushed_back() into a std::vector
Alternatives for Storing References or Pointers
If you intend to store references or pointers to objects in your vector, the solution is to use a std::vector
Maintaining Object Validity
When using a std::vector
The above is the detailed content of Does `std::vector` Copy Objects When Using `push_back()`?. For more information, please follow other related articles on the PHP Chinese website!