国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

在c++11 的unordered_set和unordered_map中插入pair或tuple作為鍵值

這篇具有很好參考價值的文章主要介紹了在c++11 的unordered_set和unordered_map中插入pair或tuple作為鍵值。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

參考:https://blog.csdn.net/pineappleKID/article/details/108341064

想完成的任務 與 遇到的問題
想在c++11 的unordered_set和unordered_map中插入pair或tuple作為鍵值

std::unordered_map<std::pair<std::string,std::string>, int> m;

會報錯
/usr/include/c++/4.9/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::__is_noexcept_hash<std::tuple<int, int>, std::hash<std::tuple<int, int> > >’
或者
/usr/include/c++/4.9/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::__is_noexcept_hash<std::pair<std::basic_string, std::basic_string >, std::hash<std::pair<std::basic_string, std::basic_string > > >’
C++的std::pair是無法std::hash的,為了在unordered_set和unordered_map中使用std::pair,有如下方法。還有個前提,pair 和 tuple 中的元素本身得是可以 std::hash 哈希的。

方法一:專門寫個可用于std::pair的std::hash

#include <iostream>
#include <unordered_map>
#include <utility>

typedef std::pair<std::string,std::string> pair;

struct pair_hash
{
	template <class T1, class T2>
	std::size_t operator() (const std::pair<T1, T2> &pair) const
	{
		return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
	}
};

int main()
{
	std::unordered_map<pair,int,pair_hash> unordered_map =
	{
		{{"C++", "C++11"}, 2011},
		{{"C++", "C++14"}, 2014},
		{{"C++", "C++17"}, 2017},
		{{"Java", "Java 7"}, 2011},
		{{"Java", "Java 8"}, 2014},
		{{"Java", "Java 9"}, 2017}
	};

	for (auto const &entry: unordered_map)
	{
		auto key_pair = entry.first;
		std::cout << "{" << key_pair.first << "," << key_pair.second << "}, "
				  << entry.second << '\n';
	}

	return 0;
}

輸出

{Java,Java 8}, 2014
{Java,Java 7}, 2011
{Java,Java 9}, 2017
{C++,C++17}, 2017
{C++,C++14}, 2014
{C++,C++11}, 2011

注意:上面的代碼使用的異或(XOR),由于x^x == 0并且x^y == y^x,所以應該配合一些位運算的shift或rotate來做。

方法二:使用boost::hash
boost::hash可以用于哈希integers, floats, pointers, strings, arrays, pairs 以及其它 STL 里的東西

#include <iostream>
#include <boost/functional/hash.hpp>
#include <unordered_map>
#include <utility>

typedef std::pair<std::string,std::string> pair;

int main()
{
	std::unordered_map<pair,int,boost::hash<pair>> unordered_map =
	{
		{{"C++", "C++11"}, 2011},
		{{"C++", "C++14"}, 2014},
		{{"C++", "C++17"}, 2017},
		{{"Java", "Java 7"}, 2011},
		{{"Java", "Java 8"}, 2014},
		{{"Java", "Java 9"}, 2017}
	};

	for (auto const &entry: unordered_map)
	{
		auto key_pair = entry.first;
		std::cout << "{" << key_pair.first << "," << key_pair.second << "}, "
				  << entry.second << '\n';
	}

	return 0;
}

輸出

{Java,Java 8}, 2014
{Java,Java 9}, 2017
{Java,Java 7}, 2011
{C++,C++17}, 2017
{C++,C++14}, 2014
{C++,C++11}, 2011

注意:boost的hash的位置改過,有網(wǎng)友說boost 1.72的hash在

#include <boost/container_hash/extensions.hpp>

原話是

By the way the functional hash has moved. I am not sure when, but in boost 1.72 it is in #include <boost/container_hash/extensions.hpp> I am not sure why the boost hash function for a tuple is not documented somewhere.

方法三:hash_combine
把下列代碼放在任何你想實現(xiàn)本文目的代碼頭文件里
原話是

This works on gcc 4.5 allowing all c++0x tuples containing standard hashable types to be members of unordered_map and unordered_set without further ado. (I put the code in a header file and just include it.)
The function has to live in the std namespace so that it is picked up by argument-dependent name lookup (ADL).文章來源地址http://www.zghlxwxcb.cn/news/detail-836000.html

#include <tuple>
namespace std{
    namespace
    {

        // Code from boost
        // Reciprocal of the golden ratio helps spread entropy
        //     and handles duplicates.
        // See Mike Seymour in magic-numbers-in-boosthash-combine:
        //     http://stackoverflow.com/questions/4948780

        template <class T>
        inline void hash_combine(std::size_t& seed, T const& v)
        {
            seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
        }

        // Recursive template code derived from Matthieu M.
        template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
        struct HashValueImpl
        {
          static void apply(size_t& seed, Tuple const& tuple)
          {
            HashValueImpl<Tuple, Index-1>::apply(seed, tuple);
            hash_combine(seed, std::get<Index>(tuple));
          }
        };

        template <class Tuple>
        struct HashValueImpl<Tuple,0>
        {
          static void apply(size_t& seed, Tuple const& tuple)
          {
            hash_combine(seed, std::get<0>(tuple));
          }
        };
    }

    template <typename ... TT>
    struct hash<std::tuple<TT...>> 
    {
        size_t
        operator()(std::tuple<TT...> const& tt) const
        {                                              
            size_t seed = 0;                             
            HashValueImpl<std::tuple<TT...> >::apply(seed, tt);    
            return seed;                                 
        }                                              

    };
}

到了這里,關于在c++11 的unordered_set和unordered_map中插入pair或tuple作為鍵值的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 【C++】unordered_set與unordered_map的封裝

    【C++】unordered_set與unordered_map的封裝

    ??個人主頁:平凡的小蘇 ??學習格言:命運給你一個低的起點,是想看你精彩的翻盤,而不是讓你自甘墮落,腳下的路雖然難走,但我還能走,比起向陽而生,我更想嘗試逆風翻盤 。 ?? C++專欄 : C++內功修煉基地 家人們更新不易,你們的??點贊??和?關注?真的對我真

    2024年02月08日
    瀏覽(23)
  • 改造哈希表,封裝unordered_map和unordered_set

    改造哈希表,封裝unordered_map和unordered_set

    正文開始前給大家推薦個網(wǎng)站,前些天發(fā)現(xiàn)了一個巨牛的 人工智能 學習網(wǎng)站, 通俗易懂,風趣幽默 ,忍不住分享一下給大家。點擊跳轉到網(wǎng)站。 unordered_map是存的是pair是K,V型的,而unordered_set是K型的,里面只存一個值,那我們如何利用一個數(shù)據(jù)結構將他們都封裝出來呢?

    2024年02月03日
    瀏覽(25)
  • 【C++】unordered_set 和 unordered_map 使用 | 封裝

    【C++】unordered_set 和 unordered_map 使用 | 封裝

    unordered_map官方文檔 unordered_set 官方文檔 set / map與unordered_set / unordered_map 使用功能基本相同,但是兩者的底層結構不同 set/map底層是紅黑樹 unordered_map/unordered_set 底層是 哈希表 紅黑樹是一種搜索二叉樹,搜索二叉樹又稱為排序二叉樹,所以 迭代器遍歷是有序的 而哈希表對應的

    2024年02月06日
    瀏覽(24)
  • 【高階數(shù)據(jù)結構】封裝unordered_map 和 unordered_set

    【高階數(shù)據(jù)結構】封裝unordered_map 和 unordered_set

    (???(??? )??,我是 Scort 目前狀態(tài):大三非科班啃C++中 ??博客主頁:張小姐的貓~江湖背景 快上車??,握好方向盤跟我有一起打天下嘞! 送給自己的一句雞湯??: ??真正的大師永遠懷著一顆學徒的心 作者水平很有限,如果發(fā)現(xiàn)錯誤,可在評論區(qū)指正,感謝?? ????

    2024年02月03日
    瀏覽(56)
  • 【C++】哈希表封裝實現(xiàn) unordered_map 和 unordered_set

    【C++】哈希表封裝實現(xiàn) unordered_map 和 unordered_set

    在 C++98 中,STL 提供了底層為紅黑樹結構的一系列關聯(lián)式容器,在查詢時效率可達到 O(logN),即最差情況下只需要比較紅黑樹的高度次;但是當樹中的節(jié)點非常多時,其查詢效率也不夠極致。 最好的查詢是,不進行比較或只進行常數(shù)次比較就能夠將元素找到,因此在 C++11 中,

    2023年04月16日
    瀏覽(23)
  • C++進階--unordered_set、unordered_map的介紹和使用

    ??在C++98中,STL提供了底層為紅黑樹結構的一系列關聯(lián)式容器,在查詢時效率可達到 l o g 2 N log_2N l o g 2 ? N ,即最差情況下需要比較紅黑樹的高度次,當樹中的節(jié)點非常多時,查詢效率也不理想。最好的查詢是,進行很少的比較次數(shù)就能夠將元素找到,因此在C++11中,STL又

    2024年01月16日
    瀏覽(47)
  • 【C++】用哈希桶模擬實現(xiàn)unordered_set和unordered_map

    【C++】用哈希桶模擬實現(xiàn)unordered_set和unordered_map

    順序結構中(數(shù)組)查找一個元素需要遍歷整個數(shù)組,時間復雜度為O(N);樹形結構中(二叉搜索樹)查找一個元素,時間復雜度最多為樹的高度次logN。理想的搜索方法: 可以不經過任何比較,一次直接從表中得到要搜索的元素。 構造一種存儲結構, 通過某種函數(shù)使元素的

    2024年04月11日
    瀏覽(21)
  • 從哈希桶角度看 unordered_map 與 unordered_set 的實現(xiàn)

    哈希函數(shù)與哈希桶是計算機科學中用于實現(xiàn)高效數(shù)據(jù)檢索的重要工具。在之前的博客中,我們已經詳細探討了哈希的基本概念、哈希函數(shù)的構造方法以及哈希桶的工作原理等內容。在本篇博客中,我們將進一步深入探索C++中的unordered系列數(shù)據(jù)結構,并利用之前介紹的哈希桶原

    2024年03月22日
    瀏覽(20)
  • 由[哈希/散列]模擬實現(xiàn)[unordered_map/unordered_set] (手撕迭代器)

    由[哈希/散列]模擬實現(xiàn)[unordered_map/unordered_set] (手撕迭代器)

    以下兩篇文章均為筆者的嘔心瀝血 想要搞懂本篇文章的uu請自行查閱 哈希/散列的細節(jié)實現(xiàn) 哈希/散列–哈希表[思想到結構][==修訂版==] 手撕迭代器的細節(jié)處理 模擬實現(xiàn)map/set[改編紅黑樹實現(xiàn)map/set容器底層]

    2024年02月07日
    瀏覽(17)
  • 【C++】unordered_map和unordered_set的使用 及 OJ練習

    【C++】unordered_map和unordered_set的使用 及 OJ練習

    在前面的文章中,我們已經學習了STL中底層為紅黑樹結構的一系列關聯(lián)式容器——set/multiset 和 map/multimap(C++98) 在C++98中,STL提供了底層為紅黑樹結構的一系列關聯(lián)式容器,在查詢時效率可達到 l o g 2 N log_2 N l o g 2 ? N ,即最差情況下需要比較紅黑樹的高度次。 在C++11中,

    2024年02月11日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包