There is no native function to do this because this comparison does not make sense as a general-purpose resource . That is, you may have some need to make that comparison, but a vector has very broad usage and an individual comparison of its numbers can be implemented with different nuances (more details at the end).
The less
function does not do what you want, although it can be used to help you. This function is used to compare two unique 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% w / o% therein is merely being used as the comparator . You could pass another existing comparer or create one to sort the list according to any other criteria, because the less
algorithm interface is just created.
If your intention is to "use less code" when comparing, the best way is to actually overload the lower comparison operator ( sort
).
I've refactored the code you posted in a complete example (by the way, the next time you create a question, try to provide Minimum, Full and Verifiable example to make it easier for someone to help you) that overloads this operator. The code is below and can also be run on 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 final remarks:
This code overloads operator<
in global scope for class operator<
, so you can std::vector
for any integer vector.
Note the use of the v1 < v2
function in the implementation of this overload. It works, but it's totally unnecessary, since it's more practical and straightforward to simply do the comparison yourself (commented line).
In your original code you had a variable called less
, whose name is the same as the less
function. Be very careful with this , because when you use the same name you run the risk of mixing the references (unless you keep the namespace for each call of std::less
instead of just std::less
- that is, without using less
at the beginning).
Note how the overhead implementation does not use a numeric index as in its original implementation, but rather two iterators (one for each vector using namespace std
and a
). In your original code, if the vectors were different sizes, you ran the risk of having invalid access when doing b
, since v2->at(j)
was incremented by the size of j
.
My choice of implementation ensures that the error mentioned in item
4 does not happen (because the loop ends when any of the
iterators arrive at the end). However, in cases where the vectors have
different sizes, the response given will be the comparison only of the
of the largest vector, with the same amount of vector elements
smaller. That's why at the beginning I said that this comparison does not
always felt. We do not know what your problem is, nor if the vectors
they will always be the same size. But in the case of different sizes, it's worth
say v1
is less than v2
even though v1
has more
elements than v2
? No one but you can respond to this
question.