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.

  • 相关阅读:
    数列变形中隐含条件的指向作用
    有效挖掘题目中的隐含条件[高阶辅导]
    三角模板函数使用示例
    【Machine Learning in Action --2】K-近邻算法构造手写识别系统
    【Machine Learning in Action --2】K-近邻算法改进约会网站的配对效果
    Python使用os.listdir()函数来获得目录中的内容
    【python问题系列--1】SyntaxError:Non-ASCII character 'xe5' in file kNN.py on line 2, but no encoding declared;
    【Python爬虫实战--1】深入理解urllib;urllib2;requests
    Centos7下安装numpy+matplotlib+scipy
    【Machine Learning in Action --1】机器学习入门指南
  • 原文地址:https://www.cnblogs.com/sssblog/p/11018900.html
Copyright © 2011-2022 走看看