Boost: bimap

Source: Internet
Author: User

Boost. bimap is a C ++ bidirectional Map Library. With boost. bimap, you can create two types of associated containers that can be used as key values. Bimap<X,Y> Can be considered STD::Map<X,Y> Add STD::Map<Y,X> . If you know how to use a standard container, the learning curve of bimap is almost flat. A lot of effort has been made in boost. bimap to comply with STL naming rules. This library is designed to match common STL containers.

Remember,BMA set that can be used as a link separately. We can insert an element or use this view for traversal.

 

Boost: bimap-bidirectional ing

Define a bimap:

# Include bimap bm;

It has three different views:

BM. Left: It is compatible with STD: map.

BM. Right: is compatible with STD: map.

BM is compatible with STD: Set <relation type.

For example, we have an output map function as follows:

Template <class maptype> void print_map (const maptype & M)

{

Typedef typename maptype: const_iterator;

For (const_iterator iter = M. Begin (), iend = M. End (); iter! = Iend; ++ ITER)

{

STD: cout <ITER-> first <"-->" <ITER-> second <STD: Endl;

}

}

Its parameters can accept a STD: map object. Therefore, it can also accept the following calls:

Print_map (BM. Left );

Print_map (BM. Right );

Below we define a bidirectional ing between integers and strings:

Typedef boost: bimap <int, STD: String> bm_type;

Bm_type bm;

Insert data to it:

BM. insert (bm_type: value_type (1, "one "));

BM. insert (bm_type: value_type (2, "two "));

Output the inserted data:

STD: cout <"there are" <BM. size () <"relations" <STD: Endl; For (bm_type: const_iterator iter = BM. begin (), iend = BM. end (); iter! = Iend; ++ ITER)

{

STD: cout <ITER-> left <"<-->" <ITER-> right <STD: Endl;

}

Then we use its left view (like using a STD: Map): // The iterator type of the Left view.

Typedef bm_type: left_map: const_iterator left_const_iterator;

For (left_const_iterator left_iter = BM. Left. Begin (), iend = BM. Left. End (); left_iter! = Iend; ++ left_iter)

{

STD: cout <left_iter-> first <"-->" <left_iter-> second <STD: Endl;

}

Bm_type: left_const_iterator left_iter = BM. Left. Find (2 );

Assert (left_iter-> second = "two"); // insert data to BM in the Left view //

It has the same effect as directly using BM. insert (bm_type: value_type (3, "three ");

BM. Left. insert (bm_type: left_value_type (3, "three "));

Similarly, we can use its right view: // search by string

Bm_type: right_const_iterator right_iter = BM. right. find ("two"); Assert (right_iter-> second = 2); // call at () assert (BM. right. at ("one") = 1); // Delete the relationship BM corresponding to "two. right. erase ("two ");

Finally, the missing time is saved in STD: map. We can insert ("1", 1) and then insert ("one", 1.

But this does not work in bimap. because both of them can be used as key values. both of them must be unique. but this is just a lack of time-saving. bimap can customize some features through template parameters: for example, you can specify whether one-to-many ing is required: one X corresponds to multiple y. multiple x maps to one y. you can also set multiple X to correspond to multiple y. you can also specify whether the Left view or right view or the two are ordered. if order is not required. it can use hash tables to implement the underlying layer.

 

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.