I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com

adjacent_find

Searches for two adjacent elements that are equal (by some criterion)
+ View version details

Key Facts

Gyroscopic Couple: The rate of change of angular momentum (\tau) = I\omega\Omega (In the limit).
  • I = Moment of Inertia.
  • \omega = Angular velocity
  • \Omega = Angular velocity of precession.


Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

Definition

The adjacent_find() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template < class ForwardIterator >
   ForwardIterator adjacent_find(
      ForwardIterator first, 
      ForwardIterator last
   );
 
template < class ForwardIterator , class BinaryPredicate >
   ForwardIterator adjacent_find(
      ForwardIterator first, 
      ForwardIterator last, 
      BinaryPredicate comp
   );

Parameters:
Parameter Description
first A forward iterator addressing the position of the first element in the range to be searched
last A forward iterator addressing the position one past the final element in the range to be searched
comp The binary predicate giving the condition to be satisfied by the values of the adjacent elements in the range being searched

Description

The adjacent_find algorithm searches for two adjacent elements that are either equal or satisfy a specified condition.

Return Value

The first version of adjacent_find returns the first iterator i such that i and i+1 are both valid iterators in [first, last), and such that *i == *(i+1). It returns last if no such iterator exists.

The second version of adjacent_find returns the first iterator i such that i and i+1 are both valid iterators in [first, last) and such that binary_pred (*i, *(i+1)) is true. It returns last if no such iterator exists.

Complexity

The complexity is linear. If first == last then no comparison are performed; otherwise, at most (last - first) - 1 comparisons.
Example:

Example - adjacent_find function
Problem
This program illustrates the use of the STL adjacent_find() algorithm (default version) to find adjacent equal values in a vector of integers.
Workings
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
  int a[] = {1, 2, 3, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10};
  vector<int> v(a, a+12);
  cout <<"\nHere are the values in the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  vector<int>::iterator p = adjacent_find(v.begin(), v.end());
  if (p == v.end())
    cout <<"\nThere are no, or no more, matching pairs.";
  else
    cout <<"\nFirst value of first matching pair is "<<*p<<".";
 
  p = adjacent_find(p+1, v.end());
  if (p == v.end())
    cout <<"\nThere are no, or no more, matching pairs.";
  else
    cout <<"\nFirst value of next matching pair is "<<*p<<".";
 
  p = adjacent_find(p+1, v.end());
  if (p == v.end())
    cout <<"\nThere are no, or no more, matching pairs.";
  else
    cout <<"\nFirst value of next matching pair is "<<*p<<".";
 
  p = adjacent_find(p+1, v.end());
  if (p == v.end())
    cout <<"\nThere are no, or no more, matching pairs.";
  else
    cout <<"\nFirst value of next matching pair is "<<*p<<".";
 
  return 0;
}
Solution
Output:

Here are the values in the vector:
1 2 3 3 3 4 5 6 7 7 8 9

First value of first matching pair is 3.

First value of next matching pair is 3.

First value of next matching pair is 7.

There are no, or no more, matching pairs.
References