There is no native function that does this because this comparison makes no sense as a general purpose resource. That is, you may have some need to make this comparison, but a vector has very broad use and an individual comparison of its numbers can be implemented with different nuances (more details at the end).
The function less
does not do what you want, although it can be used to help you. This function serves to compare two single values/objects. If you look at the example of the documentation you referenced, you will see the following calls:
int foo[]={10,20,5,15,25};
. . .
std::sort (foo, foo+5, std::less<int>()); // 5 10 15 20 25
That is, what the last line (where less
is being used) does is sort the elements in foo
in ascending order. The less
there is merely being used as the comparator. You could pass another existing comparator or or create one of your own to sort the list according to any other criteria, because the interface of the algorithm sort
is rightly created for such.
If your intention by "more direct way" is to use less code when making the comparison, the best way is to really overload the smaller comparison operator (operator<
).
I redid the code you posted in a full example (by the way, next time you create a question, try to provide the Minimum, Complete and Verifiable Example to make it easier for someone interested in helping you) to overload that operator. The code is below and can be executed as well in Ideone:
#include <vector>
#include <iostream>
#include <functional> // Necessário para usar o std::less
using namespace std;
// Sobrecarrega o operador de menor para a classe `vector`
bool operator<(const vector<int>& a, const vector<int>& b)
{
vector<int>::const_iterator aIt = a.begin();
vector<int>::const_iterator bIt = b.begin();
bool ret = true;
while(aIt != a.end() && bIt != b.end())
{
//if(*aIt >= *bIt) // <== Mais simples e mais claro. Portanto, melhor.
if(!less<int>()(*aIt, *bIt))
{
ret = false;
break;
}
++aIt;
++bIt;
}
return ret;
}
int main()
{
vector<int> v1 = {5, 3, 8};
vector<int> v2 = {4, 1, 6};
// Imprime os vetores para conferência
cout << "v1: ";
for (auto i: v1)
std::cout << i << ' ';
cout << endl;
cout << "v2: ";
for (auto i: v2)
std::cout << i << ' ';
cout << endl;
// Usa diretamente o operador para verificar se v2 é menor do que v1
cout << "v2 < v1? " << (v2 < v1 ? "SIM" : "NAO") << endl;
return 0;
}
Some concluding remarks:
- This code overloads
operator<
in the overall scope for the class std::vector
, so that you can do v1 < v2
for any vector of integers.
- Note the use of the function
less
in implementing this overload. It works, but is totally unnecessary, since it is more practical and straightforward to simply make the comparison yourself (commented line).
- In your original code you had a variable called
less
, whose name is the same as the function std::less
. Be very careful with that, because using the same name you run the risk of mixing the references (unless you keep the namespace every call std::less
instead of just less
- that is, without using the using namespace std
at first).
- Note how the overload implementation does not use a numeric index as in its original implementation, but rather two iterators (one for each vector
a
and b
). In your original code, if the vectors have different sizes, you would run the risk of having an invalid access while doing v2->at(j)
, since j
was increased according to the size of v1
.
My choice of implementation ensures that the error mentioned in the item
4 does not happen (because the loop ends when any of the
iterators reach the end). However, in cases where the vectors have
different sizes, the answer will be to compare only the part
initial of the larger vector, with the same amount of vector elements
minor. Why there at the beginning I said that this comparison does not
sense always. We don’t know what your problem is, nor if the vectors
will always have the same size. But in case of different sizes, it is worth
say that v2
is less than v1
even though v2
has been plus
elements of what v1
? No one but you can answer that
question.
There is nothing ready, there are several ways to do this, but it needs a clear criterion, it is not clear what to ask in the question.
– Maniero
@bigown the example I gave would be one of the ways to accomplish this if the criterion is "an element of v2 is less than an element of v1 for all vector positions", right? Also, by your comment, then there is nothing ready to accomplish something with that criterion, right?
– Lucas João
You have already used this phrase and it is not clear to define the problem you are trying to solve. You can interpret it in several ways.
– Maniero
@bigown I’m sorry, but I don’t know how to be clearer. I believe you managed to understand what the code of the question does, right?
– Lucas João
Maybe, but it could be the wrong solution if the problem is wrong.
– Maniero
@bigown I will try to explain again using the code in the question. In this code, considering that v1 and v2 have the values that are in the comments, after the execution of the loop the variable "Less" will have the true value, because for each possible index (the variable k) the value of v2 in this index is less than the value of v1 in this same index. So "being smaller" would be the criterion. But anyway, if you believe that it rained in the wet and I now repeated what I said before, no problem, because I already consider that you answered my question in your first comment saying that there is nothing ready to do this.
– Lucas João