小编use*_*572的帖子

c ++错误std :: result_of没有命名类型

g++ -std=c++0x'ing之后std::result_of产生以下错误消息

error: ‘result_of’ in namespace ‘std’ does not name a type
Run Code Online (Sandbox Code Playgroud)

(SUSE上的g ++版本4.5.0.)

足以重现错误的相关代码段如下

#include <random>
#include <type_traits>

using namespace std;

class Rnd{
protected:
  static default_random_engine generator_;
};

template<class distribution>
class Distr: Rnd{
  distribution distribution_;
public:
  typename std::result_of<distribution(default_random_engine)>::type 
       operator() (){ return distribution_(default_random_engine); }
};
Run Code Online (Sandbox Code Playgroud)

此外,我试图从维基百科或cpluplus.com编译示例无济于事.这是特定编译器的问题还是我做错了什么?

c++ gcc g++ result-of c++11

6
推荐指数
1
解决办法
2494
查看次数

c ++ 11 STL的binomial_distribution非常慢

我使用STL的'随机'生成二项分布的随机数.当范围很大时,它变得非常慢.对于范围40,生成100个数字需要12秒.对于更大的范围,时间会急剧增加(我需要大约10000的范围).它似乎不依赖于概率参数.我正在使用g ++ 4.5.0.

#include <iostream>
#include <random>

using namespace std;

vector<int> v;

default_random_engine gen(123);
binomial_distribution<int> rbin(40,0.7);

int main(){
  v.reserve(2000);
  for(int i=0; i<100;++i){
    v.push_back(rbin(gen));
   }
}
Run Code Online (Sandbox Code Playgroud)

输出:

50.~/.../fs/> g++ -std=c++0x q.cpp 
51.~/.../fs/> time ./a.out 
real    0m12.102s
user    0m12.094s
sys     0m0.002s
52.~/.../fs/>
Run Code Online (Sandbox Code Playgroud)

我可以使用正态近似,但它对于概率参数的极值是不好的.

更新:

'-O3'选项时间变为~2秒.使用g ++ 4.6.3时,问题完全消失了 - 几乎没有时间依赖于范围,100个数字的生成需要5ms.

c++ random gcc libstdc++ c++11

5
推荐指数
1
解决办法
525
查看次数

Wolfram Mathematica 中的 Sum[] 和 Sequence[]

我需要评估可变数量集合的笛卡尔积的总和。假设 f[...] 是一个多元函数,定义

p[A__set] :=  Module[{Alist, args, iterators,it},
  Alist = {A};
  i = 1;
  iterators = {it[i++], Level[#1, 1]} & /@ Alist;
  args = Table[it[i], {i, Range[Length[Alist]]}];
  Sum[f@@ args, Sequence @@ iterators ]
]
Run Code Online (Sandbox Code Playgroud)

但是之后

p[set[1, 2, 3], set[11, 12, 13]]
Run Code Online (Sandbox Code Playgroud)

给出错误: Sum::vloc: "The variable Sequence@@iterators cannot be localized so that it can be assigned to numerical values."

以下黑客工作:

p[A__set] :=  Module[{Alist, args, iterators,it,TmpSymbol},
  Alist = {A};
  i = 1;
  iterators = {it[i++], Level[#1, 1]} & /@ Alist;
  args = …
Run Code Online (Sandbox Code Playgroud)

wolfram-mathematica

3
推荐指数
1
解决办法
2020
查看次数

标签 统计

c++ ×2

c++11 ×2

gcc ×2

g++ ×1

libstdc++ ×1

random ×1

result-of ×1

wolfram-mathematica ×1