Using emplace_back
function is way better than any other method since it creates an object in-place of type T
where vector<T>
, whereas push_back
expects an actual value from you.
vector<pair<string,double>> revenue;
// make_pair function constructs a pair objects which is expected by push_back
revenue.push_back(make_pair("cash", 12.32));
// emplace_back passes the arguments to the constructor
// function and gets the constructed object to the referenced space
revenue.emplace_back("cash", 12.32);