Boost Multi Index Container如何使用不同的复合键进行搜索?

时间:2015-02-10 15:50:33

标签: c++ boost

typedef multi_index_container<PositionSummary*,
        indexed_by<
                ordered_unique<
                        composite_key<PositionSummary, const_mem_fun<PositionSummary, int, &PositionSummary::positiondate>,
                                const_mem_fun<PositionSummary, const std::string&, &PositionSummary::accountid>,
                                const_mem_fun<PositionSummary, const std::string&, &PositionSummary::instid> > >,
                ordered_unique<
                        composite_key<PositionSummary, const_mem_fun<PositionSummary, int, &PositionSummary::positiondate>,
                                const_mem_fun<PositionSummary, const std::string&, &PositionSummary::instid>,
                                const_mem_fun<PositionSummary, const std::string&, &PositionSummary::accountid> > > 
                    > > PositionSummaryContainer;

我可以在下面搜索第一个“ordered_unique”,如下所示:

PositionSummaryContainer::iterator it = psContainer_.find(boost::make_tuple(date, accountID, instID));

但是如何根据第二个“ordered_unique”进行搜索?由于accountID和instID都是字符串?如何告诉程序使用哪个索引?

1 个答案:

答案 0 :(得分:3)

您使用索引编号(get<i>())或标记(使用bmi::tag<T>)。

我将展示一个示例:

<强> livce On Coliru

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

struct PositionSummary {
    int                positiondate() const { return 42;  }
    std::string const& accountid()    const { return acc; }
    std::string const& instid()       const { return ins; }
  private:
    std::string acc = "a";
    std::string ins = "a";
};

namespace bmi = boost::multi_index;

typedef boost::multi_index_container<PositionSummary*,
        bmi::indexed_by<
                bmi::ordered_unique<
                        bmi::tag<struct byPosAccInst>,
                        bmi::composite_key<PositionSummary, 
                            bmi::const_mem_fun<PositionSummary, int, &PositionSummary::positiondate>,
                            bmi::const_mem_fun<PositionSummary, const std::string&, &PositionSummary::accountid>,
                            bmi::const_mem_fun<PositionSummary, const std::string&, &PositionSummary::instid> 
                        > >,
                bmi::ordered_unique<
                        bmi::tag<struct byPosInstAcc>,
                        bmi::composite_key<PositionSummary, 
                            bmi::const_mem_fun<PositionSummary, int, &PositionSummary::positiondate>,
                            bmi::const_mem_fun<PositionSummary, const std::string&, &PositionSummary::instid>,
                            bmi::const_mem_fun<PositionSummary, const std::string&, &PositionSummary::accountid> 
                        > > 
                    > > PositionSummaryContainer;

int main() 
{
    PositionSummaryContainer c;

    c.insert(new PositionSummary());

    {
        auto& index = c.get<0>();
        auto it = index.find(boost::make_tuple(42, "a", "a"));
    }
    {
        auto& index = c.get<1>();
        auto it = index.find(boost::make_tuple(42, "a", "a"));
    }

    // by tag:
    {
        auto& index = c.get<byPosAccInst>();
        auto it = index.find(boost::make_tuple(42, "a", "a"));
    }
    {
        auto& index = c.get<byPosInstAcc>();
        auto it = index.find(boost::make_tuple(42, "a", "a"));
    }
}