1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
struct MyData {
MyData() : s1(), s2(), s3() {}
MyData( const std::string& s1, const std::string& s2, const std::string& s3 ) :
s1( s1 ), s2( s2 ), s3( s3 ) {}
std::string s1;
std::string s2;
std::string s3;
};
struct first_string {};
struct second_string {};
struct third_string {};
typedef boost::multi_index_container<
MyData,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique< boost::multi_index::tag< first_string >,
boost::multi_index::member< MyData, std::string, &MyData::s1 > >,
boost::multi_index::ordered_unique< boost::multi_index::tag< second_string >,
boost::multi_index::member< MyData, std::string, &MyData::s2 > >,
boost::multi_index::ordered_unique< boost::multi_index::tag< third_string >,
boost::multi_index::member< MyData, std::string, &MyData::s3 > >
>
> MyContainer;
/* Note: Change ordered_unique to ordered_nonunique if the value is not necessarily unique.
Then your searches will return ranges that you have to deal with in a manner similar to multimap. */
// To use the container:
MyContainer ctr;
// Insert elements
ctr.insert( MyData( "a", "b", "c" ) );
ctr.insert( MyData( "d", "e", "f" ) );
// Find occurrence of s1 == "a".
MyContainer::index<first_string>::type::iterator i = ctr.get<first_string>().find( "a" );
// Erase it.
if( i != ctr.get<first_string>().end() )
ctr.get<first_string>().erase( i );
|