• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Bimap
2 //
3 // Copyright (c) 2006-2007 Matias Capeletto
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 //  VC++ 8.0 warns on usage of certain Standard Library and API functions that
10 //  can be cause buffer overruns or other possible security issues if misused.
11 //  See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
12 //  But the wording of the warning is misleading and unsettling, there are no
13 //  portable alternative functions, and VC++ 8.0's own libraries use the
14 //  functions in question. So turn off the warnings.
15 #define _CRT_SECURE_NO_DEPRECATE
16 #define _SCL_SECURE_NO_DEPRECATE
17 
18 // Boost.Bimap Example
19 //-----------------------------------------------------------------------------
20 
21 #include <boost/config.hpp>
22 
23 #include <iostream>
24 #include <boost/tokenizer.hpp>
25 
26 #include <boost/bimap/bimap.hpp>
27 #include <boost/bimap/unordered_set_of.hpp>
28 #include <boost/bimap/list_of.hpp>
29 
30 using namespace boost::bimaps;
31 
32 struct counter {
countercounter33     counter() : c(0) {}
operator ++counter34     counter& operator++() { ++c; return *this; }
operator ++counter35     unsigned int operator++(int) { return c++; }
operator const unsigned intcounter36     operator const unsigned int() const { return c; }
37     private:
38     unsigned int c;
39 };
40 
main()41 int main()
42 {
43     //[ code_repetitions_counter
44 
45     typedef bimap
46     <
47         unordered_set_of< std::string >,
48         list_of< counter > /*< `counter` is an integer that is initialized
49                                 in zero in the constructor >*/
50 
51     > word_counter;
52 
53     typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;
54 
55     std::string text=
56         "Relations between data in the STL are represented with maps."
57         "A map is a directed relation, by using it you are representing "
58         "a mapping. In this directed relation, the first type is related to "
59         "the second type but it is not true that the inverse relationship "
60         "holds. This is useful in a lot of situations, but there are some "
61         "relationships that are bidirectional by nature.";
62 
63     // feed the text into the container
64     word_counter   wc;
65     text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
66 
67     for( text_tokenizer::const_iterator it = tok.begin(), it_end = tok.end();
68          it != it_end ; ++it )
69     {
70         /*<< Because the right collection type is `list_of`, the right data
71              is not used a key and can be modified in the same way as with
72              standard maps. >>*/
73         ++ wc.left[*it];
74     }
75 
76     // list words with counters by order of appearance
77     /*<< When we insert the elements using the left map view, the element
78          is inserted at the end of the list. >>*/
79     for( word_counter::right_const_iterator
80             wit = wc.right.begin(), wit_end = wc.right.end();
81 
82          wit != wit_end; ++wit )
83     {
84         std::cout << wit->second << ": " << wit->first;
85     }
86     //]
87 
88     return 0;
89 }
90 
91 
92