为什么不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元组执行此操作而不使用可变参数模板?
当然这应该在标准:(
我有一个未知大小的元组(它是方法的模板参数)
是否可以获得它的一部分(我需要丢掉它的第一个元素)
例如,我有tuple<int,int,int>(7,12,42)
.我想要tuple<int,int>(12,42)
在这里
给定 a ,std::tuple<A, B, ...> foo
C++14 中是否有任何通用(模板化)函数或技术来获取包含std::tuple<B, ...> bar
除第一个元素之外的所有元素的新元组foo
?或者,也许是 Boost 中的某些东西?
我已经使用参数包和一些模板元编程编写了一个辅助函数来执行此操作,但我很想扔掉所有这些东西!
这就是我目前正在做的事情。我定义了一个辅助函数unshift_tuple()
,它返回一个元组,其中包含传递给函数的元组中除第一个元素之外的所有元素。的实现unshift_tuple()
使用一个辅助函数unshift_tuple_with_indices()
,该函数采用包含要提取的元组索引的参数包;帮助sequential_integer_list
器类型用于使用模板元编程生成适当的索引列表参数包。丑陋的!
#include <tuple>
template <size_t... Integers>
struct integer_list {};
template <size_t N, size_t... Args>
struct sequential_integer_list : sequential_integer_list<N - 1, N - 1, Args...> {};
template <size_t... Args>
struct sequential_integer_list<0, Args...> { typedef integer_list<Args...> type; };
template <typename FirstElement, typename... Elements, size_t... Indices>
static std::tuple<Elements...> unshift_tuple_with_indices(
const std::tuple<FirstElement, Elements...>& tuple,
integer_list<Indices...> index_type)
{
return …
Run Code Online (Sandbox Code Playgroud)