- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
使用 c++ std 的 unordered_map 我想将整数三元组映射到单个整数,我通常不使用哈希表(不知道它们这么酷),但我不知道在这种情况下的正确方法,我应该使用默认的散列函数直接映射三元组(类似于 << int,int >,int >->int)
std::unordered_map <std::make_pair <make_pair <int,int>,int>,int> hash;
或者可能使用函数将三元组映射到单个值并将该值与默认函数一起使用?
int mapping(int a, int b, int c){
}
std::unordered_map <int,int> hash;
这两种方法都有效,但我想知道哪种方法最有效。谢谢
最佳答案
首先,您将使用 std::tuple<int, int, int>
作为 key 类型。
接下来,您需要一种散列元组的方法,因为您可以对每个元素进行散列。有一个函数叫做 hash_combine
在 Boost 中这样做,但由于我不清楚的原因,该标准未包含在标准中。无论如何,这里是:
#include <tuple>
#include <utility>
template <class T>
inline void hash_combine(std::size_t & seed, const T & v)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template <class Tuple, std::size_t Index = std::tuple_size<Tuple>::value - 1>
struct tuple_hash_impl
{
static inline void apply(std::size_t & seed, Tuple const & tuple)
{
tuple_hash_impl<Tuple, Index - 1>::apply(seed, tuple);
hash_combine(seed, std::get<Index>(tuple));
}
};
template <class Tuple>
struct tuple_hash_impl<Tuple, 0>
{
static inline void apply(std::size_t & seed, Tuple const & tuple)
{
hash_combine(seed, std::get<0>(tuple));
}
};
namespace std
{
template<typename S, typename T> struct hash<pair<S, T>>
{
inline size_t operator()(const pair<S, T> & v) const
{
size_t seed = 0;
::hash_combine(seed, v.first);
::hash_combine(seed, v.second);
return seed;
}
};
template<typename ...Args> struct hash<tuple<Args...>>
{
inline size_t operator()(const tuple<Args...> & v) const
{
size_t seed = 0;
tuple_hash_impl<tuple<Args...>>::apply(seed, v);
return seed;
}
};
}
关于c++ - 将 "int Triplets"映射到 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9815142/
如果我们有 (a1, b1) 和 (a2, b2) 就很容易使用字典来存储对应关系: dict[a1] = b1 dict[a2] = b2 我们可以得到 (a1, b1) 和 (a2, b2) 没有
所以我刚刚了解了 Triplet类(class)。我没有使用 ASP.NET 的经验,只有核心 .NET Framework。 有人可以向我解释 Triplet 类存在的位置/原因吗?它像元组吗? 最
题目地址:https://leetcode.com/problems/increasing-triplet-subsequence/description/ 题目描述: Given an unso
我发现识别服务的方法是通过 DVB 三元组。 DVB三元组是如何形成的,如何保证唯一性? 最佳答案 根据 TS 102 539,DVB 三元组由以下部分组成: 原始网络 ID 传输流 ID 服务 ID
代码如下: <?php /** * 三元组 Triplet * */ class Triplet { p
使用 c++ std 的 unordered_map 我想将整数三元组映射到单个整数,我通常不使用哈希表(不知道它们这么酷),但我不知道在这种情况下的正确方法,我应该使用默认的散列函数直接映射三元组(
我想比较以下类型的 CNN 对于两个不同的大型图像数据集的性能。目标是测量两幅图像之间的相似性,这两幅图像在训练期间都没有出现过。我可以使用 2 个 GPU 和 16 个 CPU 内核。 Triple
我的原生 gcc 说,它的三元组如下。 > gcc -dumpmachine x86_64-suse-linux 其中cpu-vendor-os对应x86_64、suse、linux。后者意味着 gl
我正在努力了解如何使用 tm 包的 TermDocumentMatrix() 生成的简单三重矩阵进行基本操作。 问题似乎出在矩阵未被识别为数字。 library(tm) data("crude") t
我想制作一个通用的特征提取器,它还可以将新类分类为模型中的未知类。除了基于距离的矩阵之外,还有什么好的技术可用吗? 目前我正在使用 Triplet loss,由于 alpha 值较小,我的 Tripl
这是 Codility 的三角问题: A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is
我正在使用邻接矩阵来表示可以在视觉上解释为的 friend 网络 Mary 0 1 1 1 Joe 1 0 1 1
我对 C 还很陌生,并且部分地通过 Codility 培训来学习它。 对于三重问题,我只得到 11% 我不确定这里有什么问题。问题是:给定一个由 N 个整数组成的非空零索引数组 A。三元组 (P, Q
在我将 android studio 更新到 3.4.2 和 gradle 之后,当我运行这条线时 gradlew publishApkappNameRelease 我收到这个错误: * What w
你看,大多数讨论图像创建的帖子都涉及 3D 矩阵 [0][1][2],它有效地包含直接应用的必要信息 img = Image.fromarray(Matrix, 'RGB') 但是,我遇到了一个具有“
碰到古老的 .NET Pair Class和 Triplet Class 为什么它们在 System.Web.UI 命名空间中? 我可以在桌面(Windows 窗体)或控制台应用程序中使用它们吗? 更
碰到古老的 .NET Pair Class和 Triplet Class 为什么它们在 System.Web.UI 命名空间中? 我可以在桌面(Windows 窗体)或控制台应用程序中使用它们吗? 更
这个问题在这里已经有了答案: Find all triplets in array with sum less than or equal to given sum (5 个答案) 关闭 8 年前。
我是一名优秀的程序员,十分优秀!