| 5 | #include "Box.h" // From Ex13_03A |
| 6 | |
| 7 | int main() |
| 8 | { |
| 9 | std::vector boxes{ Box{1,2,3}, Box{5,2,3}, Box{9,2,1}, Box{3,2,1} }; |
| 10 | |
| 11 | // Define a lambda functor to print the result of find() or find_if(): |
| 12 | auto print_result{ [&boxes] (auto result) |
| 13 | { |
| 14 | if (result == end(boxes)) |
| 15 | std::cout << "No box found." << std::endl; |
| 16 | else |
| 17 | std::cout << "Found matching box at position " |
| 18 | << (result - begin(boxes)) << std::endl; |
| 19 | }}; |
| 20 | |
| 21 | // Find an exact box |
| 22 | Box box_to_find{ 3,2,1 }; |
| 23 | auto result{ std::find(begin(boxes), end(boxes), box_to_find) }; |
| 24 | print_result(result); |
| 25 | |
| 26 | // Find a box with a volume larger than that of box_to_find |
| 27 | const auto required_volume{ box_to_find.volume() }; |
| 28 | result = std::find_if(begin(boxes), end(boxes), |
| 29 | [required_volume](const Box& box) { return box > required_volume; }); |
| 30 | print_result(result); |
| 31 | } |