gpt4 book ai didi

c++ - 'random_shuffle' : is not a member of 'std' error

转载 作者:太空狗 更新时间:2023-10-29 19:38:27 27 4
gpt4 key购买 nike

我正在尝试使用 std::random_shuffle , 并得到一个编译错误。

我的编译器是 v140(Visual Studio 2015),我在 x64 上工作, Release模式。

我的代码:

#include <random>
#include <algorithm>
void foo()
{
std::vector<int> v;
std::random_shuffle(v.begin(), v.end());
}

我得到的错误:

error C2039: 'random_shuffle': is not a member of 'std'
error C3861: 'random_shuffle': identifier not found
  • 知道问题出在哪里吗?

谢谢!

最佳答案

random_shuffle 在 C++14 中被弃用,并在 C++17 中被完全移除。

你需要使用shuffle,它以一个随机生成器作为参数。

你可以在网站上看到一个简单的例子:

std::random_device rd;
std::mt19937 g(rd());

std::shuffle(v.begin(), v.end(), g);

n4190

Removing auto_ptr, random_shuffle(), And Old Stuff

...

III. What Must Die

D.12 "Random shuffle" [depr.alg.random.shuffle]

This defines random_shuffle(first, last) and random_shuffle(first, last, rng). (The latter takes a RandomNumberGenerator, whose requirements are totally different from C++11's UniformRandomNumberGenerator.)

The problem with random_shuffle(first, last) is that it's permitted to use rand(), which is permitted to be low quality. (rand() is even permitted to introduce data races, 26.8 [c.math]/5, although I'm not aware of any implementation that does so.) Constructing mt19937 urng and calling shuffle(first, last, urng) is a far superior alternative. Unlike random_shuffle(first, last), calling shuffle(first, last, urng) requires the user to be aware of the URNG's existence and state, but I argue that this is a feature, not a bug.

random_shuffle(first, last, rng) is the Knuth shuffle algorithm. It's not evil, but it's almost unusable in practice, because the "rng" function object is required to provide a very strange interface. (It's actually required to return a uniform integer distribution over a varying interval, which is difficult to do without introducing bias.) shuffle(first, last, urng) is vastly easier to use, because it directly consumes a URNG.


Visual Studio C++ 标准版。

gccclang 不同,Visual Studio 不提供选择标准版本的选项,因此弹出的问题是:哪个 C++ 标准VS 实现了吗? 答案是……两者都不是……而且每个都有一点。最后我检查了他们的理念是他们努力慢慢获得最新的标准版本,但不是在标准版本步骤中,也就是说他们正在实现 C++14 功能,而 C+11 还没有完全实现。因此,您将看到 C++11 的某些部分已实现,C++14 的某些部分已实现,C++17 的某些部分已实现。

关于c++ - 'random_shuffle' : is not a member of 'std' error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45013977/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com