By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

How the comparison operators are defined for iterator and const_iterator

P: n/a
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::operator == (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;
}

Oct 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Pe*******@gmail.com wrote:
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.


Just a loosely related note on any operator<= and >=; the STL works with
the two basic principles of equality and equivalence. Usually it
requires an ordering corresponding to one of those concepts. With
equality you can only implement == and !=, and with equivalence only <
and >. Therefore the combination operators <= and >= require both the
concepts of equality and equivalence, and so are difficult to implement.
--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Oct 23 '05 #2

P: n/a
Ferdi Smit wrote:
Pe*******@gmail.com wrote:

Just a loosely related note on any operator<= and >=; the STL works with
the two basic principles of equality and equivalence. Usually it
requires an ordering corresponding to one of those concepts. With
equality you can only implement == and !=, and with equivalence only <
and >. Therefore the combination operators <= and >= require both the
concepts of equality and equivalence, and so are difficult to implement.


Oh I messed something up, with equivalence you can ofcourse implement a
== b as a<b && b<a, and consequently also !=. However a <= b would
become a<b || (a<b && b<a) which is the same as a<b. That's what I meant.

--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Oct 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.