bind_goose.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*=============================================================================
  2. For Boost Bind:
  3. Copyright (c) 2001-2004 Peter Dimov and Multi Media Ltd.
  4. Copyright (c) 2001 David Abrahams
  5. Copyright (c) 2005 Peter Dimov
  6. For Boost Phoenix:
  7. Copyright (c) 2001-2010 Joel de Guzman
  8. Copyright (c) 2010 Thomas Heller
  9. For the example:
  10. Copyright (c) 2011 Paul Heil
  11. Copyright (c) 2015 John Fletcher
  12. Distributed under the Boost Software License, Version 1.0. (See accompanying
  13. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  14. ==============================================================================*/
  15. // bind_goose.cpp
  16. // This example is based on code by Paul Heil to be found here:
  17. // http://www.codeproject.com/Tips/248492/How-does-boost-phoenix-improve-boost-bind
  18. //
  19. // Show different ways of using boost bind and phoenix to handle deletion.
  20. //
  21. #include <iostream>
  22. #include <boost/function.hpp>
  23. #include <boost/bind.hpp>
  24. #include <boost/phoenix/core.hpp>
  25. #include <boost/phoenix/bind.hpp>
  26. #include <boost/phoenix/operator/comparison.hpp>
  27. #include <boost/phoenix/stl/algorithm/transformation.hpp>
  28. #include <functional>
  29. #include <string>
  30. #include <vector>
  31. ////////////////////////////////////////////
  32. // Set up the list here
  33. ////////////////////////////////////////////
  34. std::vector< std::string > make_list() {
  35. std::vector< std::string > list;
  36. list.push_back( "duck" );
  37. list.push_back( "duck" );
  38. list.push_back( "goose" );
  39. return list;
  40. }
  41. //////////////////////////////////////////////
  42. // First example using standard library only
  43. //////////////////////////////////////////////
  44. bool IsGoose( const std::string& s )
  45. {
  46. return s == "goose";
  47. }
  48. void delete_value1(std::vector< std::string > &list )
  49. {
  50. list.erase( std::remove_if( list.begin(), list.end(), IsGoose ), list.end() );
  51. }
  52. void out_string(const std::string &s)
  53. {
  54. std::cout << s << std::endl;
  55. }
  56. void show_list1( const std::vector< std::string > &list )
  57. {
  58. std::for_each(list.begin(), list.end(), out_string);
  59. }
  60. //////////////////////////////////////////////
  61. // Second example using boost bind
  62. //////////////////////////////////////////////
  63. bool isValue(const std::string &s1, const std::string &s2)
  64. {
  65. return s1==s2;
  66. }
  67. void delete_value2(std::vector< std::string > &list, const std::string & value)
  68. {
  69. list.erase(
  70. std::remove_if(
  71. list.begin(),
  72. list.end(),
  73. boost::bind(
  74. isValue, // &isValue works as well.
  75. _1, // Boost.Bind placeholder
  76. boost::cref( value ) ) ),
  77. list.end() );
  78. }
  79. ///////////////////////////////////////////////////////
  80. // Third example using boost phoenix for the comparison
  81. ///////////////////////////////////////////////////////
  82. namespace phx = boost::phoenix;
  83. using phx::placeholders::arg1;
  84. using phx::placeholders::arg2;
  85. void delete_value3(std::vector< std::string > &list, const std::string & value)
  86. {
  87. list.erase( std::remove_if(
  88. list.begin(),
  89. list.end(),
  90. // This needs header boost/phoenix/operator/comparison.
  91. // arg1 is a Boost.Phoenix placeholder.
  92. arg1 == phx::cref( value ) ),
  93. list.end() );
  94. }
  95. //////////////////////////////////////////////////////////////
  96. // Third example using boost phoenix for the algorithm as well
  97. //////////////////////////////////////////////////////////////
  98. void delete_value4(std::vector< std::string > &list, const std::string & value)
  99. {
  100. // This need header boost/phoenix/stl/algorithm/transformation
  101. list.erase( phx::remove_if( arg1, arg2 )
  102. ( list, arg1 == phx::cref( value ) ),
  103. list.end() );
  104. }
  105. int main() {
  106. std::cout << "--------------------------------" << std::endl;
  107. std::cout << "Delete the goose examples." << std::endl;
  108. std::cout << "--------------------------------" << std::endl;
  109. std::string value = "goose";
  110. std::vector< std::string > list1 = make_list();
  111. delete_value1(list1);
  112. show_list1(list1);
  113. std::cout << "--------------------------------" << std::endl;
  114. std::vector< std::string > list2 = make_list();
  115. delete_value2(list2,value);
  116. show_list1(list2);
  117. std::cout << "--------------------------------" << std::endl;
  118. std::vector< std::string > list3 = make_list();
  119. delete_value3(list3,value);
  120. show_list1(list3);
  121. std::cout << "--------------------------------" << std::endl;
  122. std::vector< std::string > list4 = make_list();
  123. delete_value4(list4,value);
  124. show_list1(list4);
  125. std::cout << "--------------------------------" << std::endl;
  126. return 0;
  127. }