This blog Http://blog.csdn.net/livelylittlefish posted by the author (321 @ Small fish) related research, learning content made notes, welcome to the vast number of friends to correct.
1. An example
Imagine a 5-tuple r= (X,Y,Z,A,B), which is indexed, for a data pair.
Set R={r}, it forms a multi-indexed data table, which is well designed and implemented in a database system.
If we use the Boost Multi_index_container component, how to design it. The design and implementation of the process, should pay attention to what problems.
First, let's implement this simple example. All you have to do is put the following data in a Multi_index_container, print it, and then look through the index to find each item given. This column gives two methods of printing, two methods of finding.
This example, without regard to memory leaks, program efficiency and other issues, write code as follows.
2. code and running results
Multiindexcontainer1.cpp
/** * Boost Multi index container test * platform:win32, Visual Studio 2005/2010; Linux, gcc4.1.2 */#include <iostream> #include "boost/multi_index_container.hpp" #include "boost/multi_index/ Member.hpp "#include" boost/multi_index/ordered_index.hpp "using namespace std; using namespace Boost::multi_index; Using Boost::multi_index_container; Define multiple index typedef struct MYINDEX {int x; int y; int z; Myindex (int ax = 0, int ay = 0, int az = 0): x (Ax), Y (ay), Z (AZ) {}}myindex; Define data to is indexed typedef struct {int A; int b;} MyData; Define object to be indexed class MyTest {Public:myindex myindex; MyData MyData; Public:mytest (int x, int y, int z, int a, int b) {myindex.x = x; myindex.y = y; myindex.z = z; mydata.a = A; mydata.b = b } ~mytest () {}; void print (const char* prompt) const {cout << "(" << myindex.x << "," << myindex.y << "," << myindex.z << ")-"; cout << "(" << mydata.a << ","<< mydata.b << ")" << prompt << Endl; } private:mytest (const mytest&); mytest& operator= (const mytest&); }; Define index tag, Multi_index_container, and its type struct myindextag{}; typedef multi_index_container< mytest*, indexed_by< ordered_unique< tag<myindextag>, member<MyTest , Myindex, &MyTest::myIndex> > > >MyContainer_T; typedef mycontainer_t::index<myindextag>::type MYCONTAINERINDEX_T; typedef mycontainer_t::index<myindextag>::type::iterator mycontaineriterator_t; typedef std::p air<mycontaineriterator_t, bool> mycontainerpair_t; Define operator< of index for this container bool operator< (const myindex& LHS, const myindex& RHS) {if ( Lhs.x < rhs.x) return true; else if (lhs.x > Rhs.x) return false; else if (Lhs.y < RHS.Y) return true; else if (Lhs.y > Rhs.y) return false; else if (Lhs.z < rhs.z) return true; else if (Lhs.z > Rhs.z) return false; }//define a global container mycontainer_t mycontainer; Print the container void Print_container1 () {mycontainerindex_t& indexset = mycontainer.get<myindextag> (); typedef mycontainerindex_t::value_type VALUE_TYPE; Std::copy (Indexset.begin (), Indexset.end (), std::ostream_iterator<value_type> (cout)); }//print the container void Print_container2 () {mycontainerindex_t& indexset = mycontainer.get<myindextag> () ; mycontaineriterator_t iter = Indexset.begin (); while (iter! = Indexset.end ()) {(*iter)->print (""); ++iter; }} std::ostream& operator<< (std::ostream& os, const mytest* MyTest) {//mytest->print (""); This clause can work OS << "(" << mytest->myindex.x << "," << mytest->myindex.y << "," << mytest->myindex.z << ")-"; Os << "(" << mytest->mydata.a << "," << mytest->mydata.b << ")" << Endl; return OS; }//find an element in the container void find (int x, int y, int z) {mycontainerindex_t& indexset = mycontainer.get<myindextag> ();//or//const boost::multi_i ndex::index<mycontainer_t, myindextag>::type& indexset = get<myindextag> (MyContainer); mycontaineriterator_t iter = Indexset.find (Myindex (x, Y, z)); if (indexset.end () = = iter) {cout << ("<<x<<", "<<y<<", "<<z<<")-Not Found " << Endl; Return } (*iter)->print (", found"); }//find an element in the container, the efficiency is very low void find2 (int x, int y, int z) {mycontainerindex_t& Indexset = mycontainer.get<myindextag> (); mycontaineriterator_t iter = Indexset.begin (); while (iter = Indexset.end ()) {Myindex index = (*iter)->myindex; if (index.x = = x && index.y = = y && i Ndex.z = = Z) {(*iter)->print (", found"); return;} ++iter; } cout << "(" <<x<< "," <<y<< "," <<z<< ")-not Found" << Endl; } void Test1 () {MyTest*a = new MyTest (1,1,1,10,100); Mycontainer.insert (a); MyTest *b = new MyTest (1,1,2,20,200); Mycontainer.insert (b); MyTest *c = new MyTest (1,1,3,30,300); Mycontainer.insert (c); } void Test2 () {MyTest *a = new MyTest (1,2,1,40,400); Mycontainer.insert (a); MyTest *b = new MyTest (1,2,2,50,500); Mycontainer.insert (b); MyTest *c = new MyTest (1,2,3,60,600); Mycontainer.insert (c); } void Test3 () {MyTest *a = new MyTest (1,3,1,70,700); Mycontainer.insert (a); MyTest *b = new MyTest (1,3,2,80,800); Mycontainer.insert (b); MyTest *c = new MyTest (1,3,3,90,900); Mycontainer.insert (c); } void Test4 () {MyTest *a = new MyTest (2,1,1,110,1000); Mycontainer.insert (a); MyTest *b = new MyTest (2,1,2,220,2000); Mycontainer.insert (b); MyTest *c = new MyTest (2,1,3,330,3000); Mycontainer.insert (c); } void Test5 () {MyTest *a = new MyTest (2,2,1,440,4000); Mycontainer.insert (a); MyTest *b = new MyTest (2,2,2,550,5000); Mycontainer.insert (b); MyTest *c = new MyTest (2,2,3,660,6000); Mycontainer.insert (c); } void Test6() {MyTest *a = new MyTest (2,3,1,770,7000); Mycontainer.insert (a); MyTest *b = new MyTest (2,3,2,880,8000); Mycontainer.insert (b); MyTest *c = new MyTest (2,3,3,990,9000); Mycontainer.insert (c); } void Test_find () {find (1,1,1); find (1,1,2); find (1,1,3); find (1,2,1); find (1,2,2); find (1,3,1); Find ($ 1, 3, 2); Find (1,3,3); Find (2,1,1); Find (2,1,2); Find (2,1,3); Find (2,2,1); Find (2,2,2); Find (2,2,3); Find (2,3,1); Find (2,3,2); Find (2,3,3); } void Test_find2 () {find2 (1,1,1); Find2 (1,1,2); Find2 (1,1,3); Find2 (1,2,1); Find2 (1,2,2); Find2 (n/a); Find2 (1,3,1); f Ind2 (1,3,2); Find2 (1,3,3); Find2 (2,1,1); Find2 (2,1,2); Find2 (2,1,3); Find2 (2,2,1); Find2 (2,2,2); Find2 (2,2,3); Find2 (2,3,1); Find2 (2,3,2); Find2 (2,3,3); } int main () {test2 (); test4 (); Test6 (); Test1 (); Test3 (); Test5 (); Print_container1 (); cout<<endl; Print_container 2 (); cout<<endl; Test_find (); cout<<endl; Test_find2 (); return 0; }
The results of the operation are as follows:
(1, 1, 1)-(10, 100)
(1, 1, 2)-(20, 200)
(1, 1, 3)-(30, 300)
(1, 2, 1)-(40, 400)
(1, 2, 2)-(50, 500)
(1, 2, 3)-(60, 600)
(1, 3, 1)-(70, 700)
(1, 3, 2)-(80, 800)
(1, 3, 3)-(90, 900)
(2, 1, 1)-(110, 1000)
(2, 1, 2)-(220, 2000)
(2, 1, 3)-(330, 3000)
(2, 2, 1)-(440, 4000)
(2, 2, 2)-(550, 5000)
(2, 2, 3)-(660, 6000)
(2, 3, 1)-(770, 7000)
(2, 3, 2)-(880, 8000)
(2, 3, 3)-(990, 9000)
(1, 1, 1)-(10, 100)
(1, 1, 2)-(20, 200)
(1, 1, 3)-(30, 300)
(1, 2, 1)-(40, 400)
(1, 2, 2)-(50, 500)
(1, 2, 3)-(60, 600)
(1, 3, 1)-(70, 700)
(1, 3, 2)-(80, 800)
(1, 3, 3)-(90, 900)
(2, 1, 1)-(110, 1000)
(2, 1, 2)-(220, 2000)
(2, 1, 3)-(330, 3000)
(2, 2, 1)-(440, 4000)
(2, 2, 2)-(550, 5000)
(2, 2, 3)-(660, 6000)
(2, 3, 1)-(770, 7000)
(2, 3, 2)-(880, 8000)
(2, 3, 3)-(990, 9000)
(1, 1, 1)-(found),
(1, 1, 2)-not found
(1, 1, 3)-(found),
(1, 2, 1)-Not found
(1, 2, 2)-not found
(1, 2, 3)-(found),
(1, 3, 1)-Not found
(1, 3, 2)-not found
(1, 3, 3)-not found
(2, 1, 1)-Not found
(2, 1, 2)-(found, +),
(2, 1, 3)-not found
(2, 2, 1)-Not found
(2, 2, 2)-not found
(2, 2, 3)-(660, 6000), found
(2, 3, 1)-Not found
(2, 3, 2)-not found
(2, 3, 3)-not found
(1, 1, 1)-(found),
(1, 1, 2)-(found),
(1, 1, 3)-(found),
(1, 2, 1)-(found),
(1, 2, 2)-(found),
(1, 2, 3)-(found),
(1, 3, 1)-(found),
(1, 3, 2)-(found, +),
(1, 3, 3)-(found),
(2, 1, 1)-(found, +),
(2, 1, 2)-(found, +),
(2, 1, 3)-(found, +),
(2, 2, 1)-(4000, found)
(2, 2, 2)-(550, found),
(2, 2, 3)-(660, 6000), found
(2, 3, 1)-(770, 7000), found
(2, 3, 2)-(880, 8000), found
(2, 3, 3)-(990, 9000), found
All examples in this series take the above data as an example.
3. Other issues in this example
3.1 Myindex class must have default constructor
This is a hint question, and it's a good idea to provide the default constructor when it comes to reminding yourself of the design class.
The arguments to the Myindex constructor are the default values.
Define multiple Index
typedef struct MYINDEX
{
int x;
int y;
int z;
Myindex (int ax = 0, int ay = 0, int az = 0): x (Ax), Y (ay), Z (AZ) {}
}myindex;
Otherwise, if there are no default values, the following.
Define multiple Index
typedef struct MYINDEX
{
int x;
int y;
int z;
Myindex (int ax, int ay, int az): x (Ax), Y (ay), Z (AZ) {}
}myindex;
The following compilation error will be generated.
Multiindexcontainer1.cpp:In constructor ' mytest::mytest (int, int, int, int, int) ':
Multiindexcontainer1.cpp:42:error:no matching function for call to ' Myindex::myindex () '
Multiindexcontainer1.cpp:22:note:candidates are:myindex::myindex (int, int, int)
Multiindexcontainer1.cpp:17:note:myindex::myindex (const myindex&)
What is this for? What causes such a compilation error.
View the definition of the MyTest class to know that the Myindex object Myindex is defined in the MyTest class. We know that when the MyTest object is initialized, the other objects defined in its class are initialized, that is, before the MyTest constructor starts to initialize the 3 members of Myindex, they have been initialized, that is, the default constructor of Myindex has been called, because The code only defines the constructor and does not define the default constructor, so there is an error "no default constructor" on line 42nd.
The result of 3.2 find is incorrect
As you can see from the running results, the results of find are not correct. Why. Keep Thinking First ... In fact, it is very simple, is to write the program careless or not thinking completely caused by the solution see "Boost Component Multi_index_container Instance (cont. 2)".
Technorati Tags: Boost, multi_index, Multi_index_container