Created
September 11, 2021 03:21
-
-
Save insom/137c75a1067327d4379e1af2e92850d9 to your computer and use it in GitHub Desktop.
Kind of sort two products in C++
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
#include <algorithm> | |
#include <functional> | |
#include <iostream> | |
#include <vector> | |
#include <utility> | |
#include <string> | |
using namespace std; | |
typedef pair<string,int> product; | |
class sorter : binary_function<product,product,bool> { | |
public: | |
result_type operator()(first_argument_type v1, second_argument_type v2) { | |
return v2.second > v1.second; | |
} | |
}; | |
int main() { | |
product p1 = make_pair(string("Foo"), 10); | |
product p2 = make_pair(string("Fool"), 5); | |
vector<product> products = vector<product>(2); | |
products[0] = p1; | |
products[1] = p2; | |
sort(products.begin(), products.end(), sorter()); | |
sorter s = sorter(); | |
cout << "Hello "; | |
cout << products[0].first; | |
cout << s(p2, p1); | |
cout << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment