Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back_inserter or push_back

Tags:

c++

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?

like image 914
eNtiTy Avatar asked Apr 12 '13 15:04

eNtiTy


1 Answers

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));
like image 64
juanchopanza Avatar answered Oct 17 '22 13:10

juanchopanza