Comparing Only Part of Two Ranges

Can I use std::ranges::equal() to compare only a part of two ranges?

Yes, you can use std::ranges::equal() to compare only a part of two ranges by specifying subranges.

This can be done using iterator pairs or views to define the portions of the ranges you want to compare.

Here's an example that demonstrates comparing parts of two vectors:

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

int main() {
  std::vector<int> vec1{1, 2, 3, 4, 5};
  std::vector<int> vec2{10, 2, 3, 40, 50};

  auto subrange1 = std::ranges::subrange(
    vec1.begin() + 1, vec1.begin() + 3);  

  auto subrange2 = std::ranges::subrange(
    vec2.begin() + 1, vec2.begin() + 3);  

  if (std::ranges::equal(subrange1, subrange2)) {
    std::cout << "Subranges are equal\n";
  } else {
    std::cout << "Subranges are not equal\n";
  }
}
Subranges are equal

In this example, subrange1 and subrange2 represent parts of vec1 and vec2, respectively.

Specifically, they represent the elements from the second to the fourth element (inclusive).

The std::ranges::equal() function compares these subranges and prints the result.

Alternatively, you can use views to achieve the same result:

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

int main() {
  std::vector<int> vec1{1, 2, 3, 4, 5};
  std::vector<int> vec2{10, 2, 3, 40, 50};

  auto view1 = vec1
    | std::views::drop(1)  
    | std::views::take(2);      

  auto view2 = vec2
    | std::views::drop(1)  
    | std::views::take(2);      

  if (std::ranges::equal(view1, view2)) {
    std::cout << "Views are equal\n";
  } else {
    std::cout << "Views are not equal\n";
  }
}
Views are equal

In this example, std::views::drop(1) and std::views::take(3) are used to create views of the specified subranges.

The std::ranges::equal() function then compares these views.

Using subranges or views allows you to compare only specific parts of your ranges, making std::ranges::equal() a flexible tool for various comparison needs.

8 Key Standard Library Algorithms

An introduction to 8 more useful algorithms from the standard library, and how we can use them alongside views, projections, and other techniques

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::for_each() with a Member Function
How do I use std::ranges::for_each() with a member function of a class?
Using std::iota() with a Custom Step Size
How can I use std::ranges::iota() to generate a sequence with a custom step size?
Merging Three or More Sorted Ranges
How do I merge three or more sorted ranges using std::ranges::merge()?
Ensuring No Repeated Elements in std::ranges::sample()
Can I use std::ranges::sample() to ensure no repeated elements in the sample?
Removing Duplicates from a Range
What is the most efficient way to remove duplicates from a range using standard algorithms?
Using Subranges vs Iterator Pairs
What are the use cases for using std::ranges::subrange() versus standard iterator pairs?
std::ranges::set_union() vs std::ranges::merge()
What are the benefits of using std::ranges::set_union() over std::ranges::merge() in terms of performance and use cases?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant