3 // Copyright (c) 2006-2007 Matias Capeletto
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)
10 // Boost.Bimap Example
11 //-----------------------------------------------------------------------------
12 // Hashed indices can be used as an alternative to ordered indices when fast
13 // lookup is needed and sorting information is of no interest. The example
14 // features a word counter where duplicate entries are checked by means of a
17 #include <boost/config.hpp>
19 //[ code_mi_to_b_path_hashed_indices
24 #include <boost/tokenizer.hpp>
26 #include <boost/bimap/bimap.hpp>
27 #include <boost/bimap/unordered_set_of.hpp>
28 #include <boost/bimap/multiset_of.hpp>
29 #include <boost/bimap/support/lambda.hpp>
31 using namespace boost::bimaps;
34 struct occurrences {};
39 multiset_of< tagged<unsigned int,occurrences>, std::greater<unsigned int> >,
40 unordered_set_of< tagged< std::string, word> >
44 typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;
50 "Relations between data in the STL are represented with maps."
51 "A map is a directed relation, by using it you are representing "
52 "a mapping. In this directed relation, the first type is related to "
53 "the second type but it is not true that the inverse relationship "
54 "holds. This is useful in a lot of situations, but there are some "
55 "relationships that are bidirectional by nature.";
57 // feed the text into the container
60 text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
61 unsigned int total_occurrences = 0;
63 for( text_tokenizer::const_iterator it = tok.begin(), it_end = tok.end();
68 word_counter::map_by<occurrences>::iterator wit =
69 wc.by<occurrences>().insert(
70 word_counter::map_by<occurrences>::value_type(0,*it)
73 wc.by<occurrences>().modify_key( wit, ++_key);
76 // list words by frequency of appearance
78 std::cout << std::fixed << std::setprecision(2);
80 for( word_counter::map_by<occurrences>::const_iterator
81 wit = wc.by<occurrences>().begin(),
82 wit_end = wc.by<occurrences>().end();
84 wit != wit_end; ++wit )
86 std::cout << std::setw(15) << wit->get<word>() << ": "
88 << 100.0 * wit->get<occurrences>() / total_occurrences << "%"