Adding to a vector of pair

后端 未结 10 1417
悲哀的现实
悲哀的现实 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:46

    As many people suggested, you could use std::make_pair.

    But I would like to point out another method of doing the same:

    revenue.push_back({"string",map[i].second});
    

    push_back() accepts a single parameter, so you could use "{}" to achieve this!

    0 讨论(0)
  • 2021-01-30 05:50
    revenue.pushback("string",map[i].second);
    

    But that says cannot take two arguments. So how can I add to this vector pair?

    You're on the right path, but think about it; what does your vector hold? It certainly doesn't hold a string and an int in one position, it holds a Pair. So...

    revenue.push_back( std::make_pair( "string", map[i].second ) );     
    
    0 讨论(0)
  • 2021-01-30 05:54

    Or you can use initialize list:

    revenue.push_back({"string", map[i].second});
    
    0 讨论(0)
  • 2021-01-30 05:57

    Read the following documentation:

    http://cplusplus.com/reference/std/utility/make_pair/

    or

    http://en.cppreference.com/w/cpp/utility/pair/make_pair

    I think that will help. Those sites are good resources for C++, though the latter seems to be the preferred reference these days.

    0 讨论(0)
提交回复
热议问题