提升使用MEM_FUN multi_index组合键 [英] Boost multi_index composite keys using MEM_FUN

查看:221
本文介绍了提升使用MEM_FUN multi_index组合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用成员函数composite_key的在提升的multi_index_container的规范?

Is there a way to use member functions in the specification of composite_key's in boost multi_index_container's?

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/member.hpp>
#include <iostream>
#include <string>

using namespace boost::multi_index;
using namespace std;

class Name {
public:
    Name(const string &first, const string &middle, const string &last) : m_first(first) , m_middle(middle) , m_last(last) {}

    friend std::ostream& operator << (ostream& os,const Name& n)    {
        os << n.m_first << " " << n.m_middle << " " << n.m_last << endl;
        return os;
    }

    const string &first() const { return m_first; }
    const string &middle() const { return m_middle; }
    const string &last() const { return m_last; }
private:
    string m_first, m_middle, m_last;
};

struct first {};
struct middle {};
struct last {};
struct last_first {};


typedef multi_index_container <
    Name, 
    indexed_by<
        ordered_non_unique<tag<first>, BOOST_MULTI_INDEX_CONST_MEM_FUN( Name, const string &, first)
        >,
        ordered_non_unique<tag<middle>, BOOST_MULTI_INDEX_CONST_MEM_FUN(Name, const string &, middle)
        >,
        ordered_non_unique<tag<last>, BOOST_MULTI_INDEX_CONST_MEM_FUN(Name, const string &, last)
        >,
        ordered_non_unique< 
            composite_key<
                Name,
                member<tag<last>, BOOST_MULTI_INDEX_CONST_MEM_FUN(Name, const string &, last)>,
                member<tag<first>, BOOST_MULTI_INDEX_CONST_MEM_FUN( Name, const string &, first)>
            >
        >
    >
> Name_set;

typedef Name_set::index<first>::type Name_set_by_first;
typedef Name_set::index<middle>::type Name_set_by_middle;
typedef Name_set::index<last>::type Name_set_by_last;

void main() {

    Name_set names;
    names.insert(Name("robert", "shawn", "mitchell"));
    names.insert(Name("ravi", "john", "spaceman"));
    names.insert(Name("david", "abel", "mccoy"));
    names.insert(Name("steven", "xavier", "anser"));
    names.insert(Name("kris", "nomiddlename", "spigha"));
    names.insert(Name("jina", "wilson", "fabrice"));
    names.insert(Name("zebbo", "aniston", "michaels"));
    names.insert(Name("antonio", "magician", "esfandiari"));
    names.insert(Name("nora", "iris", "mitchell"));

    cout << "SORTED BY FIRST NAME" << endl;
    for (Name_set_by_first::iterator itf = names.get<first>().begin(); itf != names.get<first>().end(); ++itf)
        cout << *itf;

    cout << "SORTED BY MIDDLE NAME" << endl;
    for (Name_set_by_middle::iterator itm = names.get<middle>().begin(); itm != names.get<middle>().end(); ++itm)
        cout << *itm;

    cout << "SORTED BY LAST NAME" << endl;
    for (Name_set_by_last::iterator itl = names.get<last>().begin(); itl != names.get<last>().end(); ++itl)
        cout << *itl;

    Name_set_by_last::iterator mitchells = names.get<last>().find("mitchell");
    while (mitchells->last() == "mitchell")
        cout << *mitchells++;

}

在code以上重presents什么,我想这样做,但是成员&LT;>模板不接受BOOST_MULTI_INDEX_CONST_MEM_FUN宏方式ordered_non_unique&的LT;>确实

The code above represents what I would like to do, but the member<> template does not accept the BOOST_MULTI_INDEX_CONST_MEM_FUN macro the way ordered_non_unique<> does.

任何人遇到这样?

谢谢!

推荐答案

它看起来像你想标记在复合材料的情况下键提取,但我认为你只能标记索引。我想你想要的是替换

It looks like you're trying to tag the key extractors in the composite case, but I think you can only tag indexes. I think what you want is to replace

    ordered_non_unique< 
        composite_key<
            Name,
            member<tag<last>, BOOST_MULTI_INDEX_CONST_MEM_FUN(Name, const string &, last)>,
            member<tag<first>, BOOST_MULTI_INDEX_CONST_MEM_FUN( Name, const string &, first)>
        >
    >

    ordered_non_unique< tag<somenewtagtype>
        composite_key<
            Name,
            BOOST_MULTI_INDEX_CONST_MEM_FUN(Name, const string &, last),
            BOOST_MULTI_INDEX_CONST_MEM_FUN(Name, const string &, first)
        >
    >

这篇关于提升使用MEM_FUN multi_index组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆