set comparator c++ code example
Example 1: c++ custom comparator for elements in set
struct compare {
bool operator() (const int& x, const int& y) const {
return x<y; // if x<y then x will come before y. Change this condition as per requirement
}
};
int main()
{
set<int,compare> s; //use the comparator like this
}
Example 2: reverse iterator c++
// A reverse_iterator example using vectors
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::vector<int>::reverse_iterator r_iter;
// rbegin() points to the end of the vector, and rend()
// points to the front. Use crbegin() and crend() for
// the const versions of these interators.
for (r_iter = vec.rbegin(); r_iter != vec.rend(); r_iter++) {
std::cout << *r_iter << std::endl;
}
return 0;
}