user_defined_names.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <string>
  20. #include <iostream>
  21. #include <boost/bimap/bimap.hpp>
  22. #include <boost/bimap/multiset_of.hpp>
  23. using namespace boost::bimaps;
  24. void untagged_version()
  25. {
  26. //[ code_user_defined_names_untagged_version
  27. typedef bimap
  28. <
  29. multiset_of<std::string>,
  30. int
  31. > People;
  32. People people;
  33. // ...
  34. int user_id;
  35. std::cin >> user_id;
  36. // people.right : map<id,name>
  37. People::right_const_iterator id_iter = people.right.find(user_id);
  38. if( id_iter != people.right.end() )
  39. {
  40. // first : id
  41. // second : name
  42. std::cout << "name: " << id_iter->second << std::endl
  43. << "id: " << id_iter->first << std::endl;
  44. }
  45. else
  46. {
  47. std::cout << "Unknown id, users are:" << std::endl;
  48. // people.left : map<name,id>
  49. for( People::left_const_iterator
  50. name_iter = people.left.begin(),
  51. iend = people.left.end();
  52. name_iter != iend; ++name_iter )
  53. {
  54. // first : name
  55. // second : id
  56. std::cout << "name: " << name_iter->first << std::endl
  57. << "id: " << name_iter->second << std::endl;
  58. }
  59. }
  60. //]
  61. }
  62. struct id {};
  63. struct name {};
  64. void tagged_version()
  65. {
  66. //[ code_user_defined_names_tagged_version
  67. //<-
  68. /*
  69. //->
  70. struct id {}; // Tag for the identification number
  71. struct name {}; // Tag for the name of the person
  72. //<-
  73. */
  74. //->
  75. typedef bimap
  76. <
  77. tagged< int , id > ,
  78. multiset_of< tagged< std::string, name > >
  79. > People;
  80. People people;
  81. // ...
  82. int user_id;
  83. std::cin >> user_id;
  84. People::map_by<id>::const_iterator id_iter = people.by<id>().find(user_id);
  85. if( id_iter != people.by<id>().end() )
  86. {
  87. std::cout << "name: " << id_iter->get<name>() << std::endl
  88. << "id: " << id_iter->get<id>() << std::endl;
  89. }
  90. else
  91. {
  92. std::cout << "Unknown id, users are:" << std::endl;
  93. for( People::map_by<name>::const_iterator
  94. name_iter = people.by<name>().begin(),
  95. iend = people.by<name>().end();
  96. name_iter != iend; ++name_iter )
  97. {
  98. std::cout << "name: " << name_iter->get<name>() << std::endl
  99. << "id: " << name_iter->get<id>() << std::endl;
  100. }
  101. }
  102. //]
  103. }
  104. int main()
  105. {
  106. untagged_version();
  107. tagged_version();
  108. return 0;
  109. }