random_access.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Boost.MultiIndex example of use of random access 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/random_access_index.hpp>
  18. #include <boost/tokenizer.hpp>
  19. #include <algorithm>
  20. #include <iostream>
  21. #include <iterator>
  22. #include <string>
  23. using boost::multi_index_container;
  24. using namespace boost::multi_index;
  25. /* text_container holds words as inserted and also keep them indexed
  26. * by dictionary order.
  27. */
  28. typedef multi_index_container<
  29. std::string,
  30. indexed_by<
  31. random_access<>,
  32. ordered_non_unique<identity<std::string> >
  33. >
  34. > text_container;
  35. /* ordered index */
  36. typedef nth_index<text_container,1>::type ordered_text;
  37. /* Helper function for obtaining the position of an element in the
  38. * container.
  39. */
  40. template<typename IndexIterator>
  41. text_container::size_type text_position(
  42. const text_container& tc,IndexIterator it)
  43. {
  44. /* project to the base index and calculate offset from begin() */
  45. return project<0>(tc,it)-tc.begin();
  46. }
  47. typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;
  48. int main()
  49. {
  50. std::string text=
  51. "'Oh, you wicked little thing!' cried Alice, catching up the kitten, "
  52. "and giving it a little kiss to make it understand that it was in "
  53. "disgrace. 'Really, Dinah ought to have taught you better manners! You "
  54. "ought, Dinah, you know you ought!' she added, looking reproachfully at "
  55. "the old cat, and speaking in as cross a voice as she could manage "
  56. "-- and then she scrambled back into the armchair, taking the kitten and "
  57. "the worsted with her, and began winding up the ball again. But she "
  58. "didn't get on very fast, as she was talking all the time, sometimes to "
  59. "the kitten, and sometimes to herself. Kitty sat very demurely on her "
  60. "knee, pretending to watch the progress of the winding, and now and then "
  61. "putting out one paw and gently touching the ball, as if it would be glad "
  62. "to help, if it might.";
  63. /* feed the text into the container */
  64. text_container tc;
  65. tc.reserve(text.size()); /* makes insertion faster */
  66. text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
  67. std::copy(tok.begin(),tok.end(),std::back_inserter(tc));
  68. std::cout<<"enter a position (0-"<<tc.size()-1<<"):";
  69. text_container::size_type pos=tc.size();
  70. std::cin>>pos;
  71. if(pos>=tc.size()){
  72. std::cout<<"out of bounds"<<std::endl;
  73. }
  74. else{
  75. std::cout<<"the word \""<<tc[pos]<<"\" appears at position(s): ";
  76. std::pair<ordered_text::iterator,ordered_text::iterator> p=
  77. get<1>(tc).equal_range(tc[pos]);
  78. while(p.first!=p.second){
  79. std::cout<<text_position(tc,p.first++)<<" ";
  80. }
  81. std::cout<<std::endl;
  82. }
  83. return 0;
  84. }