'random_shuffle': is not member of 'std' 错误
'random_shuffle': is not a member of 'std' error
我正在尝试使用 std::random_shuffle,但出现编译错误。
我的编译器是 v140(Visual Studio 2015),我在 x64 上工作,发布模式。
我的代码:
#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.
Visual Studio C++ 标准版。
与gcc
或clang
不同,Visual Studio不提供select标准版本的选项,因此弹出的问题是:VS 实现了哪个 C++ 标准? 答案是……两者都不是……每个都有一点。最后我检查了他们的理念是他们努力慢慢获得最新的标准版本,但不是在标准版本步骤中,也就是说他们正在实现 C++14 功能,而 C+11 还没有完全实现。所以你会看到 C++11 的一些部分实现了,C++14 的一些部分实现了,C++17 的一些部分实现了。
使用工具链 v140 的默认设置可以很好地编译代码。但是,如果您添加选项 /std:c++latest
,标准库中删除的部分可能不再可用。
可能是为了尽可能接近 C++17。
TLDR:
我在 Visual Studio 遇到了这个错误。
要解决此问题,只需在 Properties > C/C++ > Command Line
中设置 /Zc:__cplusplus
标志即可。
详细说明:
我在尝试使用 Hayai 时遇到了这个问题。在 visual studio 中,__cplusplus
总是设置为 199711L
并且 Hayai
也检查这个总是在 visual studio 中失败,因为这个:
/// Randomly shuffles the order of tests.
static void ShuffleTests()
{
Benchmarker& instance = Instance();
#if __cplusplus > 201100L
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(instance._tests.begin(),
instance._tests.end(),
g);
#else
std::random_shuffle(instance._tests.begin(),
instance._tests.end());
#endif
}
正如您在 visual studio 中看到的,由于这个问题,它总是分支到错误的片段中,因此失败。
要解决此问题,只需在 Properties > C/C++ > Command Line
中设置 /Zc:__cplusplus
标志即可。
来自微软:
The /Zc:__cplusplus
compiler option enables the __cplusplus
preprocessor macro to report an updated value for recent C++ language
standards support. By default, Visual Studio always returns the value
"199711L" for the __cplusplus
preprocessor macro.
Remarks
The __cplusplus
preprocessor macro is commonly used to report support
for a particular version of the C++ standard. Because lots of existing
code appears to depend on the value of this macro matching "199711L",
the compiler does not change the value of the macro unless you
explicitly opt-in by using the /Zc:__cplusplus
compiler option. The
/Zc:__cplusplus
option is available starting in Visual Studio 2017
version 15.7, and is off by default. In earlier versions of Visual
Studio, and by default, or if /Zc:__cplusplus
- is specified, Visual
Studio returns the value "199711L" for the __cplusplus preprocessor
macro. The /permissive
- option does not enable /Zc:__cplusplus
.
When the /Zc:__cplusplus
option is enabled, the value reported by the
__cplusplus
macro depends on the /std
version switch setting. This table shows the possible values for the macro:
确保阅读实际文章Here
我正在尝试使用 std::random_shuffle,但出现编译错误。
我的编译器是 v140(Visual Studio 2015),我在 x64 上工作,发布模式。
我的代码:
#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.
Visual Studio C++ 标准版。
与gcc
或clang
不同,Visual Studio不提供select标准版本的选项,因此弹出的问题是:VS 实现了哪个 C++ 标准? 答案是……两者都不是……每个都有一点。最后我检查了他们的理念是他们努力慢慢获得最新的标准版本,但不是在标准版本步骤中,也就是说他们正在实现 C++14 功能,而 C+11 还没有完全实现。所以你会看到 C++11 的一些部分实现了,C++14 的一些部分实现了,C++17 的一些部分实现了。
使用工具链 v140 的默认设置可以很好地编译代码。但是,如果您添加选项 /std:c++latest
,标准库中删除的部分可能不再可用。
可能是为了尽可能接近 C++17。
TLDR:
我在 Visual Studio 遇到了这个错误。
要解决此问题,只需在 Properties > C/C++ > Command Line
中设置 /Zc:__cplusplus
标志即可。
详细说明:
我在尝试使用 Hayai 时遇到了这个问题。在 visual studio 中,__cplusplus
总是设置为 199711L
并且 Hayai
也检查这个总是在 visual studio 中失败,因为这个:
/// Randomly shuffles the order of tests.
static void ShuffleTests()
{
Benchmarker& instance = Instance();
#if __cplusplus > 201100L
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(instance._tests.begin(),
instance._tests.end(),
g);
#else
std::random_shuffle(instance._tests.begin(),
instance._tests.end());
#endif
}
正如您在 visual studio 中看到的,由于这个问题,它总是分支到错误的片段中,因此失败。
要解决此问题,只需在 Properties > C/C++ > Command Line
中设置 /Zc:__cplusplus
标志即可。
来自微软:
The
/Zc:__cplusplus
compiler option enables the __cplusplus preprocessor macro to report an updated value for recent C++ language standards support. By default, Visual Studio always returns the value "199711L" for the__cplusplus
preprocessor macro.Remarks
The
__cplusplus
preprocessor macro is commonly used to report support for a particular version of the C++ standard. Because lots of existing code appears to depend on the value of this macro matching "199711L", the compiler does not change the value of the macro unless you explicitly opt-in by using the/Zc:__cplusplus
compiler option. The/Zc:__cplusplus
option is available starting in Visual Studio 2017 version 15.7, and is off by default. In earlier versions of Visual Studio, and by default, or if/Zc:__cplusplus
- is specified, Visual Studio returns the value "199711L" for the __cplusplus preprocessor macro. The/permissive
- option does not enable/Zc:__cplusplus
.When the
/Zc:__cplusplus
option is enabled, the value reported by the__cplusplus
macro depends on the/std
version switch setting. This table shows the possible values for the macro:
确保阅读实际文章Here