sequenced.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #if !defined(NDEBUG)
  11. #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
  12. #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
  13. #endif
  14. #include <boost/multi_index_container.hpp>
  15. #include <boost/multi_index/identity.hpp>
  16. #include <boost/multi_index/ordered_index.hpp>
  17. #include <boost/multi_index/sequenced_index.hpp>
  18. #include <boost/tokenizer.hpp>
  19. #include <algorithm>
  20. #include <iomanip>
  21. #include <iostream>
  22. #include <iterator>
  23. #include <string>
  24. using boost::multi_index_container;
  25. using namespace boost::multi_index;
  26. /* text_container holds words as inserted and also keep them indexed
  27. * by dictionary order.
  28. */
  29. typedef multi_index_container<
  30. std::string,
  31. indexed_by<
  32. sequenced<>,
  33. ordered_non_unique<identity<std::string> >
  34. >
  35. > text_container;
  36. /* ordered index */
  37. typedef nth_index<text_container,1>::type ordered_text;
  38. typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;
  39. int main()
  40. {
  41. std::string text=
  42. "Alice was beginning to get very tired of sitting by her sister on the "
  43. "bank, and of having nothing to do: once or twice she had peeped into the "
  44. "book her sister was reading, but it had no pictures or conversations in "
  45. "it, 'and what is the use of a book,' thought Alice 'without pictures or "
  46. "conversation?'";
  47. /* feed the text into the container */
  48. text_container tc;
  49. text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
  50. std::copy(tok.begin(),tok.end(),std::back_inserter(tc));
  51. /* list all words in alphabetical order along with their number
  52. * of occurrences
  53. */
  54. ordered_text& ot=get<1>(tc);
  55. for(ordered_text::iterator it=ot.begin();it!=ot.end();){
  56. std::cout<<std::left<<std::setw(14)<<*it<<":"; /* print the word */
  57. ordered_text::iterator it2=ot.upper_bound(*it); /* jump to next */
  58. std::cout<<std::right<<std::setw(3) /* and compute the distance */
  59. <<std::distance(it,it2)<<" times"<<std::endl;
  60. it=it2;
  61. }
  62. /* reverse the text and print it out */
  63. tc.reverse();
  64. std::cout<<std::endl;
  65. std::copy(
  66. tc.begin(),tc.end(),std::ostream_iterator<std::string>(std::cout," "));
  67. std::cout<<std::endl;
  68. tc.reverse(); /* undo */
  69. /* delete most common English words and print the result */
  70. std::string common_words[]=
  71. {"the","of","and","a","to","in","is","you","that","it",
  72. "he","for","was","on","are","as","with","his","they","at"};
  73. for(std::size_t n=0;n<sizeof(common_words)/sizeof(common_words[0]);++n){
  74. ot.erase(common_words[n]);
  75. }
  76. std::cout<<std::endl;
  77. std::copy(
  78. tc.begin(),tc.end(),std::ostream_iterator<std::string>(std::cout," "));
  79. std::cout<<std::endl;
  80. return 0;
  81. }