我正在尝试使用gcc版本6.3.0使用以下命令编译包含std::sample
的这段c ++ 17代码:g++ -std=gnu++17 -c main.cpp
。
但我明白了:error: ‘sample’ is not a member of ‘std’
......
#include <vector>
#include <algorithm>
#include <random>
int main()
{
std::vector<int> a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> b(5);
std::sample(a.begin(), a.end(),
b.begin(), b.size(),
std::mt19937{std::random_device{}()});
return 0;
}
gcc 6是否支持使用std::sample
? (用gcc 8.2.0编译好)
我在这两页上找不到答案:
是的,因为GCC 5,但直到GCC 7它在std::experimental
命名空间并在<experimental/algorithm>
标题中定义。
来自GCC 5发行说明:
运行时库(libstdc ++)
- 改进了对图书馆基础知识TS的实验支持,包括: 函数模板std :: experimental :: sample;
在GCC 5.1 https://wandbox.org/permlink/HWnX3qSgKbZO2qoH上测试
我们可以从table in the documentation的“Library Fundamentals V1 TS Components:Sampling”中看到,最早版本的libstdc ++支持std::sample
是版本7.1
gcc 6是否支持使用std :: sample?
不。你需要GCC 7.来自GCC 7 release notes:
- 对C ++ 17的实验支持,包括以下新功能: ... std :: sample,std :: default_searcher,std :: boyer_moore_searcher和std :: boyer_moore_horspool_searcher;
对于GCC 7,您可能需要-std=c++1z
或-std=gnu++1z
,因为它是实验性的。