How the comparison operators are defined for iterator and const_iterator

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::eek: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;
}
 
F

Ferdi Smit

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: (e-mail address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 
F

Ferdi Smit

Ferdi said:
(e-mail address removed) 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: (e-mail address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top