2013-05-13 46 views
2

給定一串字符串我試圖創建一個程序,可以模仿一個基於我的輸入加權分佈的僞隨機行爲。問題與std :: piecewise_constant_distribution和std ::向量

到目前爲止,我想出了這個

#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來實現它,這是我發現的主要區別。

鏗鏘++錯誤的輸出

/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我不明白,我也不會從取之於因爲來獲得調試模板化的課程,這是一場噩夢,我只是從這個開始。

任何人有任何想法爲什麼代碼不起作用?

+0

@taocp是的,我也在IDE內部完成了這個工作,並且帶有一個命令行編譯器接口,這兩個接口都支持C++ 11,沒有什麼變化。 – user2348816 2013-05-13 01:14:11

+0

你在哪裏見過帶字符串的例子?它抱怨,因爲無法在字符串 – 4pie0 2013-05-13 01:22:04

+0

@ restart.localhost.localdomain操作無處,這只是我需要和我想要實現的。 – user2348816 2013-05-13 01:23:23

回答

3

std::piecewise_constant_distribution的默認結果類型是RealTypedouble此處)。看起來你正試圖從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"; 
} 

... 
+0

感謝您的解決方案:D – user2348816 2013-05-13 01:33:37

1
template< class RealType = double > 
class piecewise_constant_distribution; 

RealType操作,不能操作字符串。 see here

產生隨機浮點數,其是均勻 在每個子區間數[雙向的分佈,雙+ 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 ::矢量:: VALUE_TYPE & & {又名雙& &}」沒有已知的轉換

告訴一切。

+0

謝謝'/ /填料' – user2348816 2013-05-13 01:33:53

+0

肯定,沒問題 – 4pie0 2013-05-13 01:35:03