我正在挖掘多指标网络,但没有找到任何简单的例子。谁能给我一个像multi_index_container这样的小代码,并带-single_index-查找内容并确定值?

最佳答案

我将复制并粘贴your other, near-duplicate question中的代码:

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

struct EnumStruct
{
    EnumStruct(int const me, std::string const& mes, int const le, std::string const& les)
      : microsoftEnum(me),
        microsoftEnumString(mes),
        localEnum(le),
        localEnumString(les)
    { }

    int microsoftEnum;
    std::string microsoftEnumString;
    int localEnum;
    std::string localEnumString;
};

namespace bmi = boost::multi_index;

struct localEnum_t;

typedef bmi::multi_index_container<
    EnumStruct,
    bmi::indexed_by<
        bmi::ordered_unique<
            bmi::tag<localEnum_t>,
            bmi::member<EnumStruct, int, &EnumStruct::localEnum>
        >
    >
> boost_Enum_Container;

int main()
{
    boost_Enum_Container enumStructContainer;
    enumStructContainer.insert(EnumStruct(1, "MS_1", 11, "LOC11"));
    enumStructContainer.insert(EnumStruct(2, "MS_2", 22, "LOC22"));
    enumStructContainer.insert(EnumStruct(3, "MS_3", 33, "LOC33"));
    boost_Enum_Container::index_iterator<localEnum_t>::type iter = enumStructContainer.get<localEnum_t>().find(22);
    if (iter != enumStructContainer.get<localEnum_t>().end())
        std::cout << "found, localEnumString == " << iter->localEnumString << std::endl;
    else
        std::cout << "didn't find anything" << std::endl;
}

09-07 04:14