In old c++ style, I always used vector < shared_ptr < string> >
or vector < string* >
to avoid memory copy when constructing a big vector
which holds many string
objects.
Since c++11, c++ has rvalue
and move
semantics; can I use vector < string >
now?
I am using gcc 7.1.0 and clang 3.6 with the c++ 14 option.
There are several situations where using vector<shared_ptr<string>>
or vector<string*>
could help to optimize performance before C++11:
std::string
has non-throwing move constructor, so in push_back() for std::vector<string>
should be not slower then for std::vector<std::shared_ptr<string>>
. std::vector<string>
is appropriate too. If your intention is to return copy of the collection but not to copy the elements - that is the only way where std::vector<shared_ptr<string>>
can still help. But in this case my advice is to share immutable objects between collections, ie use std::vector<shared_ptr<const string>>
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.