本文介绍了如何添加有序唯一索引指定的multi_index_container的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用升压:multi_index_container的。
以下是code

 的#include<串GT;
#包括LT&;升压/ multi_index_container.hpp>
#包括升压/ multi_index / global_fun.hpp
#包括升压/ multi_index / hashed_index.hpp
#包括LT&;提升/ multi_index / sequenced_index.hpp>
#包括升压/ multi_index / mem_fun.hpp
#包括升压/ multi_index / member.hpp
#包括升压/ multi_index / ordered_index.hpp
#包括升压/ multi_index / indexed_by.hpp使用命名空间std;类URI
{
上市:
    字符串str;
    URI(字符串s):STR(S)
    {
    }
    串getUri(){返回海峡; }};
结构UriWithBase
{
    UriWithBase(URI U1,U2 URI)链接(U1),baseLink(U2){}
    URI链接;
    URI baseLink;
};诠释的main()
{
    TYPEDEF的boost :: multi_index :: multi_index_container的<
        UriWithBase *
        提高:: multi_index ::的indexed_by<
        提高:: multi_index ::测序< >
        >
    > UriMultiIndex;    UriMultiIndex m_uris;    URI urib1(www.google.com);
    URI uri1(www.news.google.com);    UriWithBase * ubptr =新UriWithBase(uri1,urib1);    m_uris.push_back(ubptr);
    返回0;
}

现在我想添加一个有序的唯一索引指定的multi_index_container。该指数是在容器元素的独特顺序取决于UriWithBase级,即URI链接的第一要素。
我试过以下,但没有奏效。
提高:: multi_index :: ordered_unique<

 的boost :: multi_index ::标签< uris_by_uri>中
BOOST_MULTI_INDEX_CONST_MEM_FUN(UriWithBasePtr,常量URI和放大器;,(*(UriWithBasePtr))链接。)>


解决方案

有几种方法可以增加一个有序索引:


  1. 通过使用一起

     类URI
    {
    上市:
        字符串str;
        URI(字符串s):STR(S)
        {
        }
        串getUri()const的{返回海峡; }
    };结构UriWithBase
    {
        UriWithBase(URI U1,U2 URI)链接(U1),baseLink(U2){}
        URI链接;
        URI baseLink;
    };结构CUriComparator {
        布尔运算符()(const的URI和LH,常量URI和湿度)常量{
            返回lh.str< rh.str;
        }
    };结构uris_by_uri_and_cr​​awl_state {};TYPEDEF的boost :: multi_index :: multi_index_container的<
        UriWithBase *
        提高:: multi_index ::的indexed_by<
          提高:: multi_index ::测序<>中
          提高:: multi_index :: ordered_unique<
            提高:: multi_index ::标签< uris_by_uri_and_cr​​awl_state>中
            BOOST_MULTI_INDEX_MEMBER(UriWithBase,URI链接),
            CUriComparator
          >
        >
    > UriMultiIndex;


  2. 通过定义一个比较 URI 类的运营商

     类URI
    {
    上市:
        字符串str;
        URI(字符串s):STR(S)
        {
        }
        串getUri(){返回海峡; }
        布尔运算符< (const的URI和湿度)常量{
            返回这个 - > STR< rh.str;
        }
    };结构UriWithBase
    {
        UriWithBase(URI U1,U2 URI)链接(U1),baseLink(U2){}
        URI链接;
        URI baseLink;
    };结构uris_by_uri_and_cr​​awl_state {};TYPEDEF的boost :: multi_index :: multi_index_container的<
        UriWithBase *
        提高:: multi_index ::的indexed_by<
          提高:: multi_index ::测序<>中
            提高:: multi_index :: ordered_unique<
                提高:: multi_index ::标签< uris_by_uri_and_cr​​awl_state>中
                BOOST_MULTI_INDEX_MEMBER(UriWithBase,URI链接)
            >
        >
    > UriMultiIndex;


  3. 通过声明的方法( UriWithBase :: getLinkUri ),其中返回 UriWithBase 类的 STR 成员值。

    code>链接

     类URI
    {
    上市:
        字符串str;
        URI(字符串s):STR(S)
        {
        }
        串getUri()const的{返回海峡; }
    };结构UriWithBase
    {
        UriWithBase(URI U1,U2 URI)链接(U1),baseLink(U2){}
        URI链接;
        URI baseLink;
    上市:
        字符串getLinkUri(无效)常量{
            返回link.getUri();
        }
    };结构uris_by_uri_and_cr​​awl_state {};TYPEDEF的boost :: multi_index :: multi_index_container的<
        UriWithBase *
        提高:: multi_index ::的indexed_by<
            提高:: multi_index ::测序<>中
            提高:: multi_index :: ordered_unique<
                提高:: multi_index ::标签< uris_by_uri_and_cr​​awl_state>中
                BOOST_MULTI_INDEX_CONST_MEM_FUN(UriWithBase,字符串getLinkUri)
            >
        >
    > UriMultiIndex;


I'm using boost:multi_index_container.following is the code

#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;
}

Now I want to add a ordered unique index specifies in multi_index_container. This index is for unique order of elements in the container depends upon the first element of "UriWithBase" class i.e. uri link.I tried following but not worked.boost::multi_index::ordered_unique<

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

There a few ways to add an ordered index:

  1. by using the ordered_unique specifier together with a custom comparator

    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. by defining a compare operator of the 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;
    

  3. by declaring a method (UriWithBase::getLinkUri) in the UriWithBase class which returns the link's str member value.

    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;
    

这篇关于如何添加有序唯一索引指定的multi_index_container的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 05:28