相关疑难解决方法(0)

unordered_map/unordered_set中元组的通用哈希

为什么不std::unordered_map<tuple<int, int>, string>开箱即用?必须为tuple<int, int>例如定义散列函数是繁琐的

template<> struct do_hash<tuple<int, int>>                               
{   size_t operator()(std::tuple<int, int> const& tt) const {...}  }; 
Run Code Online (Sandbox Code Playgroud)

构建一个以元组为键的无序映射(Matthieu M.)展示了如何自动执行此操作boost::tuple.有没有为c ++ 0x元组执行此操作而不使用可变参数模板?

当然这应该在标准:(

c++ unordered-map tuples unordered-set c++11

27
推荐指数
4
解决办法
2万
查看次数

记忆,递归因子函数?

我知道如何在Python中轻松进行memoization,但我需要一种更快的方式来计算它们,所以我使用的是C++.但是,我不知道如何记忆.我知道它是关于将数值存储到数组或向量中,然后在检索时扫描它的值,但是看看如何完成它真的很有帮助,所以我可以尝试它的速度.

c++ algorithm math factorial

8
推荐指数
2
解决办法
3697
查看次数

变体模板,类型演绎和std :: function

我正在尝试创建一个模板函数,可以通过任何类型和数量的参数传递一些其他函数并将其绑定到std::function.我成功地做到了这一点:

#include <iostream>
#include <functional>

int foo(int bar)
{
  std::cout << bar << std::endl;
  return bar;
}

template <typename Ret, typename... Args>
std::function<Ret (Args...)> func(std::function<Ret (Args...)> f)
{
  return f;
}

int main()
{
  //auto barp = func(foo); // compilation error
  auto bar  = func(std::function<void (int)>(foo));

  bar (0); // prints 0
}
Run Code Online (Sandbox Code Playgroud)

我想调用auto barp = func(foo);并推导出类型,但是这一行给出了以下编译错误:

error: no matching function for call to ‘func(void (&)(int))’
    auto barp = func(foo);
                        ^
note: candidate is:
note: template<class Ret, …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates c++11 std-function

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