C++ boost multiindex:以值类型作为参数的下界
我想将C++ boost multiindex:以值类型作为参数的下界,c++,boost,boost-multi-index,C++,Boost,Boost Multi Index,我想将下限与Boost多索引容器的值类型一起使用。到目前为止,我只通过显式提取成员来实现这一点: #include <boost/multi_index_container.hpp> #include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/composite_key.hpp> #include <boost/multi_index/member.hpp>
下限
与Boost多索引容器的值类型
一起使用。到目前为止,我只通过显式提取成员来实现这一点:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/member.hpp>
#include <string>
struct name {
std::string firstname;
std::string lastname;
name(const std::string & firstname, const std::string & lastname) :
firstname(firstname), lastname(lastname) {}
};
typedef boost::multi_index::multi_index_container<
name,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<
boost::multi_index::composite_key<
name,
boost::multi_index::member<name, std::string, &name::lastname>,
boost::multi_index::member<name, std::string, &name::firstname>
>,
boost::multi_index::composite_key_compare<
std::less<std::string>,
std::less<std::string>
>
>
>
> NameIndex;
int main(void) {
NameIndex nameindex;
nameindex.insert(name("Alfred", "Ammer"));
nameindex.insert(name("Martin", "Mauser"));
// In my real code, I get this object passed.
name lookupname("Hans", "Hoffer");
// Does not compile
//auto it = nameindex.get<0>().lower_bound(lookupname);
// compiles, but I have to take explicitly list the members - in the right order
auto it = nameindex.get<0>().lower_bound(std::make_tuple(lookupname.lastname, lookupname.firstname));
}
#包括
#包括
#包括
#包括
#包括
结构名{
std::stringfirstname;
std::字符串lastname;
名称(常量std::string和firstname,常量std::string和lastname):
名字(名字),名字(名字){}
};
typedef boost::multi_index::multi_index_容器<
名称
boost::多索引::按索引索引<
boost::多索引::有序唯一<
boost::多索引::复合索引键<
名称
boost::multi_index::member,
boost::multi_index::member
>,
boost::多索引::组合键\u比较<
std::更少,
std::less
>
>
>
>名称索引;
内部主(空){
名称索引名称索引;
名称索引。插入(名称(“阿尔弗雷德”、“安默”);
名称索引。插入(名称(“马丁”、“毛瑟”);
//在我的真实代码中,我传递了这个对象。
名称查找名称(“Hans”、“Hoffer”);
//不编译
//auto it=nameindex.get().下限(lookupname);
//编译,但我必须以正确的顺序显式列出成员
auto it=nameindex.get().下限(std::make_tuple(lookupname.lastname,lookupname.firstname));
}
如何避免提取成员?请尝试以下操作:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/member.hpp>
#include <string>
struct name {
std::string firstname;
std::string lastname;
name(const std::string & firstname, const std::string & lastname) :
firstname(firstname), lastname(lastname) {}
};
struct name_compare:
boost::multi_index::composite_key_compare<
std::less<std::string>,
std::less<std::string>
>
{
using base=boost::multi_index::composite_key_compare<
std::less<std::string>,
std::less<std::string>
>;
using base::operator();
template<typename T>
bool operator()(const T& x,const name& y)const
{
return base::operator()(
x,
std::make_tuple(std::ref(y.firstname),std::ref(y.lastname)));
}
template<typename T>
bool operator()(const name& x,const T& y)const
{
return base::operator()(
std::make_tuple(std::ref(x.firstname),std::ref(x.lastname)),
y);
}
};
typedef boost::multi_index::multi_index_container<
name,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<
boost::multi_index::composite_key<
name,
boost::multi_index::member<name, std::string, &name::lastname>,
boost::multi_index::member<name, std::string, &name::firstname>
>,
name_compare
>
>
> NameIndex;
int main(void) {
NameIndex nameindex;
nameindex.insert(name("Alfred", "Ammer"));
nameindex.insert(name("Martin", "Mauser"));
// In my real code, I get this object passed.
name lookupname("Hans", "Hoffer");
auto it = nameindex.get<0>().lower_bound(lookupname);
// this also works, as before
it = nameindex.get<0>().lower_bound(std::make_tuple(lookupname.lastname, lookupname.firstname));
}
#包括
#包括
#包括
#包括
#包括
结构名{
std::stringfirstname;
std::字符串lastname;
名称(常量std::string和firstname,常量std::string和lastname):
名字(名字),名字(名字){}
};
结构名称\u比较:
boost::多索引::组合键\u比较<
std::更少,
std::less
>
{
使用base=boost::multi_index::composite_key\u compare<
std::更少,
std::less
>;
使用base::operator();
模板
布尔运算符()
{
返回基::运算符()(
x,,
std::make_tuple(std::ref(y.firstname)、std::ref(y.lastname));
}
模板
布尔运算符()
{
返回基::运算符()(
std::make_tuple(std::ref(x.firstname)、std::ref(x.lastname)),
y) );
}
};
typedef boost::multi_index::multi_index_容器<
名称
boost::多索引::按索引索引<
boost::多索引::有序唯一<
boost::多索引::复合索引键<
名称
boost::multi_index::member,
boost::multi_index::member
>,
名称比较
>
>
>名称索引;
内部主(空){
名称索引名称索引;
名称索引。插入(名称(“阿尔弗雷德”、“安默”);
名称索引。插入(名称(“马丁”、“毛瑟”);
//在我的真实代码中,我传递了这个对象。
名称查找名称(“Hans”、“Hoffer”);
auto it=nameindex.get().下限(lookupname);
//和以前一样,这也是有效的
it=nameindex.get().下限(std::make_tuple(lookupname.lastname,lookupname.firstname));
}
写入
auto it = nameindex.get<0>().lower_bound(
nameindex.get<0>().key_extractor()(lookupname));
auto it=nameindex.get().下限(
nameindex.get().key_提取器()(lookupname));
这并不能真正解决问题。虽然我现在可以对name
对象使用lower_bound,但我仍然必须在一些帮助程序代码中反映复合键的顺序。事实上,您已经证明了这可能是危险的,因为您假设了顺序,尽管我将其定义为。使用multi_index::composite_键定义的顺序真的不可能吗?好的,我明白你的意思。看看我的另一个答案。