在VS2013中初始化std :: discrete_distribution [英] Initialising std::discrete_distribution in VS2013

查看:232
本文介绍了在VS2013中初始化std :: discrete_distribution的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 std :: vector< float> weight; 包含权重列表。我不知道什么将在这个列表,直到一些阶段在运行程序。我想做

I have a std::vector<float> weights; containing the list of weights. I don't know what will be in this list until some stage in running the program. I would like to do

std::discrete_distribution<> dist(weights.begin(),weights.end());

但VS2013看起来没有接受迭代器范围的std :: discrete_distribution构造函数。是否有任何解决方法?

but VS2013 doesn't appear to have a constructor for std::discrete_distribution that accepts an iterator range. Is there any workaround?

推荐答案

比较 cppreference.com Microsoft引用 std :: discrete_distribution

这些是VS2013提供的构造函数:

These are the constructors provided by VS2013:

discrete_distribution();
explicit discrete_distribution(
    const param_type& par0
);
discrete_distribution(
    initializer_list<double> IList
);
template<class Fn>
    discrete_distribution(
        size_t count,
        double low, 
        double high, 
        Fn func
);

缺少一个重要的构造函数,可能是因为Microsoft开发人员没有时间来实现它:

There is one important constructor missing, probably because the Microsoft developers didn't have the time to implement it:

template< class InputIt >
 discrete_distribution( InputIt first, InputIt last );

这意味着,除非文档不完整,否则不能使用基于迭代器的构造函数这个类。

That means, unless the documentation is incomplete, you simply can't use an iterator-based constructor for this class. Switch to another compiler (like clang or g++), or wait until this feature is implemented.

现在,为了解决方法,您可以立即使用:

Now for a workaround you can use right now:

std::size_t i(0);
assert( !weights.empty() ); // No weights would be very weird.
std::discrete_distribution<> dist(weights.size(),
                                  0.0, // dummy!
                                  0.0, // dummy!
                                  [&weights,&i](double)
{
   auto w = weights[i];
   ++i;
   return w;
 });

我希望至少支持lambdas ;-)重要的是捕获 i ,以使其得到适当的增量。演示: http://ideone.com/nIBUts

I hope that at least lambdas are supported ;-) The important thing is to capture i by reference, such that it gets properly incremented. Demo: http://ideone.com/nIBUts

为什么这个工作?我们在这里使用的构造函数是:

Why does this work? The constructor we are using here is:

template< class UnaryOperation >
discrete_distribution( std::size_t count, double xmin, double xmax,
                       UnaryOperation unary_op );

关于cppreference的文档告诉我们 count (在我们的例子中 size()),以及 xmin xmax 使用 UnaryOperation 的权重。

The documentation on cppreference tells us that the count (in our case weights.size()), as well as the xmin and xmax is used to create the weights using the UnaryOperation.

我们忽略 xmin xmax 。由于 UnaryOperation 我们使用lambda

We are ignoring xmin and xmax on purpose. As the UnaryOperation we use the lambda

[&weights,&i](double)
{
   auto w = weights[i];
   ++i;
   return w;
 }

[&weights,&i](double)
{
   return weights[i++];
}

现在,我们忽略该运算符的输入值,并返回向量的第i个元素。我们通过引用捕获向量和索引,以避免副本。

Now, we are ignoring the input value for that operator and just return the i^th element of our vector. We capture both the vector and the index by reference to avoid copies.

这篇关于在VS2013中初始化std :: discrete_distribution的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆