Convert a vector to initializer_list

前端 未结 6 1964
时光取名叫无心
时光取名叫无心 2021-01-31 01:26

Everyone creates std::vector from std::initializer_list, but what about the other way around?

eg. if you use a std::initializer_list

6条回答
  •  误落风尘
    2021-01-31 02:16

    I think that the best solution without templating obscure iterator classes for the good of passing vector using its two methods returning iterators is just to implement your function logic in a function taking vector.

    void someThing(std::initializer_list items)
    {
         std::vector v;
         for(int i:items)
         {
                 v.push_back(i);
         }
         someThing(v);
    }
    
    void someThing(std::vector items)
    {
    ...
    }
    

提交回复
热议问题