1350

How to get the value out of a multi index

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

<strong>EDIT</strong>

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,&EnumStruct::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; }

Answer1:

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

Answer2:

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";

Answer3:

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 (<strong>hint</strong>, <strong>hint</strong>) 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; }

Recommend

  • Unit testing something with ObserveOnDispatcher
  • Can't get JSON data from Rails API
  • Hexagonal pattern with CSS
  • Populate column values from MS Access database into VB .Net Combo-Box dropdown values?
  • System.Data.SqlClient Namespace for MySQL?
  • SQL Error while importing Data From Excel
  • What exactly does a Sample Rate of 44100 sample?
  • Update TOC in .docx document using DocumentFormat.OpenXml (C#)
  • CSS - Cursor Transition
  • On Index page, when editing 1 row, all rows are re-rendering (painfully slow). Using Reactjs with Ru
  • Is a single constant value considered an expression?
  • How can I launch the Alarms App through a Universal Windows App
  • IE blocking file download with SSL connections - CacheControl issue
  • Developing Word add-ins for Macintosh
  • UUID into unsigned int
  • ASP.NET how to check type of the file type irrespective of extension
  • Combinatios in List of LIsts Prolog
  • Custom partiotioning of JavaDStreamPairRDD
  • Hibernate reverse engineering - mapping a table to Java enum
  • Advice on how to create this button shape
  • sqlite3_wal_checkpoint_v2 always returns SQL_BUSY
  • How can I associate a custom URL protocol with a ClickOnce app?
  • How do I write an item to a DynamoDb with the AWS DynamoDB DocumentClient?
  • How to make Plotly chart with year mapped to line color and months on x-axis
  • Visual Studio 2008 and JavaScript brackets formatting
  • Cosmos DB succeeds and fails on randomly on the same query, saying they are cross partition when the
  • css calendar - td background diagonal split - two colors
  • Color time-series based on column values in pandas
  • Flex items with same property values are rendering in different sizes
  • RxJava debounce by arbitrary value
  • HTTP/2 streams vs HTTP/1.1 connections
  • Menu Color Fade on Hover with Jquery
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs