Apr 1, 2015 at 11:39pm UTC
I have a vector of pairs which i need to sort through both first and second element.
I sort them using this function:
bool f1(pair <int,char> a, pair <int,char> b)
{
return (a.second < b.second) || (a.first < b.first);
}
sort(v.begin(),v.end(),f1);
and when i enter
1A 2A 1B 3A 4B 2B 3B 4A where the first element of the pair is the letter and second is the number
it couts: 1A 1B 2A 2B 3A 4A 3B 4B which really doesnt make sense
it should cout: 1A 2A 3A 4A 1B 2B 3B 4B
Any help is appriciated!
Last edited on Apr 1, 2015 at 11:39pm UTC
Apr 1, 2015 at 11:54pm UTC
Your condition is not comprehensive enough. What if (a.second > b.second)? (Your current code treats it the same as if (a.second == b.second).)
Hope this helps.
Apr 2, 2015 at 12:19am UTC
@Duoas
thanks man and okay, guess what ur saying is if either the case of a.second>b.second or a.second==b.second happens it will just pass onto the next condition, right?
how do i fix it, how do i make it more "comprehensive", keeping in mind i want my bool function this simple? haha
Last edited on Apr 2, 2015 at 12:30am UTC
Apr 2, 2015 at 11:10pm UTC
Remember, if (a.second < b.second), return true.
Else if (a.second == b.second) and (a.first < b.first) return true.
Else return false.
Good luck!