boost教程(十四):容器bimap

每一部分都单独注释的,运行时取消注释,将其他部分注释起来就可以。

/*
Boost.Bimap 库提供了一个建立在 Boost.MultiIndex 之上但不需要预先定义就可以使用的容器。
********建立在 Boost.MultiIndex 之上***********
这个容器十分类似于 std::map, 但他不仅可以通过 key 搜索, 还可以用 value 来搜索。
*/


#include <boost/bimap.hpp> 
#include <boost/bimap/multiset_of.hpp>

#include <boost/bimap/unconstrained_set_of.hpp> 
#include <boost/bimap/support/lambda.hpp> 

#include <iostream> 
#include <string> 


int main()
{
	
	存储不同key值得记录
	///*
	// boost::bimap 为我们提供了两个属性: left 和 right 
	// 来访问在 boost::bimap 统一的两个 std::map 类型的容器。 
	// left 用 std::string 类型的 key 来访问容器, 而 right 用到了 int 类型的 key。
	//*/
	//typedef boost::bimap<std::string, int> bimap;
	//bimap persons;

	//persons.insert(bimap::value_type("Boris", 31));
	//persons.insert(bimap::value_type("Anton", 31));
	//persons.insert(bimap::value_type("Caesar", 25));

	///*
	//对一个记录访问时, left 和 right 并不是必须的。 
	//你也可以使用迭代器来访问每个记录中的 left 和 right 容器。
	//*/
	//std::cout << persons.left.count("Boris") << std::endl;
	//std::cout << persons.right.count(31) << std::endl;

	///*
	//上面的这个示例 ,只保存了一个年龄为31的person
	//*/
	迭代器的访问
	//for (bimap::iterator it= persons.begin() ;it!=persons.end();it++)
	//{
	//	std::cout << it->left <<" "<<it->right << std::endl;
	//}
	






	/*
	 如果像例子中那样并没有指定容器类型时, boost::bimaps::set_of 类型会缺省的使用。
	 跟 std::map 一样, 它要求记录有唯一的 key 值。
	 感觉这个没卵子用啊,跟上面的那个一样
	*/
	/*typedef boost::bimap<boost::bimaps::set_of<std::string>, boost::bimaps::set_of<int>> bimap;
	bimap persons;

	persons.insert(bimap::value_type("Boris", 31));
	persons.insert(bimap::value_type("Anton", 31));
	persons.insert(bimap::value_type("Caesar", 25));

	std::cout << persons.left.count("Boris") << std::endl;
	std::cout << persons.right.count(31) << std::endl;*/











	/*
	****关键来了:不再要求键值唯一
	用一些其他的容器类型来定制你的 boost::bimap。
	boost::bimaps::multiset_of。 这个容器的操作和 boost::bimaps::set_of 差不了多少,
	只是它不再要求 key 值是唯一的。 因此, 上面的例子将会在计算 age 为 31 的 person 数时输出: 2。
	*/
	//typedef boost::bimap<boost::bimaps::set_of<std::string>, boost::bimaps::multiset_of<int>> bimap;
	//bimap persons;

	//persons.insert(bimap::value_type("Boris", 31));
	//persons.insert(bimap::value_type("Anton", 31));
	//persons.insert(bimap::value_type("Caesar", 25));

	//std::cout << persons.left.count("Boris") << std::endl;
	年龄为31的被记录了两次
	//std::cout << persons.right.count(31) << std::endl;






	/*
	*********第二个参数的选择。(目测第一个应该也阔以)
	Boost.Bimap 还提供了类:
	boost::bimaps::unordered_set_of,
	boost::bimaps::unordered_multiset_of,
	boost::bimaps::list_of,
	boost::bimaps::vector_of 和 
	boost::bimaps::unconstrainted_set_of 以供使用。
	除了 boost::bimaps::unconstrainted_set_of, 剩下的所有容器类型的使用方法都和他们在 C++ 标准里的版本一样。
	*/


	/*
	
	boost::bimaps::unconstrainted_set_of 可以使 boost::bimap 的 right (也就是 age)值无法用来查找 person。
	在这种特定的情况下, boost::bimap 可以被视为是一个 std::map 类型的容器。
	*/
	typedef boost::bimap<std::string, boost::bimaps::unconstrained_set_of<int>> bimap;
	bimap persons;

	persons.insert(bimap::value_type("Boris", 31));
	persons.insert(bimap::value_type("Anton", 31));
	persons.insert(bimap::value_type("Caesar", 25));

	bimap::left_map::iterator it = persons.left.find("Boris");
	/*
	key 通过 boost::bimaps::_key 函数赋予了新值, 而 boost::bimaps::_key 是一个定义在 boost/bimap/support/lambda.hpp 中的 lambda 函数。
	*/
	persons.left.modify_key(it, boost::bimaps::_key = "Doris");

	std::cout << it->first << std::endl;
	/*
	boost/bimap/support/lambda.hpp 还定义了 boost::bimaps::_data。 函数 modify_data() 可以用来修改 boost::bimap 中的 value 值。
	修改了it的右值,注意类型是int,不用像上面一样加引号。
	*/
	persons.left.modify_data(it, boost::bimaps::_data = 38);
	std::cout << it->second << std::endl;
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值