]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multi_index/example/sequenced.cpp
1 /* Boost.MultiIndex example of use of sequenced indices.
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)
8 * See http://www.boost.org/libs/multi_index for library home page.
12 #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
13 #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
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>
27 using boost::multi_index_container
;
28 using namespace boost::multi_index
;
30 /* text_container holds words as inserted and also keep them indexed
31 * by dictionary order.
34 typedef multi_index_container
<
38 ordered_non_unique
<identity
<std::string
> >
44 typedef nth_index
<text_container
,1>::type ordered_text
;
46 typedef boost::tokenizer
<boost::char_separator
<char> > text_tokenizer
;
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 "
57 /* feed the text into the container */
60 text_tokenizer
tok(text
,boost::char_separator
<char>(" \t\n.,;:!?'\"-"));
61 std::copy(tok
.begin(),tok
.end(),std::back_inserter(tc
));
63 /* list all words in alphabetical order along with their number
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
;
76 /* reverse the text and print it out */
81 tc
.begin(),tc
.end(),std::ostream_iterator
<std::string
>(std::cout
," "));
83 tc
.reverse(); /* undo */
85 /* delete most common English words and print the result */
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"};
91 for(std::size_t n
=0;n
<sizeof(common_words
)/sizeof(common_words
[0]);++n
){
92 ot
.erase(common_words
[n
]);
96 tc
.begin(),tc
.end(),std::ostream_iterator
<std::string
>(std::cout
," "));