Created
April 21, 2015 17:01
-
-
Save sheva29/925c0fc7ab8f595d6b08 to your computer and use it in GitHub Desktop.
Comparing pairs and sorting them in ascending order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This template allows to compare pairs, and return them in ascending order. | |
template <class T1, class T2, class Pred = std::less<T2> > | |
struct sort_pair_second { | |
bool operator()(const std::pair<T1,T2>&left, const std::pair<T1,T2>&right) { | |
Pred p; | |
return p(right.second, left.second); | |
} | |
}; | |
//Implementation example | |
// _noteProducts is an object that contains all the product available. | |
for( int i = 0, j = 0; i < _notesProducts.size(); i++){// we assign the name of the products and the number of listed notes. | |
if ( genderAnswer == _notesProducts[i].gender && _notesProducts[i].listedNotes.size() > 0){// we make sure that we only pass the elements that have the right gender and more than one listed one. | |
pair<Products, int> tmp; | |
productListedNotes.push_back(tmp);// this is a vector of pairs where we pass the pairs we need only. | |
productListedNotes[j] = make_pair (_notesProducts[i], _notesProducts[i].listedNotes.size()); | |
j++;// we manage our own index for the pair. | |
} | |
} | |
// we finally sort from higher to lowest number of listedNotes per fragrance. | |
sort(productListedNotes.begin(), productListedNotes.end(), sort_pair_second<Products, int>());// we organize our results based the higher number of notes included in our perfumes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment