std::piecewise_stant_distribution和std::vector的问题
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
,我也不了解它的来源,因为调试模板化的类是一场噩梦,我只是从这个开始。
有人知道为什么代码不起作用吗?
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&;{aka double&&}'
告诉一切。
- 输入std::数组时出现问题
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- GCC 问题与 static_cast<std::u16string>
- C++ std::function 对于类 exept 的所有实例都是空的(只有 Visual2019 编译器问题)
- 在 std::tuple 上使用 std::variant 的问题
- std::max() 函数与定点实现的比较中的问题
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- 如何在 cpp 中使用 std::enable_if 从模板中处理此类问题
- 关于 std::bitset 构造函数的几个问题?
- 'std::filesystem::d irectory_iterator' 编译器问题
- C++ 中 std::vector 的内存问题
- 我有一个关于C++提升的问题:: asio 和 std :: 异步
- std::p 设置或多组的修订问题
- 没有'str'成员在 GCC 和 Clang 'std::basic_ostream<char>',但 MSVC 没有问题
- 我对 std::unique(算法)C++有问题
- 关于在成员重载中使用 std::move() 的问题
- 示例代码中使用分隔符将 std::string 拆分为 std::vector 的范围问题
- 安卓ndk发布套接字和std问题
- "using namespace std;"问题