了解随机数生成器调用中的C 参数
Understanding C++ Parameters in random number generator call
我正在经过一本名为掌握C 多线程的书中的一些示例,并且我遇到了一些我不完全理解的代码。
在此函数中
int randGen(const int& min, const int& max){
static thread_local mt19937 generator(hash<thread::id>() (this_thread::get_id()));
uniform_int_distribution<int> distribution(min, max);
return distribution(generator);
}
我不明白的代码是发电机函数调用
中的参数hash<thread::id>() (this_thread::get_id())
hash<thread::id>()
是从 this_thread::get_id()
中获取返回值的功能吗?
任何帮助将不胜感激,或者如果我需要提供更多信息。请大喊。
使用hash<thread::id>()
您创建std::hash
类模板的对象。
然后,您将该对象称为operator()
函数,将this_thread::get_id()
传递为参数。
如果我们将其拆分,则可能更容易理解:
hash<thread::id> my_hash; // Create object
my_hash(this_thread::get_id()); // Use the function call operator
最后一个使用函数调用操作员,等于
my_hash.operator()(this_thread::get_id()); // Use the function call operator
然后将函数调用操作员的结果用作generator
对象的构造函数的参数。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 制作具有平均值的随机数生成器
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 尝试在 c++ 中创建随机数生成器并收到错误
- 存储并重新加载随机数生成器状态
- 作为随机数生成器的争用条件
- 播种随机数生成器以使用分叉的好方法是什么?
- 随机数生成器的性能因平台而异
- 随机数生成器的种子和状态有什么区别?
- RcppShark 随机森林示例引发有关随机数生成器的异常
- C++多个实例的随机数生成器组种子中增强
- C++随机数生成器仅生成 0 - C++ 11
- GSL+OMP:C++中的线程安全随机数生成器
- 贪吃蛇游戏 - 食物瓷砖的随机数生成器
- 随机数生成器在 C++ 用于 skipList.
- RSA 加密和解密期间的随机数生成器要求
- 如何使用 cpp 设置随机数生成器的语法?
- 传递参数、参数、交换函数和随机数生成器
- 了解随机数生成器调用中的C 参数
- 更改随机数生成器的参数