Users Online
· Guests Online: 37
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
adjacent_find() Function in C++
adjacent_find() Function in C++
This C++ program demonstrates the adjacent_find() algorithm. The adjacent_find takes iterator to the beginning and end of a container and an optional predicate as third parameter. The program uses function adjacent_find() to find adjacent pair of elements in which both the elements are same or satisfy boolean true for a predicate passed as a parameter.
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)
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.