Back_inserter or push_back

Just a quick question. What is better to use to add a line to the end of vector<string> , back_inserter or push_back ? basically, which is faster (I work with huge data, so the marginal difference is really important) and what are the main differences?

+4
source share
1 answer

These 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 will not be an option in this case. for instance

 std::vector<std::string> a(100, "Hello, World"); std::vector<std::string> b; std::copy(a.begin(), a.end(), std::back_inserter(b)); 
+8
source

All Articles