本文介绍了高效升压分配使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(改写这个问题)

我创建了升压正态分布的包装类,并想使之尽可能高效。

I'm creating a wrapper class for boost normal distribution, and want to make it as efficient as possible.

如果我使用:

double x = 0.0;
boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > var_nor(rng, nd);
for (int i=0; i<20; i++) {
     double x = var_nor();
}

循环工作正常。我担心的是我不想被不必要的任何声明的方法被调用多次。我试图分裂了code,并把此行的构造器:

The loop works fine. My concern is that I don't want to be declaring anything unnecessarily as the method gets called many times. I tried splitting up the code and put this line in the constructor:

boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > var_nor(rng, nd);

和具有执行此示例方法:

and have a sample method that does this:

     double x = var_nor();
     return x;

但在这种情况下,我得到一个错误说var_nor()(即不带参数)是找不到的。
谁能告诉我这是怎么回事这些提升的声明,即。什么是

But in this case, I get an error saying var_nor() (ie. with no arguments) is not found.Can anyone tell me what's going on with these boost declarations ie. what does the

升压:variate_generate等

行实际上做的 var_nor
用我有限的C ++知识,它看起来好像var_nor正与两个不同的签名定义。

line actually do in with var_nor?With my limited C++ knowledge, it looks as if var_nor is being defined with two different signatures.

谢谢你们
皮特

Thanks guysPete

推荐答案

在您的code, var_nor 变量的,不的函数,因此它不具有一个签名。它重新presents一个 variate_generator 对象,可以的行为像的功能,因为它支持操作符()

In your code, var_nor is a variable, not a function, so it doesn't have a signature. It represents a variate_generator object that can behave like a function because it supports operator().

在您的code,你声明并初始化 var_nor 在同一时间。在 RNG 第二参数传递给构造的> variate_generator 对象。

In your code, you declare and initialize var_nor at the same time. The rng and nd arguments are passed to the constructor of the variate_generator object.

在移动声明到类的构造函数,你声明 var_nor 局部变量的构造函数,所以这也难怪它是其他地方没有的。整个一整类是可用的东西,它需要一个的成员变量的。声明它在类的私人

When you moved the declaration into your class's constructor, you were declaring var_nor as a local variable in the constructor, so it's no wonder it wasn't available elsewhere. For something to be available throughout an entire class, it needs to be a member variable. Declare it private in the class:

class NormalDistribution
{
  boost::random::mt19937 _rng;
  boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > var_nor;
public:
  NormalDistribution();
};

然后在构造函数初始化它:

Then initialize it in the constructor:

NormalDistribution::NormalDistribution():
  _rng(), var_nor(_rng, boost::normal_distribution<>(0.0, 1.0))
{ }

_rng 成员必须首先声明,这样它会先被初始化。在第二参数可以省略,直接传递到 var_nor临时 normal_distribution 对象取代构造如上图所示。

The _rng member needs to be declared first so that it will be initialized first. The nd parameter can be omitted and replaced with a temporary normal_distribution object passed directly to the var_nor constructor as shown above.

随着这些变化,你应该能够使用相同的 normal_distribution 对象到您的样品多次调用功能,或任何其他用途你有你的 NormalDistribution 类。

With those changes, you should be able to use the same normal_distribution object over multiple calls to your sample function, or whatever other uses you have for your NormalDistribution class.

在code你已经因为从你的问题删除,你混淆变量声明与函数声明。您声明第二作为函数接收两个参数并返回一个 normal_distribution 。同样与 var_nor 。这是一个功能,当你真正想要的对象。你感到困惑,因为它正好是就像一个函数的对象,但它仍然只是一个对象。

In the code you've since deleted from your question, you were confusing variable declarations with function declarations. You declared nd as a function receiving two parameters and returning a normal_distribution. Likewise with var_nor. It was a function when you really wanted an object. You were confused because it happens to be an object that acts like a function, but it's still just an object.

这篇关于高效升压分配使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:32