Here is the source code of the C++ program which demonstrates the adjacent_find() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to demonstrate adjacent_find algorithm on string vector
*/
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <iomanip>
using namespace std;
void print(int a[], int len)
{
for(int i = 0; i < len; i++)
{
cout << setw(2) << a[i] << " ";
}
cout << endl;
}
int main()
{
int a[] = {1, 2, 3, 3, 5, 6, 6, 3};
int alen = sizeof(a) / sizeof(int);
int * ii = a, * ij = a;
cout << "Array : ";
print(a, alen);
// Finding without use of predicates
cout << "Pairwise elements (a, b) where a == b : ";
while(ii <= a + alen)
{
ii = adjacent_find(ii, a + alen);
if(ii != a + alen)
cout << "(" << *ii << ", " << *(ii + 1) << ") ";
ii = ii + 2;
}
cout << endl;
// Finding with use of predicates
sort(a, a + alen, greater<int>());
cout << "Pairwise elements (a, b) where a >= b : ";
while(ij <= a + alen)
{
ij = adjacent_find(ij, a + alen, greater_equal<int>());
if(ij != a + alen)
cout << "(" << *ij << ", " << *(ij + 1) << ") ";
ij = ij + 2;
}
cout << endl;
}
$ a.out Array : 1 2 3 3 5 6 6 3 Pairwise elements (a, b) where a == b : (3, 3) (6, 6) Pairwise elements (a, b) where a >= b : (6, 6) (5, 3) (3, 3) (2, 1)