如何使用 uint64 整数对数组进行洗牌?

问题描述 投票:0回答:1

我想在 C++ 中使用 std::uin64_t 整数对数组进行洗牌。

void shuffle()
{
        std::array<char, 255> list     ({'a', 'b', 'c'}); // The list

    const std::mt19937_64 engine   (std::uint_64);   // The engine

    std::shuffle(this->list.begin(), this->listist.end(), engine);
}

但它无法编译,这是错误的第一个错误代码

error C2794: 'type': is not a member of any direct or indirect base class of 'std::_Invoke_traits_zero<void,_Urng &>'
c++ arrays list compiler-errors shuffle
1个回答
0
投票

这就是我让它在我的电脑上运行的方法。我已经发表评论来解释一切。

#include <iostream>  
#include <array>
#include <random>
#include <algorithm>

void shuffle(std::uint64_t seed) {                                              
// This allows more flexibility but note that void shuffle ≠ std::shuffle
    std::array<char, 255> list = {'a', 'b', 'c'}; //your list of whatever

    std::mt19937_64 engine(seed); //remember the seed is the uint param

    std::shuffle(list.begin(), list.end(), engine); //no change

    for (const auto &c : list) { //I put auto in case you have other ideas for the list's contents
        std::cout << c;
    }
    
}

int main() {
    std::uint64_t seed = 123456789;
    shuffle(seed)        //call our function (it works)
    return 0;                       
}
© www.soinside.com 2019 - 2024. All rights reserved.