Using adjacent_find in C++

The ‘adjacent_find’ function in C++ is a binary function that returns the first pair of adjacent elements in a range that satisfy…

The ‘adjacent_find’ function in C++ is a binary function that returns the first pair of adjacent elements in a range that satisfy a given condition. By default, this function checks for equality between adjacent elements. In this article, we will explore how to use the ‘adjacent_find’ function with an example.

Description

The ‘adjacent_find’ function is used to search for the first occurrence of two consecutive elements in a range that meet a specified condition. If no condition is provided, the default behavior is to check for equality. This function is part of the ‘<algorithm>‘ header.

Syntax

template <class ForwardIterator>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last);

template <class ForwardIterator, class BinaryPredicate>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
  • first, last’: Input iterators to the initial and final positions of the sequence to be searched.
  • ‘pred’ (optional): Binary function that accepts two elements as arguments and returns a ‘bool’. The function returns ‘true’ if the elements should be considered a match, and false otherwise.

Example

Here is an example demonstrating how to use ‘adjacent_find’ in C++:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };

    // Binary function which returns first adjacent element pairs based on certain condition (as third argument).
    // Default condition checks equality.
    auto i = std::adjacent_find(v.begin(), v.end());

    if (i != v.end()) {
        std::cout << "The first pair of repeated elements are: " << *i << '\n';
    } else {
        std::cout << "No adjacent elements found.\n";
    }

    return 0;
}

Explanation

In this example, we have a vector ‘v‘ containing integers. The ‘adjacent_find’ function searches for the first pair of adjacent elements that are equal.

  • ‘std::adjacent_find(v.begin()’, ‘v’.’end())’ searches the range ‘[v.begin(), v.end())’ for the first pair of adjacent elements that satisfy the default condition (equality).
  • If a pair is found, the iterator i points to the first element of the pair, and the value is printed.
  • If no pair is found, the iterator i is equal to ‘v’.’end()’, and a message is printed indicating that no adjacent elements were found.

Use Case: Finding Adjacent Duplicates

A common use case for ‘adjacent_find’ is to detect adjacent duplicates in a collection. This can be useful in scenarios where data integrity checks are needed, such as ensuring that no duplicate entries exist in a sorted list.

Wrapping It Up

The ‘adjacent_find‘ function is a useful tool in C++ for finding pairs of adjacent elements that meet a specified condition. By understanding how to use this function, you can efficiently perform searches for adjacent duplicates or other conditions in your data. This function can help you manage and analyze data effectively, ensuring that your applications handle sequences of elements correctly.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *