小编moo*_*kid的帖子

堆栈溢出与std :: make_unique但不与原始指针

我想分配一个大对象(a bitset<1000000000>).由于要避免拥有原始指针,我尝试了以下声明:

auto foo()->std::unique_ptr<std::bitset<MAX>>;
...
{
    auto bar = foo();
}
Run Code Online (Sandbox Code Playgroud)

这给出了堆栈溢出错误(编译器是VS2013).但

auto foo()->std::bitset<MAX>*;
...
{
    auto bar = foo();
    ...
    delete bar;
}
Run Code Online (Sandbox Code Playgroud)

才不是.

执行foo()看起来像:

auto is_prime = 
        //std::make_unique<std::bitset<MAX>>(std::bitset<MAX>{});
        // or:
        new std::bitset<MAX>{};
is_prime->set();

(*is_prime)[0] = (*is_prime)[1] = false;

auto max_i = static_cast<int>(std::sqrt(MAX)) + 1;
for (auto i = 1; i < max_i; i++) {
        if ((*is_prime)[i]) {
                for (auto j = i * i; j < MAX; j += i) {
                        (*is_prime)[j] = false;
                } …
Run Code Online (Sandbox Code Playgroud)

c++ heap smart-pointers c++11

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

重置保留键的 .NET 字典

我有一Dictionary<K,V>组已知的、不变的键。我想重置字典,但保留键的值,仅将值更改为null.

我可以先调用Clear()字典并重新添加对null作为值,应该有更好的方法。

.net containers c#-3.0

2
推荐指数
1
解决办法
3094
查看次数

使用模板函数进行clang编译错误

我正在使用clang编译一些代码(我用microsoft工具链编写和编译好的代码).这是一些我不理解错误的代码:

#include <iostream>
#include <bitset>

template <int N>
auto foo(int index, std::bitset<N> & already_given)->int
{
        return 0;
}


auto bar()->void
{
        auto const n = 10;

        auto baz = std::bitset<n>{};
        for (auto i = 0; i < n; i++) {
                std::cout << foo(i, baz)
                          << std::endl;
        }
}
Run Code Online (Sandbox Code Playgroud)

给了我错误no matching function to call to 'foo'.这个错误的来源是什么?

c++ clang c++11

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

另一个向量vs动态分配的数组

人们经常会发现,动态分配的数组与之间的性能差异很小std::vector.

以下是项目Euler测试问题10的两个版本,有两个版本:

std::vector:

const __int64 sum_of_primes_below_vectorversion(int max) 
{
        auto primes = new_primes_vector(max);
        __int64 sum = 0;
        for (auto p : primes) {
                sum += p;
        }
        return sum;
}

const std::vector<int> new_primes_vector(__int32 max_prime)
{
        std::vector<bool> is_prime(max_prime, true);
        is_prime[0] = is_prime[1] = false;
        for (auto i = 2; i < max_prime; i++) {
                is_prime[i] = true;
        }
        for (auto i = 1; i < max_prime; i++) {
                if (is_prime[i]) {
                        auto max_j = max_prime / i;
                        for …
Run Code Online (Sandbox Code Playgroud)

c++ arrays benchmarking vector c++11

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

c#:Func <T>参数中的重载决策

写这个功能:

static TResult reduce<TSource, TResult>(ParallelQuery<TSource> source,
                                        Func<TResult> seedFactory,
                                        Func<TResult, TSource, TResult> aggregator) {
    return source.Aggregate(seedFactory, aggregator, aggregator, x => x);
}                
Run Code Online (Sandbox Code Playgroud)

但我得到一个编译错误:

误差为1方法的类型参数"System.Linq.ParallelEnumerable.Aggregate( ,System.Linq.ParallelQuery<TSource>, TAccumulate,, System.Func<TAccumulate,TSource,TAccumulate> )"不能从使用推断.尝试显式指定类型参数.System.Func<TAccumulate,TAccumulate,TAccumulate>System.Func<TAccumulate,TResult>

我想要使​​用的重载是这一个, 而编译器似乎认为它也可以是这个.

我该怎么帮忙呢?

c# generics overloading overload-resolution

-1
推荐指数
1
解决办法
139
查看次数