@specsoftdev live:.cid.8e17e9b93cabb607 specsoftdev@gmail.com
std::ranges::equal
Актуально для C++26.

#include <algorithm>
Актуально на 2025-06-02.


Define overload #1
template<std::input_iterator I1, std::sentinel_for<I1> S1,
          std::input_iterator I2, std::sentinel_for<I2> S2,
          class Pred = ranges::equal_to,
          class Proj1 = std::identity,
          class Proj2 = std::identity>
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool
    equal(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
           Proj1 proj1 = {}, Proj2 proj2 = {});

Сравнивает диапазон [first1, last1] с диапазоном [first2, last2].
Example, possible implementation
Define overload #2
template<ranges::input_range R1, ranges::input_range R2,
          class Pred = ranges::equal_to,
          class Proj1 = std::identity,
          class Proj2 = std::identity>
requires std::indirectly_comparable<ranges::iterator_t<R1>,
                                    ranges::iterator_t<R2>,
                                    Pred, Proj1, Proj2>
constexpr bool
    equal(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {});

Сравнивает диапазон "r1" с диапазоном "r2".
Example, possible implementation

Notes

  • В целях оптимизации производительности, сначала сравнивается длина диапазонов, если это возможно.
  • Сравнение диапазонов осуществляется с начала.



Возвращаемое значение

Вернёт true если они равны, иначе, false.




Сложность

Линейная =  O(n), n = min(last1 - first1, last2 - first2).



Examples


Example 1:
#include <iostream>
#include <algorithm>
#include <string>
namespace ranges = std::ranges;

int main()
{
    std::string str1{"ABCDEFGHIJK"};
    std::string str2{"abcdefghijk"};
    auto pred = [](unsigned char c) {
        return std::tolower(c);
    };
    auto result = ranges::equal(str1, str2,
                                ranges::equal_to{},
                                pred);
    if (result)
    {
        std::cout << "range is equivalent" << std::endl;
    }
    else
    {
        std::cout << "range is not equivalent" << std::endl;
    }
}



range is equivalent






Changelog

C++20
Введён в стандарт.


See also

TODO

This page was last modified on 2025-06-02