Boost component multi_index_container instance (Continued 3)

Source: Internet
Author: User

This blog http://blog.csdn.net/livelylittlefish
Post
The author (@ Xiaoyu) takes notes on the relevant research and study content. You are welcome to correct me!

 

1.
Introduction

 

In the instance

(
Continued
2)

When the program exits
New
The memory is released.

 

2.

Add
Free_container
Function

 

// Free the container

Void
Free_container ()

{


While
(! Mycontainer. Empty ())


{


Mycontaineriterator_t
Iter = mycontainer. Begin ();


If (null
= (* ITER ))


{


Mycontainer. Erase (ITER );


Continue
;


}

 


Mycontainer_t
: Value_type pobj = * ITER;


Mycontainer. Erase (ITER );


Delete
Pobj;


}

}

Modify at the same time
Mytest
Class so that you can see that when the program exits
Multi_index_container
The release operation of the element in.

 

3.

Code and running result

 

Multiindexcontainer3.cpp

/** <Br/> * boost multi index container test <br/> * platform: Win32, Visual Studio 2005/2010; Linux, gcc4.1.2 <br/> */<br/> # include <iostream> <br/> # include "Boost/multi_index_container.hpp" <br/> # include "Boost/multi_index/member. HPP "<br/> # include" Boost/multi_index/ordered_index.hpp "<br/> using namespace STD; <br/> using namespace boost: multi_index; <br/> using boost: multi_index_container; <br />// Define multiple index <br/> typedef struct myindex <br/>{< br/> int X; <br/> int y; <br/> int Z; <br/> myindex (int ax = 0, int ay = 0, int AZ = 0): X (ax), y (AY), Z (AZ) {}< br/>} myindex; <br/> // define data to be indexed <br/> typedef struct <br/>{< br/> int; <br/> int B; <br/>} mydata; <br/> // define object to be indexed <br/> class mytest <br/>{< br/> Public: <br/> myindex; <br/> mydata MYD ATA; <br/> Public: <br/> mytest (int x, int y, int Z, int A, int B) <br/>{< br/> myindex. X = x; <br/> myindex. y = y; <br/> myindex. z = z; <br/> mydata. A = A; <br/> mydata. B = B; <br/>}< br/> ~ Mytest () <br/>{< br/> cout <"(" <myindex. x <"," <myindex. Y <"," <myindex. z <")-"; <br/> cout <"(" <mydata. A <"," <mydata. B <"), destructed" <Endl; <br/>}< br/> void print (const char * prompt) const <br/> {<br/> cout <"(" <myindex. x <"," <myindex. Y <"," <myindex. z <")-"; <br/> cout <"(" <mydata. A <"," <mydata. B <")" <prompt <Endl; <br />}< Br/> PRIVATE: <br/> mytest (const mytest &); <br/> mytest & operator = (const mytest &); <br/> }; <br/> // define index tag, multi_index_container, and its type <br/> struct myindextag {}; <br/> typedef multi_index_container <br/> mytest *, <br/> indexed_by <br/> ordered_unique <br/> tag <myindextag>, member <mytest, myindex, & mytest :: myindex >>>< br/> mycontainer_t; <br/> typedef mycontainer_t: index <m Yindextag >:: type mycontainerindex_t; <br/> typedef mycontainer_t: index <myindextag >:: type: iterator mycontaineriterator_t; <br/> typedef STD: pair <strong, bool> mycontainerpair_t; <br/> // define operator <of index for this container <br/> bool operator <(const myindex & LHS, const myindex & RHs) <br/>{< br/> If (LHS. x <RHS. x) return true; <br/> else if (LHS. x> RHS. x) return false; <br/> Else if (LHS. Y <RHS. y) return true; <br/> else if (LHS. y> RHS. y) return false; <br/> else if (LHS. z <RHS. z) return true; <br/> else if (LHS. z> RHS. z) return false; <br/> else return false; <br/>}< br/> // define a global container <br/> mycontainer_t mycontainer; <br/> // print the container <br/> void print_container () <br/> {<br/> mycontainerindex_t & indexset = mycontainer. get <myindextag> (); <br/> Typedef mycontainerindex_t: value_type; <br/> STD: Copy (indexset. begin (), indexset. end (), STD: ostream_iterator <value_type> (cout); <br/>}< br/> STD: ostream & operator <(STD :: ostream & OS, const mytest * mytest) <br/>{< br/> // mytest-> Print (""); // This clause can work <br/> OS <"(" <mytest-> myindex. x <"," <mytest-> myindex. Y <"," <mytest-> myindex. z <")-"; <br/> OS <"(" <Mytest-> mydata. A <"," <mytest-> mydata. B <")" <Endl; <br/> return OS; <br/>}< br/> // find an element in the container <br/> void find (int x, int y, int Z) <br/>{< br/> mycontainerindex_t & indexset = mycontainer. get <myindextag> (); <br/> // or <br/> // const boost: multi_index: index <mycontainer_t, myindextag> :: type & indexset = get <myindextag> (mycontainer); <br/> mycontaineriterator_t iter = Indexset. find (myindex (x, y, z); <br/> If (indexset. end () = ITER) <br/> {<br/> cout <"(" <x <"," <Y <", "<z <")-Not Found "<Endl; <br/> return; <br/>}< br/> (* ITER) -> Print (", found"); <br/>}< br/> // free the container <br/> void free_container () <br/>{< br/> while (! Mycontainer. empty () <br/>{< br/> mycontaineriterator_t iter = mycontainer. begin (); <br/> If (null = (* ITER) <br/>{< br/> mycontainer. erase (ITER); <br/> continue; <br/>}< br/> mycontainer_t: value_type pobj = * ITER; <br/> mycontainer. erase (ITER); <br/> Delete pobj; <br/>}< br/> void test1 () <br/>{< br/> mytest * A = new mytest (1, 1, 10, 100); <br/> mycontainer. insert (a); <br/> mytest * B = new mytest (200,); <br/> mycontainer. insert (B); <br/> mytest * c = new mytest (1, 1, 3, 30, 300); <br/> mycontainer. insert (c); <br/>}< br/> void Test2 () <br/>{< br/> mytest * A = new mytest, 400); <br/> mycontainer. insert (a); <br/> mytest * B = new mytest (1, 2, 50, 500); <br/> mycontainer. insert (B); <br/> mytest * c = new mytest (1, 2, 3, 60, 600); <br/> mycontainer. insert (c); <br/>}< br/> void test3 () <br/>{< br/> mytest * A = new mytest, 700); <br/> mycontainer. insert (a); <br/> mytest * B = new mytest (1, 3, 2, 80, 800); <br/> mycontainer. insert (B); <br/> mytest * c = new mytest (1, 3, 90, 900); <br/> mycontainer. insert (c); <br/>}< br/> void test4 () <br/>{< br/> mytest * A = new mytest (1,110,100, 0 ); <br/> mycontainer. insert (a); <br/> mytest * B = new mytest (2,220,200, 0); <br/> mycontainer. insert (B); <br/> mytest * c = new mytest (3,330,300, 0); <br/> mycontainer. insert (c); <br/>}< br/> void test5 () <br/>{< br/> mytest * A = new mytest (1,440,400, 0 ); <br/> mycontainer. insert (a); <br/> mytest * B = new mytest (2,550,500, 0); <br/> mycontainer. insert (B); <br/> mytest * c = new mytest (3,660,600, 0); <br/> mycontainer. insert (c); <br/>}< br/> void test6 () <br/>{< br/> mytest * A = new mytest (1,770,700, 0 ); <br/> mycontainer. insert (a); <br/> mytest * B = new mytest (2,880,800, 0); <br/> mycontainer. insert (B); <br/> mytest * c = new mytest (3,990,900, 0); <br/> mycontainer. insert (c); <br/>}< br/> void test_find () <br/>{< br/> Find (1, 1, 1 ); <br/> Find (, 2); <br/> Find (, 3); <br/> Find (, 1 ); <br/> Find (1, 2); <br/> Find (1, 2, 3); <br/> Find (1, 3, 1 ); <br/> Find (1, 3, 2); <br/> Find (1, 3); <br/> Find (2, 1, 1 ); <br/> Find (2, 1, 2); <br/> Find (2, 1, 3); <br/> Find (2, 2, 1 ); <br/> Find (2, 2); <br/> Find (2, 2, 3); <br/> Find (2, 3, 1 ); <br/> Find (2, 3); <br/> Find (2, 3); <br/>}< br/> int main () <br/>{< br/> Test2 (); <br/> test4 (); <br/> test6 (); <br/> test1 (); <br/> test3 (); <br/> test5 (); <br/> print_container (); <br/> cout <Endl; <br/> test_find (); <br/> cout <Endl; <br/> free_container (); <br/> return 0; <br/>}

The running result is 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,100 0)

(2, 1, 2)-(220,200 0)

(2, 1, 3)-(330,300 0)

(2, 2, 1)-(440,400 0)

(2, 2, 2)-(550,500 0)

(2, 2, 3)-(660,600 0)

(2, 3, 1)-(770,700 0)

(2, 3, 2)-(880,800 0)

(2, 3, 3)-(990,900 0)

 

(1, 1, 1)-(10,100), found

(1, 1, 2)-(20,200), found

(1, 1, 3)-(30,300), found

(1, 2, 1)-(40,400), found

(1, 2, 2)-(50,500), found

(1, 2, 3)-(60,600), found

(1, 3, 1)-(70,700), found

(1, 3, 2)-(80,800), found

(1, 3, 3)-(90,900), found

(2, 1, 1)-(0, 110,100), found

(2, 1, 2)-(0, 220,200), found

(2, 1, 3)-(0, 330,300), found

(2, 2, 1)-(0, 440,400), found

(2, 2, 2)-(0, 550,500), found

(2, 2, 3)-(0, 660,600), found

(2, 3, 1)-(0, 770,700), found

(2, 3, 2)-(0, 880,800), found

(2, 3, 3)-(0, 990,900), found

 

(1, 1, 1)-(10,100), destructed

(1, 1, 2)-(20,200), destructed

(1, 1, 3)-(30,300), destructed

(1, 2, 1)-(40,400), destructed

(1, 2, 2)-(50,500), destructed

(1, 2, 3)-(60,600), destructed

(1, 3, 1)-(70,700), destructed

(1, 3, 2)-(80,800), destructed

(1, 3, 3)-(90,900), destructed

(2, 1, 1)-(110,100 0), destructed

(2, 1, 2)-(0, 220,200), destructed

(2, 1, 3)-(0, 330,300), destructed

(2, 2, 1)-(0, 440,400), destructed

(2, 2, 2)-(0, 550,500), destructed

(2, 2, 3)-(0, 660,600), destructed

(2, 3, 1)-(770,700 0), destructed

(2, 3, 2)-(0, 880,800), destructed

(2, 3, 3)-(0, 990,900), destructed

Technorati label: boost
, Multi_index
, Multi_index_container

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.