我有以下代码,它在编译时给了我错误。问题发生在我在重载的插入运算符(即)运算符<<()函数中创建一个 const_iterator 的地方。我猜 set 的模板参数版本不正确。任何人都可以请投入并使这段代码工作吗?
#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;
}