向现有多索引容器添加更多索引

时间:2014-02-04 02:33:25

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

我目前正在使用Boost :: multi_index_container,它的工作效果很好。但是我想封装代码并创建一个看起来像这样的模板类

template<class T>
class LookUp
{
    boost::multi_index<T, indexed_by<___predefined indices___> > myTable;

    void Foo();
}

本质上,这个包装器中使用了预定义的索引,但对于专门的T,我还想添加其他索引。是否可以向myTable添加其他索引?可能是其他模板参数?但其他指数的数量尚不清楚。

1 个答案:

答案 0 :(得分:0)

有特点。让我们发明:

namespace traits
{
    template <typename T> struct predefined_indexes;
}


template <typename T> using MultiIndex = multi_index_container<T, traits::predefined_indexes<T> >;

现在的想法是我们可以实例化我们的MIC:

MultiIndex<T> myTable;

让我们试试一个简单的例子:

//////
// example from http://www.boost.org/doc/libs/1_55_0/libs/multi_index/example/fun_key.cpp
struct name_record
{
    name_record(std::string given_name_,std::string family_name_): given_name(given_name_),family_name(family_name_) {}
    std::string name() const { return family_name + " " + given_name; }
    std::string given_name, family_name;
};

std::string::size_type name_record_length(const name_record& r)
{
    return r.name().size();
}

namespace traits
{
    template <> struct predefined_indexes<name_record> :
        indexed_by<
            ordered_unique<
                BOOST_MULTI_INDEX_CONST_MEM_FUN(name_record,std::string,name)
            >,
            ordered_non_unique<
                global_fun<const name_record&,std::string::size_type,name_record_length>
            >
        >
    {
    };
}

查看 Working Live On Coliru

int main()
{
    using MyTable = MultiIndex<name_record>;
    MyTable myTable;

    myTable.insert(name_record("Joe","Smith"));
    myTable.insert(name_record("Robert","Nightingale"));
    myTable.insert(name_record("Robert","Brown"));
    myTable.insert(name_record("Marc","Tuxedo"));

    /* list the names in myTable in phonebook order */
    std::cout << "Phonenook order\n" << "---------------" << std::endl;
    for(MyTable::iterator it=myTable.begin();it!=myTable.end();++it){
        std::cout << it->name() << std::endl;
    }

    /* list the names in myTable according to their length*/

    std::cout<<"\nLength order\n"
        <<  "------------"<<std::endl;
    for(nth_index<MyTable,1>::type::iterator it1=get<1>(myTable).begin();
            it1!=get<1>(myTable).end();++it1){
        std::cout<<it1->name()<<std::endl;
    }
}

输出

Phonenook order
---------------
Brown Robert
Nightingale Robert
Smith Joe
Tuxedo Marc

Length order
------------
Smith Joe
Tuxedo Marc
Brown Robert
Nightingale Robert