How to Determine the Size of a Range Defined by a Sentinel in C++

How can you determine the size of a range defined by a sentinel?

To determine the size of a range defined by a sentinel in C++, you typically use the iterator returned by the algorithm that processes the range. Most range-based standard library algorithms return an object that includes an iterator pointing to where the sentinel was triggered.

For example, std::ranges::for_each() returns an object with an in member, which is the iterator to where the algorithm stopped. You can use this iterator to calculate the distance from the beginning of the range, giving you the size of the range.

Here's an example:

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

template <typename T>
struct Sentinel {
  bool operator==(T iter) const {
    return *iter < 0 || iter == end;
  }

  T end;
};

void Log(int x) {
  std::cout << x << ", ";
}

int main() {
  std::vector<int> numbers {1, 4, 3, 8, -2, 5};
  Sentinel<std::vector<int>::const_iterator> s{
    numbers.end()};

  auto [in, fun] = std::ranges::for_each(
    numbers.begin(), s, Log
  );

  std::cout << "\nObject at sentinel: " << *in;
  std::cout << "\nSize of range: "
    << std::distance(numbers.begin(), in);
  std::cout << "\nLast in range: " << *(in - 1);
}
1, 4, 3, 8,
Object at sentinel: -2
Size of range: 4
Last in range: 8

In this example, the std::ranges::for_each() algorithm processes the range and returns an object with the in member pointing to where the sentinel was triggered. By calculating the distance from the beginning of the range to this iterator, you can determine the size of the range.

This approach ensures that you accurately and efficiently determine the size of ranges defined by sentinels without needing to run additional algorithms.

Defining Ranges using Sentinels

An alternative way of defining ranges, and why we sometimes need to use them

Questions & Answers

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

What is a Sentinel in C++?
What is a sentinel in C++?
How to Create a Custom Sentinel in C++
How do you create a custom sentinel in C++?
Why Use Sentinels Instead of End Iterators in C++?
Why use sentinels instead of end iterators in C++?
Using Sentinels with Standard Algorithms in C++
How can sentinels be used with standard algorithms in C++?
How Do Template Sentinels Work in C++?
How do template sentinels work in C++?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant