2

If I want to compare two vectors in lexicographical order, I can do as follows:

int main() {
    std::vector<int> a{0, 7, 8, 9};
    std::vector<int> b{1, 2, 3, 4};

    std::cout << std::boolalpha;
    std::cout << "a < b returns " << (a < b) << '\n';
}

But doing the same in reverse order fails to compile:

int main() {
    std::vector<int> a{3, 2, 1};
    std::vector<int> b{9, 8, 7, 6};

    std::cout << std::boolalpha;
    std::cout << "revrese a < reverse b returns " << ((a | std::views::reverse) < (b | std::views::reverse)) << '\n';
}

The latter code fails with:

<source>:23:81: error: no match for 'operator<' (operand types are 'std::ranges::reverse_view<std::ranges::ref_view<std::vector<int> > >' and 'std::ranges::reverse_view<std::ranges::ref_view<std::vector<int> > >')
   23 |     std::cout << "reverse a < reverse b returns " << ((a | std::views::reverse) < (b | std::views::reverse)) << '\n';
      |                                                       ~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                          |                           |
      |                                                          reverse_view<[...]>         reverse_view<[...]>

So, how to do achieve this properly?

I needed this because, I was creating a custom big integer class. In that, I was storing nodes in little endian order. Now, to compare if two integers are equal, first I compare their sizes. Then I would need to compare equal sized vectors in lexicographical order of their reverse view.

11
  • 2
    @kiner_shah yes Commented Feb 17, 2023 at 7:57
  • 1
    Does this answer your question? Is there a standard way to compare two ranges in C++? Commented Feb 17, 2023 at 8:02
  • 3
    @kiner_shah OP wants to compare the reversed vectors via <, the question you propose as dupe is about equality, where reversed or not does not matter Commented Feb 17, 2023 at 8:06
  • 3
    @kiner_shah the fact that there is an answer that is slightly related does not make the questions a duplicate. Consider that the answer you link could be deleted or modified to still match the question asked there but to not match the question asked here. That question is about comparing for eqaulity, this question is about comparing < reversed. Commented Feb 17, 2023 at 8:09
  • 2
    @kiner_shah i mean that == / equality and lexicographical comparison / < are totally different. The question you link is about the former, the question here is about the latter. The answers on the question you link focus on equality because thats what is being asked for there, but not here. Commented Feb 17, 2023 at 8:14

2 Answers 2

7

operator< is not defined for std::views::reverse and others. There is however a normal algorithm as normal function template for it in the standard library: std::lexicographical_compare with iterator interface and std::ranges::lexicographical_compare with range interface.

std::cout << "revrese a < reverse b returns "
          << std::ranges::lexicographical_compare(a | std::views::reverse, b | std::views::reverse) 
          << '\n';
Sign up to request clarification or add additional context in comments.

1 Comment

For future reference, I'm using this solution, with slight modification as in shown in this.
1

As @user17732522 said you can use std::ranges::lexicographical_compare && std::views::reverse but if you want use c++11 you can simply use reverse iterators with std::lexicographical_compare.

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

int main() {
  std::vector<int> a{0, 7, 8, 9};
  std::vector<int> b{1, 2, 3, 4};

  std::cout << std::boolalpha;
  std::cout << "a < b returns "
            << std::lexicographical_compare(a.cbegin(), a.cend(), b.rbegin(),
                                            b.rend())
            << '\n';
}

1 Comment

Why do you use cbegin/cend for vector a? Is it a misprint and rbegin/rend were expected?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.