zoukankan      html  css  js  c++  java
  • boost unordered

    Boost.Unordered provides the classes boost::unordered_set, boost::unordered_multiset, boost::unordered_map, and boost::unordered_multimap. These classes are identical to the hash containers that were added to the standard library with C++11.

     1. boost::unordered_set

    #include <boost/unordered_set.hpp>
    #include <string>
    #include <iostream>
    
    int main() {
      boost::unordered_set<std::string> set;
      set.emplace("cat");
      set.emplace("shark");
      set.emplace("spider");
    
      for (const std::string& s : set) {
        std::cout << s << std::endl;
      }
    
      std::cout << set.size() << std::endl;
      std::cout << set.max_size() << std::endl;
    
      std::cout << std::boolalpha << (set.find("cat") != set.end()) << std::endl;
      std::cout << set.count("shark") << std::endl;
      return 0;
    }

    输出为:

    spider

    shark

    cat

    3

    1152921504606846975

    true

    1

    boost::unordered_set can be replaced with std::unordered_set, boost::unordered_set doesn't differ from std::ordered_set.

    2. boost::unordered_map

    #include <boost/unordered_map.hpp>
    #include <string>
    #include <iostream>
    
    int main() {
      boost::unordered_map<std::string, int> map;
      map.emplace("cat", 4);
      map.emplace("shark", 0);
      map.emplace("spider", 8);
    
      for (const auto& p : map) {
        std::cout << p.first << ";" << p.second << std::endl;
      }
    
      std::cout << map.size() << std::endl;
      std::cout << map.max_size() << std::endl;
    
      std::cout << std::boolalpha << (map.find("cat") != map.end()) << std::endl;
      std::cout << map.count("shark") << std::endl;
      return 0;
    }

    输出为:

    spider;8

    shark;0

    cat;4

    3

    1152921504606846975

    true

    1

    3. User-defined type with Boost.Unordered

    #include <boost/unordered_set.hpp>
    #include <string>
    #include <cstddef>
    
    struct animal {
      std::string name;
      int legs;
    };
    
    bool operator==(const animal& lhs, const animals& rhs) {
      return lhs.name == rhs.name && lhs.legs == rhs.legs;
    }
    
    std::size_t hash_value(const animal& a) {
      std::size_t seed = 0;
      boost::hash_value(seed, a.name);
      boost::hash_value(seed, a.legs);
      return seed;
    }
    
    int main() {
      boost::unordered_set<animal> animals;
    
      animals.insert({"cat", 4});
      animals.insert({"shark", 0});
      animals.insert({"spider", 8});
    
      return 0;
    }

    Because the hash function of boost::unordered_set doesn't know the class animal, hash values can't be automatically calculate for elements of this type. That's why a hash function must be defined-otherwise the example can't be compiled.

    In adddition to defining hash_value(), you need to make sure two objects can be compared using==. That't why the operator== is overloaded for animal.

  • 相关阅读:
    单例模式的奇幻漂流
    webmin安装简介
    Linux下添加磁盘创建lvm分区
    教训 Mac下装windows系统 失败后 磁盘空间丢失
    Mac下安装 php+nginx+mysql 开发环境
    记一次 nginx 504 Gateway Time-out
    twemproxy 安装
    centos6.7下 编译安装MySQL5.7
    vagrant 配置文件简析
    VNC配置连接远程服务器桌面-linuxwindows
  • 原文地址:https://www.cnblogs.com/sssblog/p/11018900.html
Copyright © 2011-2022 走看看