Adding to a vector of pair

后端 未结 10 1419
悲哀的现实
悲哀的现实 2021-01-30 05:19

I have a vector of pair like such:

vector> revenue;

I want to add a string and a doub

10条回答
  •  遥遥无期
    2021-01-30 05:31

    Using emplace_back function is way better than any other method since it creates an object in-place of type T where vector, whereas push_back expects an actual value from you.

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

提交回复
热议问题