• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Boost.MultiIndex example of use of sequenced indices.
2  *
3  * Copyright 2003-2008 Joaquin M Lopez Munoz.
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  * See http://www.boost.org/libs/multi_index for library home page.
9  */
10 
11 #if !defined(NDEBUG)
12 #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
13 #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
14 #endif
15 
16 #include <boost/multi_index_container.hpp>
17 #include <boost/multi_index/identity.hpp>
18 #include <boost/multi_index/ordered_index.hpp>
19 #include <boost/multi_index/sequenced_index.hpp>
20 #include <boost/tokenizer.hpp>
21 #include <algorithm>
22 #include <iomanip>
23 #include <iostream>
24 #include <iterator>
25 #include <string>
26 
27 using boost::multi_index_container;
28 using namespace boost::multi_index;
29 
30 /* text_container holds words as inserted and also keep them indexed
31  * by dictionary order.
32  */
33 
34 typedef multi_index_container<
35   std::string,
36   indexed_by<
37     sequenced<>,
38     ordered_non_unique<identity<std::string> >
39   >
40 > text_container;
41 
42 /* ordered index */
43 
44 typedef nth_index<text_container,1>::type ordered_text;
45 
46 typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;
47 
main()48 int main()
49 {
50   std::string text=
51     "Alice was beginning to get very tired of sitting by her sister on the "
52     "bank, and of having nothing to do: once or twice she had peeped into the "
53     "book her sister was reading, but it had no pictures or conversations in "
54     "it, 'and what is the use of a book,' thought Alice 'without pictures or "
55     "conversation?'";
56 
57   /* feed the text into the container */
58 
59   text_container tc;
60   text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
61   std::copy(tok.begin(),tok.end(),std::back_inserter(tc));
62 
63   /* list all words in alphabetical order along with their number
64    * of occurrences
65    */
66 
67   ordered_text& ot=get<1>(tc);
68   for(ordered_text::iterator it=ot.begin();it!=ot.end();){
69     std::cout<<std::left<<std::setw(14)<<*it<<":";  /* print the word */
70     ordered_text::iterator it2=ot.upper_bound(*it); /* jump to next   */
71     std::cout<<std::right<<std::setw(3)   /* and compute the distance */
72              <<std::distance(it,it2)<<" times"<<std::endl;
73     it=it2;
74   }
75 
76   /* reverse the text and print it out */
77 
78   tc.reverse();
79   std::cout<<std::endl;
80   std::copy(
81     tc.begin(),tc.end(),std::ostream_iterator<std::string>(std::cout," "));
82   std::cout<<std::endl;
83   tc.reverse(); /* undo */
84 
85   /* delete most common English words and print the result */
86 
87   std::string common_words[]=
88     {"the","of","and","a","to","in","is","you","that","it",
89      "he","for","was","on","are","as","with","his","they","at"};
90 
91   for(std::size_t n=0;n<sizeof(common_words)/sizeof(common_words[0]);++n){
92     ot.erase(common_words[n]);
93   }
94   std::cout<<std::endl;
95   std::copy(
96     tc.begin(),tc.end(),std::ostream_iterator<std::string>(std::cout," "));
97   std::cout<<std::endl;
98 
99   return 0;
100 }
101