gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:45:43 25 4
gpt4 key购买 nike

我正在使用 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 链接。我尝试关注但没有成功。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;

关于c++ - 如何在 multi_index_container 中添加指定的有序唯一索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29255152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com