给定一堆字符串,我正在尝试创建一个程序,它可以模仿伪随机行为,并根据我的输入进行加权分布。
到目前为止,我想出了这个
#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,但我正在尝试用std::vector实现它,这是我发现的主要区别。
使用Clang++时,错误的输出为
/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);
~~~~~~~^~~~~~~~~但是我不能理解它,因为在我的代码中没有显式的.push_back,我也不能从它得到什么,因为调试一个模板化的类是一场噩梦,我只是从这个开始。
有人知道为什么代码不能工作吗?
发布于 2013-05-13 09:25:01
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";
}
...发布于 2013-05-13 09:29:40
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&&}’看见?
未知参数1从‘std::basic_string’到‘std::vector::value_type&& {aka double&&}’的转换
什么都说出来了。
https://stackoverflow.com/questions/16513628
复制相似问题