如何添加有序的唯一索引在multi_index_container中指定

时间:2015-03-25 12:03:32

标签: c++ boost boost-multi-index

我正在使用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”类的第一个元素,即uri链接。 我试过以下但没有工作。 升压:: multi_index :: ordered_unique&LT;

    boost::multi_index::tag<uris_by_uri>,
BOOST_MULTI_INDEX_CONST_MEM_FUN(UriWithBasePtr, const uri&, (*(UriWithBasePtr)).link)>

2 个答案:

答案 0 :(得分:1)

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;

答案 1 :(得分:0)

有几种方法可以添加有序索引:

  1. 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;
    
  2. 通过定义uri

    的比较运算符
    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;
    
  3. 通过在UriWithBase::getLinkUri类中声明一个返回UriWithBase 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;