adjacent_find() in C++

adjacent_find(): This function is a build-in function in STL. This function is 

available in the file stl_algo.h

This function finds two adjacent values in a sequence that are equal. If there are 

two adjacent values that are equal it returns an iterator to that value. 

Otherwise, it returns any unexpected value. 

Parameters: Two parameters are required for this function.

__first – A forward iterator. 

__last – A forward iterator. 

Returns: The first iterator i such that i and i+1 are both valid iterators in [__first,__last) and such

 that *i == *(i+1), or __last if no such iterator exists.

For Example:

arr  = {3, 4, 5, 1, 1, 10}

adjacent_find(arr.begin(),arr.end()) = > It returns iterator to 1  

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<intarr = {3451110};

    vector<int>::iterator it = adjacent_find(arr.begin(), 
arr.end());

    cout << *it << "\n";
    return 0;
}


No comments:

Post a Comment