find.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Range v3 library
  2. //
  3. // Copyright Jeff Garland 2017
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. //
  12. ///[find]
  13. // vector: *i: 6
  14. // didn't find 10
  15. // *i: 6
  16. // *i: 2
  17. // *i after ++ (2 expected): 2
  18. // array: *i: 6
  19. // list: *i: 6
  20. // fwd_list: *i: 4
  21. // deque: *i: 6
  22. #include <array>
  23. #include <deque>
  24. #include <forward_list>
  25. #include <iostream>
  26. #include <list>
  27. #include <range/v3/all.hpp>
  28. #include <vector>
  29. using std::cout;
  30. auto is_six = [](int i) -> bool { return i == 6; };
  31. int
  32. main()
  33. {
  34. cout << "vector: ";
  35. std::vector<int> v{6, 2, 6, 4, 6, 1};
  36. {
  37. auto i = ranges::find(v, 6); // 1 2 3 4 5 6
  38. cout << "*i: " << *i << '\n';
  39. }
  40. {
  41. auto i = ranges::find(v, 10); // 1 2 3 4 5 6
  42. if(i == ranges::end(v))
  43. {
  44. cout << "didn't find 10\n";
  45. }
  46. }
  47. {
  48. auto i = ranges::find_if(v, is_six);
  49. if(i != ranges::end(v))
  50. {
  51. cout << "*i: " << *i << '\n';
  52. }
  53. }
  54. {
  55. auto i = ranges::find_if_not(v, is_six);
  56. if(i != ranges::end(v))
  57. {
  58. cout << "*i: " << *i << '\n';
  59. }
  60. }
  61. {
  62. auto i = ranges::find(v, 6);
  63. i++;
  64. if(i != ranges::end(v))
  65. {
  66. cout << "*i after ++ (2 expected): " << *i;
  67. }
  68. }
  69. cout << "\narray: ";
  70. std::array<int, 6> a{6, 2, 3, 4, 5, 1};
  71. {
  72. auto i = ranges::find(a, 6);
  73. if(i != ranges::end(a))
  74. {
  75. cout << "*i: " << *i;
  76. }
  77. }
  78. cout << "\nlist: ";
  79. std::list<int> li{6, 2, 3, 4, 5, 1};
  80. {
  81. auto i = ranges::find(li, 6);
  82. if(i != ranges::end(li))
  83. {
  84. cout << "*i: " << *i;
  85. }
  86. }
  87. cout << "\nfwd_list: ";
  88. std::forward_list<int> fl{6, 2, 3, 4, 5, 1};
  89. {
  90. auto i = ranges::find(fl, 4);
  91. if(i != ranges::end(fl))
  92. {
  93. cout << "*i: " << *i;
  94. }
  95. }
  96. cout << "\ndeque: ";
  97. std::deque<int> d{6, 2, 3, 4, 5, 1};
  98. {
  99. auto i = ranges::find(d, 6);
  100. if(i != ranges::end(d))
  101. {
  102. cout << "*i: " << *i;
  103. }
  104. }
  105. cout << '\n';
  106. }
  107. ///[find]