I have this following code which gives me error when compiling. The problem is happening at the point where I create a const_iterator inside the overloaded insertion operator (i.e) operator << () function. The template argument version of set is not correct I guess. Could anyone please pitch in and make this code working?
#include <iostream>
#include <set>
using namespace std;
class A {
public:
int _i;
A(int i) : _i(i) {}
bool operator < (A a) const { return _i < a._i; }
};
class compA {
public:
bool operator()(A a1, A a2) const { return (a1._i > a2._i); }
};
template<class T, class C>
std::ostream& operator << (std::ostream& os, const set<T, C>& s) {
std::set<T, C>::const_iterator itr = s.begin();
for (int i = 0; i < s.size(); i++) {
os << *itr++ << " ";
}
return os;
}
int main() {
set<A, less<A> > s1;
set<A, compA> s2;
s1.insert(A(9)); s1.insert(A(3)); s1.insert(A(-5)); s1.insert(A(3));
s2.insert(A(2)); s2.insert(A(1)); s2.insert(A(7));
cout << s1;
cout << s2;
}