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

  • 相关阅读:
    2021.1.20 学习总结
    2021.1.19 学习总结
    2021.1.18 学习总结
    学习总结13
    学习总结12
    学习总结11
    学习总结10
    ASP.NET网站开发步骤
    学习总结9
    阅读笔记4
  • 原文地址:https://www.cnblogs.com/sssblog/p/11016835.html
Copyright © 2011-2022 走看看