P
PengYu.UT
I'm wonder whether
1. stl directly defined the 6 comparison operators(== != < > <= >=)
directly for iterator and const_iterator
2. or it only define == and < and using std::rel_ops to get the other 4
operators.
I made the following example for case 1. But I can not make it work.
Could you please help me to make it work.
Thanks,
Peng
#include <utility>
#include <iostream>
#include <vector>
using namespace std::rel_ops;
class const_test;
class test{
public:
test(int i) : _i(i){}
bool operator == (const const_test& t) const;
friend class const_test;
private:
int _i;
};
class const_test{
public:
const_test(const test &t);
const_test(int i) : _i(i){}
bool operator == (const const_test& t) const {
return _i == t._i;
}
friend class test;
private:
int _i;
};
bool test:
perator == (const const_test& t) const {
return _i == t._i;
}
const_test::const_test(const test &t) : _i(t._i){
}
int main(){
test t1(1);
const_test t2(2);
std::cout << (t1 == t2) << std::endl;
std::cout << (t1 != t2) << std::endl;//error
std::vector<int>::iterator it1;
std::vector<int>::const_iterator it2;
std::cout << (it1 == it2) << std::endl;
std::cout << (it1 != it2) << std::endl;
}
1. stl directly defined the 6 comparison operators(== != < > <= >=)
directly for iterator and const_iterator
2. or it only define == and < and using std::rel_ops to get the other 4
operators.
I made the following example for case 1. But I can not make it work.
Could you please help me to make it work.
Thanks,
Peng
#include <utility>
#include <iostream>
#include <vector>
using namespace std::rel_ops;
class const_test;
class test{
public:
test(int i) : _i(i){}
bool operator == (const const_test& t) const;
friend class const_test;
private:
int _i;
};
class const_test{
public:
const_test(const test &t);
const_test(int i) : _i(i){}
bool operator == (const const_test& t) const {
return _i == t._i;
}
friend class test;
private:
int _i;
};
bool test:
return _i == t._i;
}
const_test::const_test(const test &t) : _i(t._i){
}
int main(){
test t1(1);
const_test t2(2);
std::cout << (t1 == t2) << std::endl;
std::cout << (t1 != t2) << std::endl;//error
std::vector<int>::iterator it1;
std::vector<int>::const_iterator it2;
std::cout << (it1 == it2) << std::endl;
std::cout << (it1 != it2) << std::endl;
}