- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 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);
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.
与 gcc
或 clang
不同,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/
我是一名优秀的程序员,十分优秀!