Initialize std::array with a range (pair of iterators)

后端 未结 3 780
北恋
北恋 2020-12-05 14:16

How can I initialize an std::array from a range (as defined by a pair of iterators)?

Something like this:

vector v;
...
// I kn         


        
3条回答
  •  被撕碎了的回忆
    2020-12-05 14:51

    You can use BOOST_PP_ENUM as:

    include 
    
    #define INIT(z, i, v) v[i] 
    
    std::vector v;
    
    //fill v with at least 5 items 
    
    std::array a = { BOOST_PP_ENUM(5, INIT, v) };  //MAGIC
    

    Here, the last line is expanded as:

    std::array a = {v[0], v[1], v[2], v[3], v[4]}; //EXPANDED LINE
    

    which is what you want.

提交回复
热议问题