Class构造函数"eating"向上值

Class constructor "eating" up values

所以我正在尝试 运行 这个 sim 程序 class 让我们使用集合建立赌注 class。

这是 class 定义:

class Bet2{
private:
    set<int> mainNumbers;
    set<int> luckyNumbers;
public:
    Bet2();
    void show() const;
    set<int> getMainNumbers();
    set<int> getLuckyNumbers();
};

所以我决定使用 运行dom 库,因为他们在 class 中给我们的 运行d() 函数在创建一堆时吐出相同的值Bet2 objects for the sim.

但是,出于某种原因,它没有吐出它应该吐出的值的数量。有时它会吐出 4 个主号码(而不是 5 个),或者只是 1 个幸运号码(而不是 2 个)

构造函数的代码如下:

Bet2::Bet2() {
    random_device rd;
    uniform_int_distribution<int> main(1, 50);
    for (int i = 0; i < 5; i++)
        mainNumbers.insert(main(rd));
    uniform_int_distribution<int> star(1, 12);
    for (int i = 0; i < 2; i++)
        luckyNumbers.insert(star(rd));
}

我运行在主函数中使用uniform_int_distribution和random_device进行了一些测试,运行没有任何问题。出于某种原因,当我为我的 sim 初始化 Bet2 向量时它会消耗值:

Main Numbers: 11 23 27 32 36
Star Numbers: 3 11
Main Numbers: 4 18 22 27 28
Star Numbers: 9 11
Main Numbers: 3 5 25 43      <-
Star Numbers: 1              <-
Main Numbers: 40 42 43 46 50
Star Numbers: 2 7
Main Numbers: 7 10 14 27 45
Star Numbers: 9 10
Main Numbers: 11 15 21 24 35
Star Numbers: 1 11
Main Numbers: 3 25 29 45 50
Star Numbers: 3 7
Main Numbers: 11 15 23 25 37
Star Numbers: 1 6
Main Numbers: 7 8 26 31 43
Star Numbers: 6 9
Main Numbers: 15 27 36 38 39
Star Numbers: 2 8

试图找出 uniform_int_distribution 无法生成值,但没有在网上找到任何东西。

提前致谢!

std::set 最多只能存储给定值的 1 个副本。

缺少数字应该是因为随机数恰好变成了和之前看到的数字一样

如果要存储相同值的倍数,则应使用 std::multiset

如果您想生成一组唯一的已定义值,最好先为此生成一个 std::vector of candidate values, and then use std::sample()