Defining Ranges using Sentinels

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?

Abstract art representing computer programming

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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved