eNtiTy
eNtiTy

Reputation: 135

Back_inserter or push_back

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?

Upvotes: 11

Views: 10478

Answers (1)

juanchopanza
juanchopanza

Reputation: 227370

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));

Upvotes: 14

Related Questions