473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How the comparison operators are defined for iterator and const_iterator

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
2 2031
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the...
8
by: Alexander Stippler | last post by:
Hello, the standard requires a type reverse_iterator. But in the form it is required and implemented for all compilers I know, there is IMO something missing, which is essential for its usage....
5
by: John Harrison | last post by:
I there a reliable and generic method to convert a const_iterator to an iterator (i.e. something like const_cast). I ask because I'm writing some methods which take and return iterators. A const...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
6
by: Evyn | last post by:
Hi, How do I compare 2 maps for identical keys, and if they have identical keys, check if they have identical values? In either case I want to copy ONLY that key value pair to one of two...
6
by: Rares Vernica | last post by:
Hi, I am using tr1/unordered_map in my code. My code compiled ok on g++ (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu1). Now I need to compile my code on g++ (GCC) 4.0.3 (Ubuntu 4.0.3-1ubuntu5). I get...
3
by: gallows | last post by:
The container is: template <typename T> class Container { public: // container methods.. // iterator: class const_iterator { public:
6
by: Pallav singh | last post by:
Hi when we should have Class defined Inside a Class ? can any one give me explanation for it ? Does it is used to Hide some information of Class Data-Member and Function from friend class? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.