zoukankan      html  css  js  c++  java
  • boost bimap

    The library Boost.Bimap is based on Boost.MultiIndex and provides a container that can be used immediately without being definded first. The container is similar to std::map, but supports looking up values from either side.

    #include <boost/bimap.hpp>
    #include <string>
    #include <iostream>
    
    int main() {
      boost::bimap<std::string, int> animals;
    
      animals.insert({"cat", 4});
      animals.insert({"shark", 0});
      animals.insert({"spider", 8});
    
      std::cout << animals.left.count("cat") << std::endl;
      std::cout << aninals.right.count(8) << std::endl;
      return 0;
    }

    boost::bimap provides two member variables, left and right, which can be used to access the two containers of type std::map that are unified by boost::bimap.

    left uses keys of type std::string to access the container, and right uses keys of type int.

    #include <boost/bimap.hpp>
    #include <string>
    #include <iostream>
    
    int main() {
      boost::bimap<std::string, int> animals;
    
      animals.insert({"cat", 4});
      animals.insert({"shark", 0});
      animals.insert({"spider", 8});
    
      for (auto it = animals.begin(); it != animals.end(); ++it) {
        std::cout << it->left << " has " << it->right << " legs" << std::endl;
      }
      return 0;
    }

    It is not necessary to access records using left or right. By iterating over records, the left and right parts of an individual record are made vaailable through the iterator.

    Strictly speaking, the two required template parameters specify container types for left and right, not the types of the elements to store. If no container type is specified, the container type boost::bimaps::set_of is used by default. This container , like std::map, only accepts records with unique keys.

    #include <boost/bimap.hpp>
    #include <string>
    #include <iostream>
    
    int main() {
      boost::bimap<boost::bimaps::set_of<std::string>, boost::bimaps::set_of<int>> animals;
    
      animals.insert({"cat", 4});
      animals.insert({"shark", 0});
      animals.insert({"spider", 8});
    
      std::cout << animals.left.count("cat") << std::endl;
      std::cout << aninals.right.count(8) << std::endl;
      return 0;
    }

    2. allowing duplicates with boost::bimaps::multiset_of

    #include <boost/bimap.hpp>
    #include <boost/bimap/multiset_of.hpp>
    #include <string>
    #include <iostream>
    
    int main() {
      boost::bimap<boost::bimaps::set_of<std::string>, boost::bimaps::multiset_of<int>> bimap;
    
      animals.insert({"cat", 4});
      animals.insert({"shark", 0});
      animals.insert({"dog", 4});
      
      std::cout << animals.left.count("dog") << std::endl;
      std::cout << animals.right.count(4) << std::endl;
    
      return 0;   
    }

    boost::bimaps::multiset_of the keys don't need to be unique.

    3. In addition to the classes shown above, Boost.Bimap provides the following boost::bimaps::unordered_set_of, boost::bimaps::unordered_multiset_of, boost::bimaps::list_of, boost::bimaps::vector_of, and boost::bimaps::unconstrained_set_of. Except for boost::bimaps::unconstrained_set_of, all of the other container types operate just like their counterparts from the standard library.

    #include <boost/bimap.hpp>
    #include <string>
    #include <iostream>
    
    int main() {
      boost::bimap<std::string, boost::bimaps::unconstrained_set_of<int>> animals;
    
      animals.insert({"cat", 4});
      animals.insert({"shark", 0});
      animals.insert({"spider", 8});
    
      auto it = animals.left.find("cat");
      animals.left.modify_key(it, boost::bimaps::_key = "dog");
    
      std::cout << it->first << std::endl;
      return 0;
    }

    输出: dog

  • 相关阅读:
    深入理解泛型之JAVA泛型的继承和实现、泛型擦除
    hadoop过程中遇到的错误与解决方法
    微服务拆分到什么粒度合适——康威定律
    墨菲定律(设计系统)和康威定律(系统划分)
    Hadoop-Impala学习笔记之SQL参考
    Hadoop-Impala学习笔记之管理
    Hadoop2-HDFS学习笔记之入门(不含YARN及MR的调度功能)
    Hadoop-Impala学习笔记之入门
    解决 Invalid character found in method name. HTTP method names must be tokens 异常信息
    从康威定律和技术债看研发之痛
  • 原文地址:https://www.cnblogs.com/sssblog/p/11016835.html
Copyright © 2011-2022 走看看