Boost库容器

1.Boost.Array

1)boost.array和std.vector基本上有相同的操作,只不过boost::array是定长的。

2)boost.array可以用=直接赋值

#include "pch.h"
#include <iostream>
#include <boost/array.hpp>
#include <boost/shared_ptr.hpp>
#include <map>
using namespace std;

typedef boost::array<string, 5> arrName;
int main()
{
	arrName a;
	a = { "zhangsan", "lisi", "wanger", "mazi", "bajie" };
	for (auto item : a)
	{
		cout << item << " ";
	}
	cout << endl;
	a[4] = "sunwukong";
	for (auto item : a)
	{
		cout << item << " ";
	}
	cout << endl;
	system("pause");
}

2.Boost.Unordered

1 )Boost.Unordered 不要求其中的元素是可排序的, 因为它不会做出排序操作。 在排序操作无足轻重时(或是根本不需要), Boost.Unordered 就很合适了。包括boost::unordered_set, boost::unordered_multiset, boost::unordered_map 和 boost::unordered_multimap四类。

2)为了能够快速的查找元素, 我们需要使用 Hash 值。 比如 std::set 要求其中的元素都要是可比较的,而 boost::unordered_set 要求其中的元素都要可计算 Hash 值。所以boost.unordered_set中的key是可以重复的。

3)对于一些自定义的类型,可能是不可hash的,但是可以自己定义其hash函数,其标签必须为hash_value()

typedef boost::unordered_multimap<string, int> NameAge;
typedef boost::unordered_multimap<std::string, int>::value_type NameAgeValue;
int main()
{
	NameAge na;
	na.insert(NameAgeValue("haha", 1));
	na.insert(NameAgeValue("haha", 2));
	na.insert(NameAgeValue("haha", 3));
	for (auto item : na)
	{
		std::cout << item.first << "  :  " << item.second << std::endl;
	}

	system("pause");
}

3.Boost.MultiIndex

跳过。。。

4.Boost.Bimap

Bimap这个容器十分类似于 std::map, 但他不仅可以通过 key 搜索, 还可以用 value 来搜索。(第一次我看成了Bitmap)。

1)实现就是用到两组map,一个是<key, value>,一个是<value, key>

2)如果定义是boost::bimap<string, int>的话,那么bimap中key和value都要求是唯一的,只有key,value定义成

boost::bimaps::multiset_of<type>,才可以允许有重复的情况。

#include "pch.h"
#include <iostream>
#include <boost/bimap/multiset_of.hpp> 
#include <boost/bimap.hpp> 
#include <string> 
#include <boost/typeof/typeof.hpp>
using namespace std;

int main()
{
	//typedef boost::bimap<string, int> bimap;

	typedef boost::bimap<boost::bimaps::multiset_of<std::string>, boost::bimaps::multiset_of<int>> bimap;
	bimap persons;

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

	for (bimap::iterator it = persons.begin(); it != persons.end(); ++it)
	{
		std::cout << it->left << " is " << it->right << " years old." << std::endl;
	}
		
	system("pause");
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏克贝塔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值