Convert a vector to initializer_list

前端 未结 6 1906
时光取名叫无心
时光取名叫无心 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 01:56

    If you don't mind copies, then I think something like this would work:

    template
    using iterator_init_list = std::initializer_list::value_type>;
    
    template
    iterator_init_list to_initializer_list(Iterator start, Iterator last, Ts... xs)
    {
        if (start == last) return iterator_init_list{xs...};
        else return to_initializer_list(start+1, last, xs..., *start);
    }
    

提交回复
热议问题