repetitions_counter.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Boost.Bimap
  2. //
  3. // Copyright (c) 2006-2007 Matias Capeletto
  4. //
  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)
  8. // VC++ 8.0 warns on usage of certain Standard Library and API functions that
  9. // can be cause buffer overruns or other possible security issues if misused.
  10. // See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
  11. // But the wording of the warning is misleading and unsettling, there are no
  12. // portable alternative functions, and VC++ 8.0's own libraries use the
  13. // functions in question. So turn off the warnings.
  14. #define _CRT_SECURE_NO_DEPRECATE
  15. #define _SCL_SECURE_NO_DEPRECATE
  16. // Boost.Bimap Example
  17. //-----------------------------------------------------------------------------
  18. #include <boost/config.hpp>
  19. #include <iostream>
  20. #include <boost/tokenizer.hpp>
  21. #include <boost/bimap/bimap.hpp>
  22. #include <boost/bimap/unordered_set_of.hpp>
  23. #include <boost/bimap/list_of.hpp>
  24. using namespace boost::bimaps;
  25. struct counter {
  26. counter() : c(0) {}
  27. counter& operator++() { ++c; return *this; }
  28. unsigned int operator++(int) { return c++; }
  29. operator const unsigned int() const { return c; }
  30. private:
  31. unsigned int c;
  32. };
  33. int main()
  34. {
  35. //[ code_repetitions_counter
  36. typedef bimap
  37. <
  38. unordered_set_of< std::string >,
  39. list_of< counter > /*< `counter` is an integer that is initialized
  40. in zero in the constructor >*/
  41. > word_counter;
  42. typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;
  43. std::string text=
  44. "Relations between data in the STL are represented with maps."
  45. "A map is a directed relation, by using it you are representing "
  46. "a mapping. In this directed relation, the first type is related to "
  47. "the second type but it is not true that the inverse relationship "
  48. "holds. This is useful in a lot of situations, but there are some "
  49. "relationships that are bidirectional by nature.";
  50. // feed the text into the container
  51. word_counter wc;
  52. text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
  53. for( text_tokenizer::const_iterator it = tok.begin(), it_end = tok.end();
  54. it != it_end ; ++it )
  55. {
  56. /*<< Because the right collection type is `list_of`, the right data
  57. is not used a key and can be modified in the same way as with
  58. standard maps. >>*/
  59. ++ wc.left[*it];
  60. }
  61. // list words with counters by order of appearance
  62. /*<< When we insert the elements using the left map view, the element
  63. is inserted at the end of the list. >>*/
  64. for( word_counter::right_const_iterator
  65. wit = wc.right.begin(), wit_end = wc.right.end();
  66. wit != wit_end; ++wit )
  67. {
  68. std::cout << wit->second << ": " << wit->first;
  69. }
  70. //]
  71. return 0;
  72. }