How to fill an array of unique_ptr?

前端 未结 3 549
眼角桃花
眼角桃花 2020-12-31 22:21

Is it possible to use std:fill to fill an array of unique_ptrs? The intention is to have distinct pointers to distinct objects which are initialize

3条回答
  •  别那么骄傲
    2020-12-31 22:49

    I don't think you can do it with std::fill() but it is trivial to do with std::generate():

    std::unique_ptr array[10];
    std::generate(std::begin(array), std::end(array),
                  []{ return std::unique_ptr(new int(17)); });
    

提交回复
热议问题