-3

I am in need of ordering a list of numbers stored in a C++ vector:

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


struct A
{
};

bool compare(A i1, A i2) 
{ 
    if(i1.vec.size() > i2.vec.size())
        return true;; 
}  

int main()
{
std::vector<A> ax;
ax.emplace_back(A{ 100, 300, 6, 123, 12, 451, 552});
ax.emplace_back(A{ 100, 300, 6, 123, 12, 451});


std::sort(ax.begin(), ax.end(), compare); 
return 0;
}

As per the above code the ordering of ax vector should be:

{ 100, 300, 6, 123, 12, 451}
{ 100, 300, 6, 123, 12, 451, 552}

I am unable to get what needs to be added in compare function to achieve it?

Programmer
  • 8,303
  • 23
  • 78
  • 162

1 Answers1

1

your comparison is not based on the content of the vector but on their size

furthermore you pass Oid by value rather than by const reference in compare, this is expensive for nothing because they are copied each time

bruno
  • 32,421
  • 7
  • 25
  • 37