3rdParty/boost/1.78.0/libs/assign/doc/multi_index_container.html
// Boost.Assign library // // Copyright Thorsten Ottosen 2003-2004. Use, modification and // distribution is subject to the Boost Software License, Version // 1.0. (See accompanying file LICENSE\_1\_0.txt or copy at // http://www.boost.org/LICENSE\_1\_0.txt) // // For more information, see http://www.boost.org/libs/assign/ // #include \<boost/detail/workaround.hpp\> #if BOOST\_WORKAROUND(\_\_BORLANDC\_\_, BOOST\_TESTED\_AT(0x564))###pragma warn -8091 // suppress warning in Boost.Test###pragma warn -8057 // unused argument argc/argv in Boost.Test#endif #include \<boost/assign/list\_of.hpp\>#include \<boost/assign/list\_inserter.hpp\>#include \<boost/multi\_index\_container.hpp\>#include \<boost/multi\_index/identity.hpp\>#include \<boost/multi\_index/member.hpp\>#include \<boost/multi\_index/ordered\_index.hpp\>#include \<boost/multi\_index/sequenced\_index.hpp\>#include \<boost/test/unit\_test.hpp\>#include \<boost/test/test\_tools.hpp\>#include \<cstddef\>#include \<ostream\>#include \<string\> using namespace boost;using namespace boost::multi\_index;namespace ba = boost::assign; // // Define a classical multi\_index\_container for employees //struct employee{ int id; std::string name; int age; employee(int id\_,std::string name\_,int age\_):id(id\_),name(name\_),age(age\_){} bool operator==(const employee& x)const { return id==x.id&&name==x.name&&age==x.age; } bool operator\<(const employee& x)const { return id\<x.id; } bool operator!=(const employee& x)const{return !(\*this==x);} bool operator\> (const employee& x)const{return x\<\*this;} bool operator\>=(const employee& x)const{return !(\*this\<x);} bool operator\<=(const employee& x)const{return !(x\<\*this);} struct comp\_id { bool operator()(int x,const employee& e2)const{return x\<e2.id;} bool operator()(const employee& e1,int x)const{return e1.id\<x;} }; friend std::ostream& operator\<\<(std::ostream& os,const employee& e) { os\<\<e.id\<\<" "\<\<e.name\<\<" "\<\<e.age\<\<std::endl; return os; }}; struct name{};struct by\_name{};struct age{};struct as\_inserted{}; typedef multi\_index\_container\< employee, indexed\_by\< ordered\_unique\< identity\<employee\> \>, ordered\_non\_unique\< tag\<name,by\_name\>, BOOST\_MULTI\_INDEX\_MEMBER(employee,std::string,name)\>, ordered\_non\_unique\< tag\<age\>, BOOST\_MULTI\_INDEX\_MEMBER(employee,int,age)\>, sequenced\< tag\<as\_inserted\> \> \> \> employee\_set; #if defined(BOOST\_NO\_MEMBER\_TEMPLATES)typedef nth\_index\< employee\_set,1\>::type employee\_set\_by\_name;#elsetypedef employee\_set::nth\_index\<1\>::type employee\_set\_by\_name;#endif typedef boost::multi\_index::index\< employee\_set,age\>::type employee\_set\_by\_age;typedef boost::multi\_index::index\< employee\_set,as\_inserted\>::type employee\_set\_as\_inserted; // // Define a multi\_index\_container with a list-like index and an ordered index //typedef multi\_index\_container\< std::string, indexed\_by\< sequenced\<\>, // list-like index ordered\_non\_unique\<identity\<std::string\> \> // words by alphabetical order \>\> text\_container; void test\_multi\_index\_container(){ employee\_set eset = ba::list\_of\< employee \>(1,"Franz",30)(2,"Hanz",40)(3,"Ilse",50); BOOST\_CHECK( eset.size() == 3u ); // // This container is associative, hence we can use 'insert()' // ba::insert( eset )(4,"Kurt",55)(5,"Bjarne",77)(7,"Thorsten",24); BOOST\_CHECK( eset.size() == 6u ); employee\_set\_by\_name& name\_index = boost::multi\_index::get\<name\>(eset); employee\_set\_by\_name::iterator i = name\_index.find("Ilse"); BOOST\_CHECK( i-\>id == 3 ); BOOST\_CHECK( i-\>age == 50 ); text\_container text = ba::list\_of\< std::string \>("Have")("you")("ever")("wondered")("how")("much")("Boost")("rocks?!"); BOOST\_CHECK\_EQUAL( text.size(), 8u ); BOOST\_CHECK\_EQUAL( \*text.begin(), "Have" ); // // This container is a sequence, hence we can use 'push\_back()' and 'push\_font()' // ba::push\_back( text )("Well")(",")("A")("LOT")(",")("obviously!"); BOOST\_CHECK\_EQUAL( text.size(), 14u ); BOOST\_CHECK\_EQUAL( \*--text.end(), "obviously!" ); ba::push\_front( text ) = "question:", "simple", "A"; BOOST\_CHECK\_EQUAL( text.size(), 17u ); BOOST\_CHECK\_EQUAL( text.front(), "A" );} #include \<boost/test/included/unit\_test\_framework.hpp\> using boost::unit\_test\_framework::test\_suite; test\_suite\* init\_unit\_test\_suite( int argc, char\* argv[] ){ test\_suite\* test = BOOST\_TEST\_SUITE( "List Test Suite" ); test-\>add( BOOST\_TEST\_CASE( &test\_multi\_index\_container ) ); return test;}