at_function_examples.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <cassert>
  22. #include <boost/bimap/bimap.hpp>
  23. #include <boost/bimap/unordered_set_of.hpp>
  24. #include <boost/bimap/list_of.hpp>
  25. #include <boost/bimap/multiset_of.hpp>
  26. using namespace boost::bimaps;
  27. void first_bimap()
  28. {
  29. //[ code_at_function_first
  30. typedef bimap< set_of< std::string >, list_of< int > > bm_type;
  31. bm_type bm;
  32. try
  33. {
  34. bm.left.at("one") = 1; // throws std::out_of_range
  35. }
  36. catch( std::out_of_range & e ) {}
  37. assert( bm.empty() );
  38. bm.left["one"] = 1; // Ok
  39. assert( bm.left.at("one") == 1 ); // Ok
  40. //]
  41. }
  42. void second_bimap()
  43. {
  44. //[ code_at_function_second
  45. typedef bimap< multiset_of<std::string>, unordered_set_of<int> > bm_type;
  46. bm_type bm;
  47. //<-
  48. /*
  49. //->
  50. bm.right[1] = "one"; // compilation error
  51. //<-
  52. */
  53. //->
  54. bm.right.insert( bm_type::right_value_type(1,"one") );
  55. assert( bm.right.at(1) == "one" ); // Ok
  56. try
  57. {
  58. std::cout << bm.right.at(2); // throws std::out_of_range
  59. }
  60. catch( std::out_of_range & e ) {}
  61. //<-
  62. /*
  63. //->
  64. bm.right.at(1) = "1"; // compilation error
  65. //<-
  66. */
  67. //->
  68. //]
  69. }
  70. int main()
  71. {
  72. first_bimap();
  73. second_bimap();
  74. return 0;
  75. }