std::piecewise_stant_distribution和std::vector的问题

Problems with std::piecewise_constant_distribution and std::vector

本文关键字:std 问题 vector stant piecewise distribution      更新时间:2023-10-16

给定一堆字符串,我正试图创建一个程序,该程序可以根据我的输入模拟具有加权分布的伪随机行为。

到目前为止,我想出了这个

#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,我也不了解它的来源,因为调试模板化的类是一场噩梦,我只是从这个开始。

有人知道为什么代码不起作用吗?

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";
}
...
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&amp;{aka double&&}'

告诉一切。