开发者

How to get the value out of a multi index

开发者 https://www.devze.com 2023-02-15 23:31 出处:网络
I am trying to do one multi map(4 variables) using boost- multi index.Can I know how to get the value of a particular index like

I am trying to do one multi map(4 variables) using boost- multi index. Can I know how to get the value of a particular index like

tmp_Container.get<name>().find("aaa")->firstVariable

EDIT

struct EnumStruct
{
    int microsoftEnum;
    std::string microsoftEnumString;
    int localEnum;
    std::string localEnumString;
}

typedef bmi::multi_index_container<
    EnumStruct,
    bmi::ordered_unique<bmi::tag<localEnum>,bmi::member < 
        EnumStruct,std::string,&EnumS开发者_如何学编程truct::localEnumString > >
    >
> boost_Enum_Container;

boost_Enum_Container enumStructContainer;


int main()
{
    enumStructContainer.insert(EnumStruct(1,"MS_1",11,"LOC11"));
    enumStructContainer.insert(EnumStruct(2,"MS_2",22,"LOC22"));
    enumStructContainer.insert(EnumStruct(3,"MS_3",33,"LOC33"));
    //enumStructContainer.get<0>().find(1);
    //boost_Enum_Container::index<localEnum>::type&
    localEnum_Index=enumStructContainer.get<localEnum>().find(22)->firstVariable;
    boost_Enum_Container::iterator
    return 0;
}


You need the project function. See http://www.boost.org/doc/libs/1_39_0/libs/multi_index/doc/tutorial/basics.html#projection and http://www.boost.org/doc/libs/1_46_0/libs/multi_index/doc/reference/multi_index_container.html#projection. Thus: tmp_Container.project<0>(tmp_Container.get<name>.find("aaa")).


You are using boost::multi_index::tag in a wrong way. To use it, you must provide a valid struct.

Also you are trying to search a number in a string index. Below code must work, I did not test it though.

struct LOCALENUM { };
struct LOCALENUMSTR { };

using namespace boost::multi_index;

typedef boost::multi_index_container<
    EnumStruct,
indexed_by<
    ordered_unique<tag<LOCALENUM>,member<EnumStruct,int,&EnumStruct::localEnum > >
    >,
    ordered_unique<tag<LOCALENUMSTR>,member<EnumStruct,std::string,&EnumStruct::localEnumString > >
    >
>
> boost_Enum_Container;


typedef boost_Enum_Container::index<LOCALENUM>::type::iterator EnumIterator;
typedef boost_Enum_Container::index<LOCALENUMSTR>::type::iterator EnumStrIterator;

EnumIterator enumiter = enumStructContainer.get<LOCALENUM>().find(22);
EnumStruct & enum_struct = (*eniter);
// There you go : 
std::cout << enum_struct.microsoftEnumString << "\n";


//Searching for LocalEnumString :
EnumStrIterator enunstriter = enumStructContainer.get<LOCALENUMSTR>().find("LOC22");
EnumStruct & enum_struct = (*enunstriter );
std::cout << enum_struct.microsoftEnumString << "\n";


As has been mentioned multiple times in your other recent questions, the ordered_unique index specifier has the same interface as std::map; so if you know how to use std::map (hint, hint) then this should be very straightforward:

#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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消