Bimap is a very important container in boost and can be searched and replaced in two directions. This makes up for the need to find the key value corresponding to data for map and multimap. The elements can be traversed cyclically to find the corresponding key value, and then deleted. The final replacement is insufficient.
However, the source programming of boost templates has inherent drawbacks: compilation errors are not easy to find and compilation takes a long time. However, if you have been using boost for a long time and know Common Errors, this disadvantage can be overcome. Although the Compilation Time is a little long, it is still mentioned in the past than writing a line by yourself.
The following are some of the required headers. If you just use bimap, you only need to introduce # include.
#include <iostream>#include <map>#include <boost/foreach.hpp>#include <boost/typeof/typeof.hpp>#include <boost/bimap.hpp>#include <boost/bimap/set_of.hpp>#include <boost/bimap/vector_of.hpp>//#include <boost/bimap/tags/tagged.hpp>usingnamespace boost::bimaps;usingnamespace boost;usingnamespace std;
First, write a tool function for printing bimap. This function can print both bimap and multimap.
BOOST_AUTO and BOOST_FOREACH, which are commonly used in boost, are used here. I personally prefer the first implementation I wrote.
If you use some boost macros flexibly in the program, you can use a lot of code.
Here BOOST_FOREACH is more concise than BOOST_AUTO. But imagine how long the first parameter of the for Loop should be written if the two parameters cannot be used.
// Print all the element templates <classT> voidprint_map (T & m) {// method 1 prefers BOOST_FOREACH (T: value_type v, m) {cout <v. first <"" <v. second <endl;} // method 2 // for (BOOST_AUTO (pos, m. begin (); pos! = M. end (); ++ pos) // {// cout <pos-> first <"--------" <pos-> second <endl ;//}}
Remember that the simple bimap key/value must be unique in both directions, although this rule can be changed later.
typedefbimap<int,std::string> bm_t;
If the inserted value is not unique, the result of the first insertion is displayed.
Below is the left value repeated insertion Test
Intmain () {std: map <int, int> test; // first create test [1] = 2; // then modify test [1] = 4; cout <test [1] <endl; // result: 4 // test bm_t col with the left value of bimap. left. insert (make_pair (5, "hello"); // although it is invalid, you can insert col. left. insert (make_pair (5, "world"); // The value BOOST_AUTO (iter, col. left. find (5); cout <"left euql:" <iter-> second <endl; // result: hello // the right value of bimap repeats the test col. right. insert (make_pair ("hello", 10); BOOST_AUTO (iter2, col. right. find ("hello"); // For the right value query, the constant output direction should be the opposite, the original first variable seoond // test also shows the cout value inserted for the first time <"right equal:" <iter2-> second <endl; // result: 5
You cannot use map to access a simple bimap. Otherwise
Compilation error. error C2664: 'boost: mpl: assertion_failed ': cannot convert parameter 1 from 'boost: mpl: failed
For template source programming, it is very beneficial to be familiar with some errors and correct the program.
Although operator [] cannot be used like an associated array, This is not absolute.
Operator [] can be used.
cout<<col.left[5]<<endl;
Below is the right value repeated insertion Test
Typedefbm_t: value_type val; // do not use left and right views. Use value_type to insert the element col. insert (val (50, "test_value_type"); typedefbm_t: left_value_type left_val; // use left_value_type to insert the element col. // Similarly, use right_value_type to insert the element col. left. insert (left_val (100, "left_value_type"); // input the result set print_map (col. left );
You can use operator [] to use some classes and methods of the bimap library, and you can use bimap without the unique key/value. However, the Left view must be ordered, the right view is a random access type.
Bimap <boost: bimaps: set_of <int>, boost: bimaps: vector_of <std: string> bm_multi; bm_multi.left.insert (make_pair (1, "111"); bm_multi.left [2] = "222"; bm_multi.left [300] = "bimap"; cout <"use operator: "<bm_multi.left [300] <endl; print_map (bm_multi.left); // output // 1 111 // 2 222 // 300 bimap
For common access types, see:
Set_of, multiset_of, unordered_set_of, unordered_multiset_of: key value index
List_of, vector_of, unconstrained_set_of: cannot be used as a key value index
For details about this part, refer to "Boost library full Development Guide" P264
For the tagged of boost bimap, the function of adding labels to the left and right views using boost tagged is not stable in vs, but it may also be a problem with my compiler.
bimap<tagged<int,structid> ,tagged<std::string ,structname> > bm_tag_test; bm_tag_test.by<id>{}.insert(make_pair(1,"hello")); bm_tag_test.by<id>{}.insert(make_pair(2,"world")); bm_tag_test.by<name>{}.insert(make_pair("test",3)); print_map(bm_tag_test.by<name>()); }
The projection, replacement, search, and modification functions of bimap are described in detail next time.