每一部分都单独注释的,运行时取消注释,将其他部分注释起来就可以。
/*
展示了 Boost.MultiIndex 中剩下的三个接口:
boost::multi_index::sequenced,
boost::multi_index::ordered_non_unique
和 boost::multi_index::random_access。
*/
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/member.hpp>
#include <iostream>
#include <string>
struct person
{
std::string name;
int age;
person(const std::string &n, int a)
: name(n), age(a)
{
}
};
/*
(下面第四行)
boost::multi_index::sequenced 接口让我们可以像使用 std::list 一样的使用 MultiIndex。
这个接口定义起来十分容易: 你不用为它传递任何模板参数。
person 类型的对象在容器中就是像 list 一样按照加入的顺序来排列的。
(下面第五行)
boost::multi_index::ordered_non_unique 接口, 容器中的对象会自动的排序。
你在定义容器时就必须指定接口的排序规则。 示例中的对象 person 就是以 age 来排序的,
它借助了辅助类 boost::multi_index::member 来实现这一功能。
boost::multi_index::ordered_non_unique 为我们提供了一些特别的函数来查找特定范围的数据。
通过使用 lower_bound() 和 upper_bound(), 示例实现了对所有 30 岁至 40 岁的 person 的查询。
要注意因为容器中的数据是有序的, 所以才提供了这些函数, 其他接口中并不提供这些函数。
(下面第10行)
最后一个接口是: boost::multi_index::random_access, 他让我们可以像使用 std::vector 一样使用 MultiIndex 容器。
你又可以使用你熟悉的 operator[]() 和 at() 操作了。
附:boost::multi_index::random_access 已经被完整的包含在了 boost::multi_index::sequenced 接口中。
所以当你使用 boost::multi_index::random_access 的时候, 你也可以使用 boost::multi_index::sequenced 接口中的所有函数。
*/
typedef boost::multi_index::multi_index_container<
person,
boost::multi_index::indexed_by<
boost::multi_index::sequenced<>,
boost::multi_index::ordered_non_unique<
boost::multi_index::member<
person, int, &person::age
>
>,
boost::multi_index::random_access<>
>
> person_multi;
class person1
{
public:
person1(const std::string &n, int a)
: name(n), age(a)
{
}
bool operator<(const person1 &p) const
{
return age < p.age;
}
std::string get_name() const
{
return name;
}
private:
std::string name;
int age;
};
/*
第四行到第五行:是将元素排了序的,在类里重载了< 符号
(下面第五行)
键值提取器boost::multi_index::identity可以使用容器中的数据类型作为键值
示例中, 就需要 person1 类是可排序的, 因为它已经作为了接口 boost::multi_index::ordered_unique 的键值。
在示例里, 它是通过重载 operator<() 操作符来实现的。
(下面第8行)
头文件 boost/multi_index/mem_fun.hpp 定义了两个可以把函数返回值作为键值的键值提取器:
boost::multi_index::const_mem_fun 和 boost::multi_index::mem_fun 。
附:Boost.MultiIndex 还提供了两个键值提取器: boost::multi_index::global_fun 和 boost::multi_index::composite_key。
前一个适用于独立的函数或者静态函数, 后一个允许你将几个键值提取器组合成一个新的的键值提取器。
*/
typedef boost::multi_index::multi_index_container<
person1,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<
boost::multi_index::identity<person1>
>,
boost::multi_index::hashed_unique<
boost::multi_index::const_mem_fun<
person1, std::string, &person1::get_name
>
>
>
> person_multi1;
int test03()
{
//person_multi persons;
//persons.push_back(person("Boris", 31));
//persons.push_back(person("Anton", 31));
//persons.push_back(person("Caesar", 25));
使用第二个接口访问
//const person_multi::nth_index<1>::type &ordered_index = persons.get<1>();
//person_multi::nth_index<1>::type::iterator lower = ordered_index.lower_bound(30);
//person_multi::nth_index<1>::type::iterator upper = ordered_index.upper_bound(40);
//for (; lower != upper; ++lower)
// std::cout << lower->name << std::endl;
使用第三个接口访问
//const person_multi::nth_index<2>::type &random_access_index = persons.get<2>();
//std::cout << random_access_index[2].name << std::endl;
/*
************键值提取器
*/
person_multi1 persons;
persons.insert(person1("Boris", 31));
persons.insert(person1("Anton", 31));
persons.insert(person1("Caesar", 25));
/*
用到了 get_name() 的返回值作为键值。
*/
std::cout << persons.begin()->get_name() << std::endl;
const person_multi1::nth_index<1>::type &hashed_index = persons.get<1>();
std::cout << hashed_index.count("Boris") << std::endl;
system("pause");
return 0;
}