Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/adjacent find"

From cppreference.com
< cpp‎ | algorithm
(Complexity for the "found" case had an erroneous "+1".)
(Refining the "complexity" statement.)
Line 29: Line 29:
 
===Complexity===
 
===Complexity===
  
Exactly the smaller of {{tt|(result - first)}} and {{tt|(last - first) - 1}} applications of the predicate where {{tt|result}} is the return value.
+
Exactly the smaller of {{tt|(result - first)}} and {{tt|((last - 1) - first)}} applications of the predicate where {{tt|result}} is the return value.
  
 
===Possible implementation===
 
===Possible implementation===

Revision as of 12:41, 22 October 2012

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <algorithm>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
template< class ForwardIt >
ForwardIt adjacent_find( ForwardIt first, ForwardIt last );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class ForwardIt, BinaryPredicate p >
ForwardIt adjacent_find( ForwardIt first, ForwardIt last, BinaryPredicate p );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Searches the range [first, last) for two consecutive identical elements. The first version uses operator== to compare the elements, the second version uses the given binary predicate p.

Contents

Parameters

first, last - the range of elements to examine
p - binary predicate which returns ​true if the elements should be treated as equal.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type1 &a, const Type2 &b);

While the signature does not need to have const &, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value category (thus, Type1 & is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy(since C++11)).
The types Type1 and Type2 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to both of them. ​

Type requirements
-
ForwardIt must meet the requirements of LegacyForwardIterator.

Return value

an iterator to the first of the identical elements. If no such elements are found, last is returned

Complexity

Exactly the smaller of (result - first) and ((last - 1) - first) applications of the predicate where result is the return value.

Possible implementation

First version
template<class ForwardIt>
ForwardIt adjacent_find(ForwardIt first, ForwardIt last)
{
    if (first == last) {
        return last;
    }
    ForwardIt next = first;
    ++next;
    for (next != last; ++next, ++first) {
        if (*first == *next) {
            return first;
        }
    }
    return last;
}
Second version
template<class ForwardIt, BinaryPredicate p>
ForwardIt adjacent_find(ForwardIt first, ForwardIt last, 
                        BinaryPredicate p)
{
    if (first == last) {
        return last;
    }
    ForwardIt next = first;
    ++next;
    for (next != last; ++next, ++first) {
        if (p(*first, *next)) {
            return first;
        }
    }
    return last;
}

Example

The following code finds a pair of equivalent integers in an array of intergers.

#include <algorithm>
#include <iostream>
 
int main()
{
    std::vector<int> v1{0, 1, 2, 3, 40, 40, 5};
 
    std::vector<int>::iterator result;
    result = std::adjacent_find(v1.begin(), v1.end());
 
    if (result == v1.end()) {
        std::cout << "no matching adjacent elements";
    } else {
        std::cout << "match at: " << std::distance(v1.begin(), result);
    }
}

Output:

match at: 4

See also

Template:cpp/algorithm/dcl list unique