Use std::uniform_int_distribution and define its range later

后端 未结 3 939
遇见更好的自我
遇见更好的自我 2020-12-19 18:23

I have problem where I want to create a std::uniform_int_distribution in a struct and then give its range later. Below is what I want.

#include          


        
3条回答
  •  醉梦人生
    2020-12-19 18:50

    You can just do

    group.dis = std::uniform_int_distribution<>(0,19);
    

    or

    group.dis.param(std::uniform_int_distribution<>::param_type(0,19));
    

    Another way would be to add a method to your struct

    struct group_s {
        int k;
        std::uniform_int_distribution<> dis;
        void set(int a, int b) { dis = std::uniform_int_distribution<>(a,b); }
    } group;
    
    
    group.set(0,19);
    

提交回复
热议问题