C++ algorithm adjacent_find() function
Example
Find adjacent elements in a vector:
vector<string> cars = {"Volvo", "BMW", "Ford", "Ford", "Mazda"};
auto it = adjacent_find(cars.begin(), cars.end());
if (it != numbers.end()) {
cout << *it << " appears twice in a row\n";
} else {
cout << "There are no adjacent elements\n";
}
Try it Yourself »
Definition and Usage
The adjacent_find()
function finds the first adjacent pair in a data range and returns an iterator pointing to the first element in the pair. An adjacent pair is a pair of elements that have the same value and are next to each other (adjacent) in the data range.
The range of data is specified by iterators.
Syntax
adjacent_find(iterator start, iterator end);
Parameter Values
Parameter | Description |
---|---|
start | Required. An iterator pointing to the start of the data range being searched. |
end | Required. An iterator pointing to the end of the data range being searched. Elements up to this position will be searched, but the element at this position will not be included. |
Technical Details
Returns: | An iterator pointing to the first element of the first adjacent pair in a data range. |
---|
Related Pages
Read more about data structures in our Data Structures Tutorial.
Read more about iterators in our Iterators Tutorial.
Read more about algorithms in our Algorithms Tutorial.