小编pat*_*lly的帖子

C++ 11随机数和std :: bind以意想不到的方式进行交互

我正在使用GCC 4.6.3并尝试使用以下代码生成随机数:

#include <random>
#include <functional>

int main()
{
    std::mt19937 rng_engine;

    printf("With bind\n");
    for(int i = 0; i < 5; ++i) {
        std::uniform_real_distribution<double> dist(0.0, 1.0);
        auto rng = std::bind(dist, rng_engine);
        printf("%g\n", rng());
    }

    printf("Without bind\n");
    for(int i = 0; i < 5; ++i) {
        std::uniform_real_distribution<double> dist(0.0, 1.0);
        printf("%g\n", dist(rng_engine));
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我期望两种方法都能生成5个随机数的序列.相反,这是我实际得到的:

With bind
0.135477
0.135477
0.135477
0.135477
0.135477
Without bind
0.135477
0.835009
0.968868
0.221034
0.308167
Run Code Online (Sandbox Code Playgroud)

这是GCC的错误吗?或者是与std :: bind有关的一些微妙问题?如果是这样,你能对结果有所了解吗?

谢谢.

c++ random gcc stdbind c++11

7
推荐指数
2
解决办法
2329
查看次数

带有ICollection值的C#IDictionary,不暴露实现细节

我正在设置一个包含某种集合类型值的Dictionary,如下所示:

IDictionary<string, ICollection<decimal>> goodPrices = 
    new Dictionary<string, List<decimal>>();
Run Code Online (Sandbox Code Playgroud)

但是,这行代码导致编译错误,无法将右侧隐式转换为左侧.显然,以下行不会产生错误:

IDictionary<string, List<decimal>> goodPrices = 
    new Dictionary<string, List<decimal>>();
Run Code Online (Sandbox Code Playgroud)

那么,在不暴露集合的底层实现的情况下,将"goodPrices"声明为一组字符串集合对的正确方法是什么?

c# generics collections covariance

0
推荐指数
1
解决办法
55
查看次数

标签 统计

c# ×1

c++ ×1

c++11 ×1

collections ×1

covariance ×1

gcc ×1

generics ×1

random ×1

stdbind ×1