Just a quick question - Which is better to use to add a string to the end of a vector<string>
, back_inserter
or push_back
? mainly, which works faster(I'm working with a huge data, so the marginal difference is actually important) and what are the main differences?
The two are not equivalent. You use std::back_inserter
for example when you need to pass an input iterator to an algorithm. std::vector<std::string>::push_back
would not be an option in this case. For example
std::vector<std::string> a(100, "Hello, World");
std::vector<std::string> b;
std::copy(a.begin(), a.end(), std::back_inserter(b));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With