zoukankan      html  css  js  c++  java
  • STL

    1. hash工具类

    hashval.hpp

    #ifndef _Core_HashVal_H_
    #define _Core_HashVal_H_
    
    #include <functional>
    
    // from boost (functional/hash):
    // see http://www.boost.org/doc/libs/1_35_0/doc/html/hash/combine.html
    template <typename T>
    inline void hash_combine (std::size_t& seed, const T& val)
    {
        seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
    
    // auxiliary generic functions to create a hash value using a seed
    template <typename T>
    inline void hash_val (std::size_t& seed, const T& val)
    {
        hash_combine(seed,val);
    }
    template <typename T, typename... Types>
    inline void hash_val (std::size_t& seed,
                          const T& val, const Types&... args)
    {
        hash_combine(seed,val);
        hash_val(seed,args...);
    }
    
    // auxiliary generic function to create a hash value out of a heterogeneous list of arguments
    template <typename... Types>
    inline std::size_t hash_val (const Types&... args)
    {
        std::size_t seed = 0;
        hash_val (seed, args...);
        return seed;
    }
    
    #endif

    2. UnorderedSetTest.cpp

    #include <unordered_set>
    #include "../../Core/print.hpp"
    #include "UnorderedSetTest.h"
    #include "../../Core/hashval.hpp"
    #include "../../Domain/Models/Customer.h"
    #include "../../Domain/Models/CustomerHash.h"
    #include "../../Domain/Models/CustomerEqual.h"
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    void UnorderedSetTest::simpleHashFunc()
    {
        // unordered set with own hash function and equivalence criterion
        unordered_set<Customer, CustomerHash, CustomerEqual> custset;
    
        custset.insert(Customer("arne", "wink", 70));
        custset.insert(Customer("peter", "zhang", 70));
        PRINT_ELEMENTS(custset);
        
        Customer cust = Customer("arne", "wink", 70);
        if (custset.find(cust) != custset.end())
        {
            cout << "Customer: " << cust << " found!" << endl;
        }
        else
        {
            cout << "Customer: " << cust << " not exists!" << endl;
        }
    
        Customer cust2 = Customer("arne", "wink2", 70);
        if (custset.find(cust2) != custset.end())
        {
            cout << "Customer: " << cust2 << " found!" << endl;
        }
        else
        {
            cout << "Customer: " << cust2 << " not exists!" << endl;
        }
    }
    
    void UnorderedSetTest::run()
    {
        printStart("simpleHashFunc()");
        simpleHashFunc();
        printEnd("simpleHashFunc()");
    }

    3. 运行结果:

    ---------------- simpleHashFunc(): Run Start ----------------
    [arne,wink,70] [peter,zhang,70]
    Customer: [arne,wink,70] found!
    Customer: [arne,wink2,70] not exists!
    ---------------- simpleHashFunc(): Run End ----------------

  • 相关阅读:
    咖啡生活-享受慵懒的午后时光
    窗体皮肤实现
    编译树莓派2代B型OpenWrt固件实现无线路由器及nodogsplash认证功能
    通过hook实现禁止shift+delete快捷键
    c++ Qt向PHP接口POST文件流
    Qt for Android开发总结
    A previous installation of Qt5 Visual Studio Add-in was detected. Please uninstall it before running this installer解决办法
    windows下Qt5.1 for android开发环境配置
    Qt+mpg123+openal播放MP3流
    RTC搭建android下三层应用程序访问服务器MsSql-客户端
  • 原文地址:https://www.cnblogs.com/davidgu/p/5056593.html
Copyright © 2011-2022 走看看