如何添加 multi_index_container 中指定的有序唯一索引
How to add the ordered unique index specifies in multi_index_container
我正在使用 boost:multi_index_container。
以下是代码
#include <string>
#include <boost/multi_index_container.hpp>
#include "boost/multi_index/global_fun.hpp"
#include "boost/multi_index/hashed_index.hpp"
#include <boost/multi_index/sequenced_index.hpp>
#include "boost/multi_index/mem_fun.hpp"
#include "boost/multi_index/member.hpp"
#include "boost/multi_index/ordered_index.hpp"
#include "boost/multi_index/indexed_by.hpp"
using namespace std;
class uri
{
public:
string str;
uri(string s) :str(s)
{
}
string getUri(){ return str; }
};
struct UriWithBase
{
UriWithBase(uri u1, uri u2) :link(u1), baseLink(u2){}
uri link;
uri baseLink;
};
int main()
{
typedef boost::multi_index::multi_index_container <
UriWithBase*,
boost::multi_index::indexed_by <
boost::multi_index::sequenced < >
>
> UriMultiIndex;
UriMultiIndex m_uris;
uri urib1("www.google.com");
uri uri1("www.news.google.com");
UriWithBase *ubptr = new UriWithBase(uri1, urib1);
m_uris.push_back(ubptr);
return 0;
}
现在我想添加在 multi_index_container 中指定的有序唯一索引。该索引用于容器中元素的唯一顺序,取决于 "UriWithBase" class 的第一个元素,即 uri link。
我尝试关注但没有成功。
boost::multi_index::ordered_unique<
boost::multi_index::tag<uris_by_uri>,
BOOST_MULTI_INDEX_CONST_MEM_FUN(UriWithBasePtr, const uri&, (*(UriWithBasePtr)).link)>
class uri
{
public:
string str;
uri(string s) :str(s)
{
}
// note we made this const
string getUri() const { return str; }
};
inline bool operator<(const uri& x,const uri& y)
{
return x.getUri()<y.getUri();
}
...
typedef boost::multi_index::multi_index_container <
UriWithBase*,
boost::multi_index::indexed_by <
boost::multi_index::sequenced < >,
boost::multi_index::ordered_unique<
boost::multi_index::tag<uris_by_uri_and_crawl_state>,
boost::multi_index::member<UriWithBase,uri,&UriWithBase::link>
>
>
> UriMultiIndex;
有几种添加有序索引的方法:
通过使用 ordered_unique specifier 和自定义比较器
class uri
{
public:
string str;
uri(string s) :str(s)
{
}
string getUri() const { return str; }
};
struct UriWithBase
{
UriWithBase(uri u1, uri u2) :link(u1), baseLink(u2){}
uri link;
uri baseLink;
};
struct CUriComparator {
bool operator()(const uri &lh, const uri &rh) const {
return lh.str < rh.str;
}
};
struct uris_by_uri_and_crawl_state {};
typedef boost::multi_index::multi_index_container <
UriWithBase*,
boost::multi_index::indexed_by <
boost::multi_index::sequenced<>,
boost::multi_index::ordered_unique <
boost::multi_index::tag<uris_by_uri_and_crawl_state>,
BOOST_MULTI_INDEX_MEMBER(UriWithBase, uri, link),
CUriComparator
>
>
> UriMultiIndex;
通过定义 uri
class
的比较运算符
class uri
{
public:
string str;
uri(string s) :str(s)
{
}
string getUri(){ return str; }
bool operator < (const uri &rh) const {
return this->str < rh.str;
}
};
struct UriWithBase
{
UriWithBase(uri u1, uri u2) :link(u1), baseLink(u2){}
uri link;
uri baseLink;
};
struct uris_by_uri_and_crawl_state {};
typedef boost::multi_index::multi_index_container <
UriWithBase*,
boost::multi_index::indexed_by <
boost::multi_index::sequenced<>,
boost::multi_index::ordered_unique <
boost::multi_index::tag<uris_by_uri_and_crawl_state>,
BOOST_MULTI_INDEX_MEMBER(UriWithBase, uri, link)
>
>
> UriMultiIndex;
通过在 UriWithBase
class 中声明方法 (UriWithBase::getLinkUri
),其中 returns link
的 str
会员价值.
class uri
{
public:
string str;
uri(string s) :str(s)
{
}
string getUri() const { return str; }
};
struct UriWithBase
{
UriWithBase(uri u1, uri u2) :link(u1), baseLink(u2){}
uri link;
uri baseLink;
public:
string getLinkUri(void) const {
return link.getUri();
}
};
struct uris_by_uri_and_crawl_state {};
typedef boost::multi_index::multi_index_container <
UriWithBase*,
boost::multi_index::indexed_by <
boost::multi_index::sequenced<>,
boost::multi_index::ordered_unique <
boost::multi_index::tag<uris_by_uri_and_crawl_state>,
BOOST_MULTI_INDEX_CONST_MEM_FUN(UriWithBase, string, getLinkUri)
>
>
> UriMultiIndex;
我正在使用 boost:multi_index_container。 以下是代码
#include <string>
#include <boost/multi_index_container.hpp>
#include "boost/multi_index/global_fun.hpp"
#include "boost/multi_index/hashed_index.hpp"
#include <boost/multi_index/sequenced_index.hpp>
#include "boost/multi_index/mem_fun.hpp"
#include "boost/multi_index/member.hpp"
#include "boost/multi_index/ordered_index.hpp"
#include "boost/multi_index/indexed_by.hpp"
using namespace std;
class uri
{
public:
string str;
uri(string s) :str(s)
{
}
string getUri(){ return str; }
};
struct UriWithBase
{
UriWithBase(uri u1, uri u2) :link(u1), baseLink(u2){}
uri link;
uri baseLink;
};
int main()
{
typedef boost::multi_index::multi_index_container <
UriWithBase*,
boost::multi_index::indexed_by <
boost::multi_index::sequenced < >
>
> UriMultiIndex;
UriMultiIndex m_uris;
uri urib1("www.google.com");
uri uri1("www.news.google.com");
UriWithBase *ubptr = new UriWithBase(uri1, urib1);
m_uris.push_back(ubptr);
return 0;
}
现在我想添加在 multi_index_container 中指定的有序唯一索引。该索引用于容器中元素的唯一顺序,取决于 "UriWithBase" class 的第一个元素,即 uri link。 我尝试关注但没有成功。 boost::multi_index::ordered_unique<
boost::multi_index::tag<uris_by_uri>,
BOOST_MULTI_INDEX_CONST_MEM_FUN(UriWithBasePtr, const uri&, (*(UriWithBasePtr)).link)>
class uri
{
public:
string str;
uri(string s) :str(s)
{
}
// note we made this const
string getUri() const { return str; }
};
inline bool operator<(const uri& x,const uri& y)
{
return x.getUri()<y.getUri();
}
...
typedef boost::multi_index::multi_index_container <
UriWithBase*,
boost::multi_index::indexed_by <
boost::multi_index::sequenced < >,
boost::multi_index::ordered_unique<
boost::multi_index::tag<uris_by_uri_and_crawl_state>,
boost::multi_index::member<UriWithBase,uri,&UriWithBase::link>
>
>
> UriMultiIndex;
有几种添加有序索引的方法:
通过使用 ordered_unique specifier 和自定义比较器
class uri { public: string str; uri(string s) :str(s) { } string getUri() const { return str; } }; struct UriWithBase { UriWithBase(uri u1, uri u2) :link(u1), baseLink(u2){} uri link; uri baseLink; }; struct CUriComparator { bool operator()(const uri &lh, const uri &rh) const { return lh.str < rh.str; } }; struct uris_by_uri_and_crawl_state {}; typedef boost::multi_index::multi_index_container < UriWithBase*, boost::multi_index::indexed_by < boost::multi_index::sequenced<>, boost::multi_index::ordered_unique < boost::multi_index::tag<uris_by_uri_and_crawl_state>, BOOST_MULTI_INDEX_MEMBER(UriWithBase, uri, link), CUriComparator > > > UriMultiIndex;
通过定义
的比较运算符uri
classclass uri { public: string str; uri(string s) :str(s) { } string getUri(){ return str; } bool operator < (const uri &rh) const { return this->str < rh.str; } }; struct UriWithBase { UriWithBase(uri u1, uri u2) :link(u1), baseLink(u2){} uri link; uri baseLink; }; struct uris_by_uri_and_crawl_state {}; typedef boost::multi_index::multi_index_container < UriWithBase*, boost::multi_index::indexed_by < boost::multi_index::sequenced<>, boost::multi_index::ordered_unique < boost::multi_index::tag<uris_by_uri_and_crawl_state>, BOOST_MULTI_INDEX_MEMBER(UriWithBase, uri, link) > > > UriMultiIndex;
通过在
UriWithBase
class 中声明方法 (UriWithBase::getLinkUri
),其中 returnslink
的str
会员价值.class uri { public: string str; uri(string s) :str(s) { } string getUri() const { return str; } }; struct UriWithBase { UriWithBase(uri u1, uri u2) :link(u1), baseLink(u2){} uri link; uri baseLink; public: string getLinkUri(void) const { return link.getUri(); } }; struct uris_by_uri_and_crawl_state {}; typedef boost::multi_index::multi_index_container < UriWithBase*, boost::multi_index::indexed_by < boost::multi_index::sequenced<>, boost::multi_index::ordered_unique < boost::multi_index::tag<uris_by_uri_and_crawl_state>, BOOST_MULTI_INDEX_CONST_MEM_FUN(UriWithBase, string, getLinkUri) > > > UriMultiIndex;