鉴于一堆字符串,我正在尝试创建一个程序,可以根据我的输入模拟加权分布的伪随机行为。
到目前为止,我想出了这个
#include <iostream>
#include <random>
#include <type_traits>
#include <map>
#include <vector>
#include <string>
#include <initializer_list>
#define N 100
int main()
{
std::vector<std::string> interval{"Bread", "Castle", "Sun"};
std::vector<float> weights { 0.40f, 0.50f, 0.10f };
std::piecewise_constant_distribution<> dist(interval.begin(),
interval.end(),
weights.begin());
std::random_device rd;
std::mt19937 gen(rd()) ;
for(int i = 0; i<N;i++)
{
std::cout << dist(gen) << "\n";
}
return(0);
}
但是这个东西不起作用,我不知道为什么,std::piecewise_constant_distribution
的通常用法,根据在线示例,它是std::arrays
,但我正在尝试使用{来实现它{1}},这是我找到的主要区别。
使用Clang ++,错误输出为
std::vector
但是我无法理解它,因为我的代码中没有明确的/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/random.tcc:2409:10: error: no matching member function for
call to 'push_back'
_M_int.push_back(*__bbegin);
~~~~~~~^~~~~~~~~
,我也无法从中得到什么,因为调试一个模板化的类这是一场噩梦而我刚刚开始这个。
任何人都知道为什么代码不起作用?
答案 0 :(得分:3)
std::piecewise_constant_distribution
的默认结果类型为RealType
(此处为double
)。您似乎尝试从3个string
选项中选择不同的权重,但这不是std::piecewise_constant_distribution
的用途。它旨在使用给定的权重从数字区间生成均匀的随机值。如果您修改示例并将interval
更改为:
std::vector<double> interval {1, 3, 5, 10, 15, 20};
一切都会愉快地编译。根据它的外观,您需要std::discrete_distribution
:
...
std::vector<std::string> interval{"Bread", "Castle", "Sun"};
std::vector<float> weights { 0.40f, 0.50f, 0.10f };
std::discrete_distribution<> dist(weights.begin(), weights.end());
std::mt19937 gen(rd());
for(int i = 0; i<N;i++)
{
std::cout << interval[dist(gen)] << "\n";
}
...
答案 1 :(得分:1)
template< class RealType = double >
class piecewise_constant_distribution;
在RealType
上运行,无法对字符串进行操作。 see here
生成随机浮点数,它们是统一的 分布在几个子区间[bi,bi + 1]中的每一个中 有自己的重量wi。
将其更改为:
std::vector<float> weights { 0.40f, 0.50f, 0.10f };
std::discrete_distribution<> dist(weights.begin(), weights.end());
std::mt19937 gen(rd());
for(int i = 0; i<N;i++)
{
std::cout << interval[dist(gen)] << "\n";
}
你已经完成了。
忠告: 读取朋友编译器生成的消息
In file included from /usr/include/c++/4.7/random:51:0,
from prog.cpp:2:
/usr/include/c++/4.7/bits/random.tcc: In instantiation of ‘std::piecewise_constant_distribution<_RealType>::param_type::param_type(_InputIteratorB, _InputIteratorB, _InputIteratorW) [with _InputIteratorB = __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >; _InputIteratorW = __gnu_cxx::__normal_iterator<float*, std::vector<float> >; _RealType = double]’:
/usr/include/c++/4.7/bits/random.h:4940:39: required from ‘std::piecewise_constant_distribution<_RealType>::piecewise_constant_distribution(_InputIteratorB, _InputIteratorB, _InputIteratorW) [with _InputIteratorB = __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >; _InputIteratorW = __gnu_cxx::__normal_iterator<float*, std::vector<float> >; _RealType = double]’
prog.cpp:17:64: required from here
/usr/include/c++/4.7/bits/random.tcc:2407:3: error: no matching function for call to ‘std::vector<double>::push_back(std::basic_string<char>&)’
/usr/include/c++/4.7/bits/random.tcc:2407:3: note: candidates are:
In file included from /usr/include/c++/4.7/vector:65:0,
from /usr/include/c++/4.7/bits/random.h:34,
from /usr/include/c++/4.7/random:50,
from prog.cpp:2:
/usr/include/c++/4.7/bits/stl_vector.h:881:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = double; _Alloc = std::allocator<double>; std::vector<_Tp, _Alloc>::value_type = double]
/usr/include/c++/4.7/bits/stl_vector.h:881:7: note: no known conversion for argument 1 from ‘std::basic_string<char>’ to ‘const value_type& {aka const double&}’
/usr/include/c++/4.7/bits/stl_vector.h:899:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = double; _Alloc = std::allocator<double>; std::vector<_Tp, _Alloc>::value_type = double]
/usr/include/c++/4.7/bits/stl_vector.h:899:7: note: no known conversion for argument 1 from ‘std::basic_string<char>’ to ‘std::vector<double>::value_type&& {aka double&&}’
见
从'std :: basic_string'到参数1没有已知的转换 “的std ::矢量:: VALUE_TYPE&安培;&安培; {aka double&amp;&amp;}'
告诉一切。