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

转载于:https://www.cnblogs.com/sssblog/p/11016835.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值