I was going through the stl::key_comp() function and found it's implementation to be pretty straight forward when it came implementation on integers.
For eg:
would give out the result [0,1,2,3,4,5] on comparing each of the integers in the set.
I wanted to try the same on a higher-level data straucture such as a structure of type:
but had some problems. It gives me an error whenever i want to insert a set element, as the default comparison function does not allow it.
So, how do I use key_comp() and value_comp() to define our own comparison function??
For eg:
Code:
#include <iostream> #include <set> using namespace std; int main () { set<int> myset; set<int>::key_compare mycomp; set<int>::iterator it; int i,highest; mycomp = myset.key_comp(); for (i=0; i<=5; i++) myset.insert(i); cout << "myset contains:"; highest=*myset.rbegin(); it=myset.begin(); do { cout << " " << *it; } while ( mycomp(*it++,highest) ); cout << endl; return 0; }
I wanted to try the same on a higher-level data straucture such as a structure of type:
Code:
struct node { int a,b; };
So, how do I use key_comp() and value_comp() to define our own comparison function??
Comment