Finding the Last Occurrence of an Element in a Container

How do I search for the last occurrence of an element using std::ranges::find() or std::find()?

To find the last occurrence of an element in a container using std::ranges::find() or std::find(), you can use the std::ranges::find_end() algorithm.

This algorithm searches for the last occurrence of a subsequence in a range, which can be adapted to find a single element.

Using std::ranges::find_end()

Here's how you can use std::ranges::find_end() to find the last occurrence of an element:

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

int main() {
  std::vector<int> numbers{1, 2, 3, 4, 2, 5, 2};

  int target = 2;
  auto result = std::ranges::find_end(
    numbers, std::views::single(target));

  if (result.begin() != numbers.end()) {
    std::cout << "Last occurrence of "
      << target << " found at position "
      << std::distance(
        numbers.begin(), result.begin());
  } else {
    std::cout << target << " not found";
  }
}
Last occurrence of 2 found at position 6

Explanation

  • std::ranges::find_end() is used to find the last occurrence of a subsequence in a range.
  • In this case, the subsequence is a single element, represented by a pointer to the target and one past the target.

Alternative: Using Reverse Iterators

Another way to find the last occurrence is to use reverse iterators with std::find():

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

int main() {
  std::vector<int> numbers{1, 2, 3, 4, 2, 5, 2};

  int target = 2;
  auto rresult = std::find(
    numbers.rbegin(), numbers.rend(), target);

  if (rresult != numbers.rend()) {
    auto position = std::distance(
      numbers.begin(), rresult.base()) - 1;
    std::cout << "Last occurrence of "
      << target << " found at position "
      << position;
  } else {
    std::cout << target << " not found";
  }
}
Last occurrence of 2 found at position 6

Explanation

  • std::find() is used with reverse iterators (rbegin() and rend()) to search the container from the end to the beginning.
  • The position is calculated by converting the reverse iterator back to a normal iterator and adjusting the index.

Using either std::ranges::find_end() or reverse iterators with std::find(), you can efficiently find the last occurrence of an element in a container.

Search Algorithms

An introduction to the 8 main searching algorithms in the C++ standard library, including find(), find_if(), find_if_not(), find_first_of(), adjacent_find(), search_n(), search(), and find_end().

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Using std::ranges::find() with Custom Data Types
How can I use std::ranges::find() with custom data types that don't implement the equality operator ==?
Case-Insensitive Search Using std::ranges::find_if()
How can I make std::ranges::find_if() case-insensitive when searching through a container of strings?
Searching for Multiple Occurrences of a Value in a Container
What is the best way to search for multiple occurrences of a value in a container using standard library algorithms?
Searching for a Substring Appearing Multiple Times
How do I handle searching for a substring within a string when the substring can appear multiple times?
Boyer-Moore vs Boyer-Moore-Horspool Search Algorithms
What are the differences and benefits of using std::boyer_moore_searcher versus std::boyer_moore_horspool_searcher in practical scenarios?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant