English 中文(简体)
Boost multi-index 成员使用混合钥匙
原标题:Boost multi_index composite keys using MEM_FUN
  • 时间:2010-08-13 22:29:37
  •  标签:
  • c++
  • boost

是否有办法利用成员的职能来具体确定复合_key s,促进多种_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++;

}

以上准则代表了我所希望做的工作,但该成员与“设计”;模板不接受BOOST_MULTI_INDEX_CONST_MEM_FUN 宏观上,命令“非经济”和“发展”;

任何人都这样做了?

感谢!

最佳回答

它希望你再次试图在综合案件中打上关键精子,但我认为,你只能是标的。 我认为,你想要取代什么。

    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)>
        >
    >

iii

    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)
        >
    >
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签