[英]Problems with std::piecewise_constant_distribution and std::vector
给定一堆字符串,我试图创建一个程序,该程序可以模拟基于输入的加权分布的伪随机行为。
到目前为止,我想到了这个
#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
,我也无法从中得到.push_back
,因为调试模板化类是一场噩梦,而我只是从这个开始。
任何人都知道为什么代码不起作用?
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::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";
}
...
template< class RealType = double >
class piecewise_constant_distribution;
对RealType
进行操作,不能对字符串进行操作。 看这里
产生随机浮点数,这些浮点数均匀分布在几个子间隔[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 &&}'的已知转换
告诉一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.