C++ Boost多指标的上界
我正在尝试学习如何使用Boost的多索引,我已经专门研究了。我如何修改此项以仅返回年龄小于某个数字(或id等)的员工 相关代码:C++ Boost多指标的上界,c++,boost,multi-index,C++,Boost,Multi Index,我正在尝试学习如何使用Boost的多索引,我已经专门研究了。我如何修改此项以仅返回年龄小于某个数字(或id等)的员工 相关代码: /* Boost.MultiIndex basic example. * * Copyright 2003-2008 Joaquin M Lopez Munoz. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.t
/* Boost.MultiIndex basic example.
*
* Copyright 2003-2008 Joaquin M Lopez Munoz.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See http://www.boost.org/libs/multi_index for library home page.
*/
#if !defined(NDEBUG)
#define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
#define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
#endif
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
using boost::multi_index_container;
using namespace boost::multi_index;
/* an employee record holds its ID, name and age */
struct employee
{
int id;
std::string name;
int age;
employee(int id_,std::string name_,int age_):id(id_),name(name_),age(age_){}
friend std::ostream& operator<<(std::ostream& os,const employee& e)
{
os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
return os;
}
};
/* tags for accessing the corresponding indices of employee_set */
struct id{};
struct name{};
struct age{};
/* see Compiler specifics: Use of member_offset for info on
* BOOST_MULTI_INDEX_MEMBER
*/
/* Define a multi_index_container of employees with following indices:
* - a unique index sorted by employee::int,
* - a non-unique index sorted by employee::name,
* - a non-unique index sorted by employee::age.
*/
typedef multi_index_container<
employee,
indexed_by<
ordered_unique<
tag<id>, BOOST_MULTI_INDEX_MEMBER(employee,int,id)>,
ordered_non_unique<
tag<name>,BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>,
ordered_non_unique<
tag<age>, BOOST_MULTI_INDEX_MEMBER(employee,int,age)> >
> employee_set;
template<typename Tag,typename MultiIndexContainer>
void print_out_by(
const MultiIndexContainer& s,
Tag* =0 /* fixes a MSVC++ 6.0 bug with implicit template function parms */
)
{
/* obtain a reference to the index tagged by Tag */
const typename boost::multi_index::index<MultiIndexContainer,Tag>::type& i=
get<Tag>(s);
typedef typename MultiIndexContainer::value_type value_type;
/* dump the elements of the index to cout */
std::copy(i.begin(),i.end(),std::ostream_iterator<value_type>(std::cout));
}
int main()
{
employee_set es;
es.insert(employee(0,"Joe",31));
es.insert(employee(1,"Robert",27));
es.insert(employee(2,"John",40));
/* next insertion will fail, as there is an employee with
* the same ID
*/
es.insert(employee(2,"Aristotle",2387));
es.insert(employee(3,"Albert",20));
es.insert(employee(4,"John",57));
/* list the employees sorted by ID, name and age */
std::cout<<"by ID"<<std::endl;
print_out_by<id>(es);
std::cout<<std::endl;
std::cout<<"by name"<<std::endl;
print_out_by<name>(es);
std::cout<<std::endl;
std::cout<<"by age"<<std::endl;
print_out_by<age>(es);
std::cout<<std::endl;
return 0;
}
/*Boost.MultiIndex基本示例。
*
*版权所有2003-2008 Joaquin M Lopez Munoz。
*根据Boost软件许可证1.0版发布。
*(参见随附的文件LICENSE_1_0.txt或副本,网址为
* http://www.boost.org/LICENSE_1_0.txt)
*
*看http://www.boost.org/libs/multi_index 图书馆主页。
*/
#如果!已定义(NDEBUG)
#定义BOOST\u多索引\u启用\u不变\u检查
#定义BOOST\u MULTI\u INDEX\u ENABLE\u SAFE\u模式
#恩迪夫
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用boost::multi_index_容器;
使用名称空间boost::multi_索引;
/*员工记录保存其ID、姓名和年龄*/
结构雇员
{
int-id;
std::字符串名;
智力年龄;
雇员(intid,std::string name,intage):id(id),name(name),age(age){
friend std::ostream&operator您可以对要查询的有序索引使用上界(或下界,具体取决于您的要求):
auto const& by_age = get<age>(es);
auto upper = by_age.upper_bound(32);
查看它
完整代码供参考:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
using boost::multi_index_container;
using namespace boost::multi_index;
struct employee {
int id;
std::string name;
int age;
friend std::ostream& operator<<(std::ostream& os,const employee& e) {
return os << e.id << " " << e.name << " " << e.age << std::endl;
}
};
typedef multi_index_container<
employee,
indexed_by<
ordered_unique<tag<struct id> , BOOST_MULTI_INDEX_MEMBER(employee, int , id)> ,
ordered_non_unique<tag<struct name>, BOOST_MULTI_INDEX_MEMBER(employee, std::string, name)> ,
ordered_non_unique<tag<struct age> , BOOST_MULTI_INDEX_MEMBER(employee, int , age)> >
> employee_set;
int main()
{
employee_set es;
es.insert(employee{ 0, "Joe", 31 });
es.insert(employee{ 1, "Robert", 27 });
es.insert(employee{ 2, "John", 40 });
// next insertion will fail, as there is an employee with the same ID
es.insert(employee{ 2, "Aristotle", 2387 });
es.insert(employee{ 3, "Albert", 20 });
es.insert(employee{ 4, "John", 57 });
auto const& by_age = get<age>(es);
auto upper = by_age.upper_bound(32);
for (auto& e : boost::make_iterator_range(by_age.begin(), upper))
std::cout << e.name << " is " << e.age << " years old\n";
}
#包括
#包括
#包括
#包括
#包括
使用boost::multi_index_容器;
使用名称空间boost::multi_索引;
结构雇员{
int-id;
std::字符串名;
智力年龄;
friend std::ostream&Operator为什么您需要将by_age定义为auto?您不需要。为什么您会这样认为?它只是比我认为的boost::multi_index::index::type const&by_age=get(es)
更便于维护。在这种情况下,“拼写出类型”甚至不添加任何文档值,因为该类型只是一个类型函数表达式,因此与auto
我明白了,好吧,这很有意义!您是如何理解该类型的?@user3707153它不是该类型;它是一个编译时类型表达式,计算结果为实际的ordered\u index
typee real type可以通过编译器自身查看,也可以通过编译器自身查看:谢谢您的帮助!
Albert is 20 years old
Robert is 27 years old
Joe is 31 years old
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
using boost::multi_index_container;
using namespace boost::multi_index;
struct employee {
int id;
std::string name;
int age;
friend std::ostream& operator<<(std::ostream& os,const employee& e) {
return os << e.id << " " << e.name << " " << e.age << std::endl;
}
};
typedef multi_index_container<
employee,
indexed_by<
ordered_unique<tag<struct id> , BOOST_MULTI_INDEX_MEMBER(employee, int , id)> ,
ordered_non_unique<tag<struct name>, BOOST_MULTI_INDEX_MEMBER(employee, std::string, name)> ,
ordered_non_unique<tag<struct age> , BOOST_MULTI_INDEX_MEMBER(employee, int , age)> >
> employee_set;
int main()
{
employee_set es;
es.insert(employee{ 0, "Joe", 31 });
es.insert(employee{ 1, "Robert", 27 });
es.insert(employee{ 2, "John", 40 });
// next insertion will fail, as there is an employee with the same ID
es.insert(employee{ 2, "Aristotle", 2387 });
es.insert(employee{ 3, "Albert", 20 });
es.insert(employee{ 4, "John", 57 });
auto const& by_age = get<age>(es);
auto upper = by_age.upper_bound(32);
for (auto& e : boost::make_iterator_range(by_age.begin(), upper))
std::cout << e.name << " is " << e.age << " years old\n";
}