• 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

  • 相关阅读:
    JS伪3D 图形透视效果
    源码安装apache及配置转发
    SpringSecutiry权限管理手册
    解决SMARTFORMS 中table 控件单行跨页的问题
    Cluster Table
    uva-133 The Dole Queue
    第三届蓝桥杯C++本科B组决赛解题报告(更新中)
    uva-673 Parentheses Balance
    VS2010不能编译SQLServer2005的Microsoft.SQLServer.ManagedDTS.dll的解决方法
    IOS设计模式学习(21)享元
  • 原文地址:https://www.cnblogs.com/sssblog/p/11016835.html
Copyright © 2020-2023  润新知