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?

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.

This Question is from the Lesson:

### Defining Ranges using Sentinels

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

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

This Question is from the Lesson:

### Defining Ranges using Sentinels

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

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:

• 124 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.