为什么不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元组执行此操作而不使用可变参数模板?
当然这应该在标准:(
我知道如何在Python中轻松进行memoization,但我需要一种更快的方式来计算它们,所以我使用的是C++.但是,我不知道如何记忆.我知道它是关于将数值存储到数组或向量中,然后在检索时扫描它的值,但是看看如何完成它真的很有帮助,所以我可以尝试它的速度.
我正在尝试创建一个模板函数,可以通过任何类型和数量的参数传递一些其他函数并将其绑定到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)