1: //adjacent_find2a.cpp
3: #include <iostream>
4: #include <vector>
5: #include <algorithm>
6: using namespace std;
8: /**
9: Determines whether two integers are both divisible by 5.
10: Pre:\n m and n contain integers.
11: Post:\n Returns true if m and n are both divisible by 5,
12: and otherwise returns false.
13: */
14: bool bothDivisibleByFive
15: (
16: int m, //in
17: int n //in
18: )
19: {
20: return (m%5 == 0) && (n%5 == 0);
21: }
23: int main()
24: {
25: cout << "\nThis program illustrates the use of the STL "
26: "adjacent_find() algorithm\n(extended version) to find "
27: "adjacent pairs of integers that are both\ndivisible by 5 "
28: "in a vector of integers.";
29: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
31: int a[] = {10, 21, 33, 40, 50, 60, 72, 77, 80, 95, 101};
32: vector<int> v(a, a+11);
33: cout << "\nHere are the values in the vector:\n";
34: for (vector<int>::size_type i=0; i<v.size(); i++)
35: cout << v.at(i) << " ";
36: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
38: vector<int>::iterator p =
39: adjacent_find(v.begin(), v.end(), bothDivisibleByFive);
40: if (p == v.end())
41: cout << "\nThere are no, or no more, matching pairs.";
42: else
43: cout << "\nThe first value of the first matching pair is "
44: << *p << ".";
45: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
47: p = adjacent_find(p+1, v.end(), bothDivisibleByFive);
48: if (p == v.end())
49: cout << "\nThere are no, or no more, matching pairs.";
50: else
51: cout << "\nThe first value of the next matching pair is "
52: << *p << ".";
53: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
55: p = adjacent_find(p+1, v.end(), bothDivisibleByFive);
56: if (p == v.end())
57: cout << "\nThere are no, or no more, matching pairs.";
58: else
59: cout << "\nThe first value of the next matching pair is "
60: << *p << ".";
61: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
63: p = adjacent_find(p+1, v.end(), bothDivisibleByFive);
64: if (p == v.end())
65: cout << "\nThere are no, or no more, matching pairs.";
66: else
67: cout << "\nThe first value of the next matching pair is "
68: << *p << ".";
69: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
70: }