Skip to content

Instantly share code, notes, and snippets.

@insom
Created September 11, 2021 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insom/137c75a1067327d4379e1af2e92850d9 to your computer and use it in GitHub Desktop.
Save insom/137c75a1067327d4379e1af2e92850d9 to your computer and use it in GitHub Desktop.
Kind of sort two products in C++
#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