Error: 'shuffle' is not a member of 'std'

Error: 'shuffle' is not a member of 'std'

使用 C++17,每当我尝试使用 std::shuffle 时,我都会收到错误消息:

'error: 'shuffle' is not a member of 'std''

我正在使用 Eclipse C++ IDE 和 MinGW 8.2,并且可以很好地使用其他函数,例如 mt19937、字符串等。这是它一直起作用的代码:

#include "MyHeader.h"
#include <random>
using namespace std;

void generateDictionary() {
    int initialAlphabet[26];
    for (int i = 0; i < 26; i++) {
        initialAlphabet[i] = i;
    }

    std::mt19937 randomSeed(std::random_device{}());
    std::shuffle(initialAlphabet, initialAlphabet+26, randomSeed);
}

std::shuffle 来自算法

添加

#include <algorithm>

这应该可以解决您的问题

std::shuffle 包含在 <algorithm>

我在尝试使用 random_shuffle 时也很头疼,后来意识到它已被弃用。