Initialize a vector of pairs in one line

后端 未结 2 1297
独厮守ぢ
独厮守ぢ 2020-12-21 18:36

I want to initialize a std::vector (of std::pair), with k objects, with the pair of values shown below.

Here is my attempt:

// int k
std         


        
相关标签:
2条回答
  • 2020-12-21 18:50

    You can just switch to { } around the constructor arguments...

    std::vector<std::pair<Point::FT, int>> v{k, {std::numeric_limits<FT>::max(), -1}};
    

    See it running here

    0 讨论(0)
  • 2020-12-21 19:01

    Assuming Point::FT is something for which numeric_limits::max() is valid,

    std::vector <std::pair<Point::FT, int>>
            v(k, std::make_pair(std::numeric_limits<FT>::max(), -1));
    
    0 讨论(0)
提交回复
热议问题