473,414 Members | 1,988 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,414 software developers and data experts.

custom iterator

Hi,

I'm implementing a custom iterator (random access type ) so I can use
stl algorithms such as sort on my legacy containers. I'm having
problems compiling however. when implementing my customIterator class I
have defined the operators below (as well as others but they're not
important right now, I don't think).

class customIterator
{

customerIterator& operator*() const;
int operator-(const customIterator& x);
int operator-(const contIter& rhs) const
customIterator& operator++();
customIterator operator++(int);
customIterator& operator--();
customIterator operator--(int);
customIterator& operator+=(difference_type n);
customIterator& operator-=(difference_type n);
customIterator operator+(difference_type rhs) const;
customIterator operator-(difference_type rhs) const;
int& operator[](int n);
bool operator==(const customIterator& x) const;
bool operator<(const customIterator& x) const;

}
The problem is when I come to pass one of these iterators into
std::sort, I'm missing all sorts of operators like operator>(int),
operator/(int), operator-(int).

These are being generated because the stl::sort on my platform has code
like this;

while (__last - __first > __stl_threshold) {

or uses expressions like;

if ( (__last - __first)/2 > something... )

Do I need to provide an operator for implicit conversion of my
customIterator to an int so that the comparison ( __last - first >
__stl_threshold) can be evaluated?

Right now I'm just trying to get this to compile with a simple case
vectors of ints.
std::vector<int> vint;

containerWrapper wrapper(vint);
customIterator begin = wrapper.begin();
customIterator end = wrapper.end();
// compilation errors here .......
std::sort(begin, end, aCompareFunction());

compilation errors are like;

1) operator> not implemented in the type customIterator for arguments
of type int

2) operator== not implemented in the type customIterator for arguments
of type int

3) operator/ not implemented in the type customIterator for arguments
of type int
etc. etc.

any ideas?

thanks.

Sep 12 '05 #1
13 4901
"Gr*****@nospam.com" <Gr**********@gmail.com> schrieb im Newsbeitrag
news:11*********************@g49g2000cwa.googlegro ups.com...
[...]
class customIterator
{ [...] } [...] // compilation errors here .......
std::sort(begin, end, aCompareFunction());
compilation errors are like;
1) operator> not implemented in the type customIterator for arguments
of type int
2) operator== not implemented in the type customIterator for arguments
of type int
3) operator/ not implemented in the type customIterator for arguments
of type int


you can define the missed operators globally, ie

bool operator>(const customIterator&i1, const customIterator&i2) { return
DistanceOf(i1, i2) > 0; }
bool operator==(const customIterator&i1, const customIterator&i2) { return
DistanceOf(i1, i2) == 0; }
....
int DistanceOf(const customIterator&i1, const customIterator&i2)
{
// calculate the distance of your iterators, which may depend on the inner
definition of the iterator
}

Hope this helps. Any questions?

// oliver
Sep 12 '05 #2
* Gr*****@nospam.com:
I'm implementing a custom iterator (random access type ) so I can use
stl algorithms such as sort on my legacy containers. I'm having
problems compiling however. when implementing my customIterator class I
have defined the operators below (as well as others but they're not
important right now, I don't think).

class customIterator
{

customerIterator& operator*() const;
int operator-(const customIterator& x);
int operator-(const contIter& rhs) const
customIterator& operator++();
customIterator operator++(int);
customIterator& operator--();
customIterator operator--(int);
customIterator& operator+=(difference_type n);
customIterator& operator-=(difference_type n);
customIterator operator+(difference_type rhs) const;
customIterator operator-(difference_type rhs) const;
int& operator[](int n);
bool operator==(const customIterator& x) const;
bool operator<(const customIterator& x) const;

}
The problem is when I come to pass one of these iterators into
std::sort, I'm missing all sorts of operators like operator>(int),
operator/(int), operator-(int).

These are being generated because the stl::sort on my platform has code
like this;

while (__last - __first > __stl_threshold) {

or uses expressions like;

if ( (__last - __first)/2 > something... )
The requirements on a random access iterator are, roughly, that it behaves
like a pointer.

That includes comparision and subtraction, but not division.

Do I need to provide an operator for implicit conversion of my
customIterator to an int so that the comparison ( __last - first >
__stl_threshold) can be evaluated?
No, you need to define

std::ptr_diff operator-( MyIter const& a, MyIter const& b )

Right now I'm just trying to get this to compile with a simple case
vectors of ints.
std::vector<int> vint;

containerWrapper wrapper(vint);
customIterator begin = wrapper.begin();
customIterator end = wrapper.end();
// compilation errors here .......
std::sort(begin, end, aCompareFunction());

compilation errors are like;

1) operator> not implemented in the type customIterator for arguments
of type int

2) operator== not implemented in the type customIterator for arguments
of type int

3) operator/ not implemented in the type customIterator for arguments
of type int


I rather doubt your point (3).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 12 '05 #3

"Alf P. Steinbach" <al***@start.no> wrote in message
news:43****************@news.individual.net...
* Gr*****@nospam.com:
I'm implementing a custom iterator (random access type ) so I can use
stl algorithms such as sort on my legacy containers. I'm having
problems compiling however. when implementing my customIterator class I
have defined the operators below (as well as others but they're not
important right now, I don't think).

[snip]
Do I need to provide an operator for implicit conversion of my
customIterator to an int so that the comparison ( __last - first >
__stl_threshold) can be evaluated?


No, you need to define

std::ptr_diff operator-( MyIter const& a, MyIter const& b )


Did I read that right? Define something in namespace 'std'?
Is that allowed by the standard?

-Mike
Sep 12 '05 #4
Mike Wahler wrote:
"Alf P. Steinbach" <al***@start.no> wrote in message
> No, you need to define


std::ptr_diff operator-( MyIter const& a, MyIter const& b )


Did I read that right? Define something in namespace 'std'?
Is that allowed by the standard?


I don't think he was suggesting defining something in namespace std
(although I believe even that is allowed in limited circumstances
relating to specializing templates already in namespace std). He's
just suggesting defining a function that returns a std::ptr_diff.
Would you be complaining if his function returned a std::size_t?

Best regards,

Tom

Sep 12 '05 #5
Gr*****@nospam.com wrote:
Hi,

I'm implementing a custom iterator (random access type ) so I can use
stl algorithms such as sort on my legacy containers. I'm having
problems compiling however. when implementing my customIterator class I
have defined the operators below (as well as others but they're not
important right now, I don't think).

class customIterator
{

customerIterator& operator*() const;
int operator-(const customIterator& x);
int operator-(const contIter& rhs) const
customIterator& operator++();
customIterator operator++(int);
customIterator& operator--();
customIterator operator--(int);
customIterator& operator+=(difference_type n);
customIterator& operator-=(difference_type n);
customIterator operator+(difference_type rhs) const;
customIterator operator-(difference_type rhs) const;
int& operator[](int n);
bool operator==(const customIterator& x) const;
bool operator<(const customIterator& x) const;

}
The problem is when I come to pass one of these iterators into
std::sort, I'm missing all sorts of operators like operator>(int),
operator/(int), operator-(int).

These are being generated because the stl::sort on my platform has code
like this;

while (__last - __first > __stl_threshold) {

or uses expressions like;

if ( (__last - __first)/2 > something... )

Do I need to provide an operator for implicit conversion of my
customIterator to an int so that the comparison ( __last - first >
__stl_threshold) can be evaluated?
When you subtract one iterator from another the result is an integer.
The > and the / above are integer operations because __last - __first is
an integer.

The question is why this isn't working when you seem to have declared
the correct operator- in the code above
int operator-(const customIterator& x);
int operator-(const contIter& rhs) const


Or maybe you haven't, what is contIter, and why did you feel the need to
provide a const and non-const version of operator- ?

Anyway that is the area where the problem lies.

john
Sep 12 '05 #6
thanks for the replies, they were all helpful.

I've a question about iterator behaviour. if I have

difference_type res = iter1 - iter2

the SGI website says that the precondition is that iter1 is "reachable
from iter2 or vice-versa, or both".
that includes past-the-end iterators too, doesn't it, for either iter1
or iter2. I mean if there are 10 iterms in a vector and I have iter1
and iter2 such that iter1 points to vector[3] and iter2 points to
past-the-end , then iter2-iter1 is still valid, isn't it? it's going to
return an iterator thats pointing to element[7] in the vector. yes?

I'm just trying to get the logic for all the right so that my iterator
respects all their behavioural requirements. I'm using the SGI website
for the moment as a reference, if there's any other suggested sites
that cover iterators in this context, then I'll definitely check it
out.

have a nice day

GrahamO

Sep 13 '05 #7
On 2005-09-13 04:42:05 -0400, "Gr*****@nospam.com"
<Gr**********@gmail.com> said:
thanks for the replies, they were all helpful.

I've a question about iterator behaviour. if I have

difference_type res = iter1 - iter2

the SGI website says that the precondition is that iter1 is "reachable
from iter2 or vice-versa, or both".
that includes past-the-end iterators too, doesn't it, for either iter1
or iter2. I mean if there are 10 iterms in a vector and I have iter1
and iter2 such that iter1 points to vector[3] and iter2 points to
past-the-end , then iter2-iter1 is still valid, isn't it?
Yes, so far.
it's going to return an iterator thats pointing to element[7] in the
vector. yes?


No, it will not return an iterator at all, it will return the value 7.

Remember that arithmetic on random access iterators (i.e. the kind of
iterator that std::vector has) works in essentially the same fashion as
arithmetic on pointers.
--
Clark S. Cox, III
cl*******@gmail.com

Sep 13 '05 #8

"Thomas Tutone" <Th***********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Mike Wahler wrote:
"Alf P. Steinbach" <al***@start.no> wrote in message
> No, you need to define
>
> std::ptr_diff operator-( MyIter const& a, MyIter const& b )
Did I read that right? Define something in namespace 'std'?
Is that allowed by the standard?


I don't think he was suggesting defining something in namespace std
(although I believe even that is allowed in limited circumstances
relating to specializing templates already in namespace std). He's
just suggesting defining a function that returns a std::ptr_diff.


Yes you're right, somehow I interpreted that code incorrectly.
But I believe the type is 'std::ptrdiff_t'
Would you be complaining if his function returned a std::size_t?


Um, if I haven't had my coffee yet. :-)

-Mike
Sep 13 '05 #9
one other thing... I have the iterators setup and I can now perform
things like;

myIter i1 = wrapper.begin();
myIter i2 = wrapper.end();

std::swap(i1, i2 );

I can also call std::sort(i1 , i2);

the above swap works fine and when I output the container contents, its
contents are correctly ordered.

however if I try to do something like;;

std::sort(i1, i2 -1 );

the (borland) compiler complains that;

[C++ Error] iterator.cpp(285): E2285 Could not find a match for
'swap<_Tp,_Alloc>(myIter,myIter)'

I have defined operator- as

// difference
int operator-(const contIter& rhs) const
{
return abs(m_index - rhs.m_index);
}

contIter operator-(int i) const
{
return contIter(m_container, m_index-i);
}


I\m obviously missing something but can't see what. any ideas?
thanks much again

GrahamO

Sep 13 '05 #10
* Mike Wahler:
But I believe the type is 'std::ptrdiff_t'


Typo, thanks.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 13 '05 #11
Gr*****@nospam.com wrote:
thanks for the replies, they were all helpful.

I've a question about iterator behaviour. if I have

difference_type res = iter1 - iter2

the SGI website says that the precondition is that iter1 is "reachable
from iter2 or vice-versa, or both".
that includes past-the-end iterators too, doesn't it, for either iter1
or iter2. I mean if there are 10 iterms in a vector and I have iter1
and iter2 such that iter1 points to vector[3] and iter2 points to
past-the-end , then iter2-iter1 is still valid, isn't it? it's going to
return an iterator thats pointing to element[7] in the vector. yes?


This is clearly a mental block so I'll say it loud.

THE RETURN VALUE WHEN YOU SUBTRACT TWO ITERATORS IS AN

***INTEGER***

Got that? Once you have everything else will fall into place.

john
Sep 14 '05 #12
Gr*****@nospam.com wrote:
one other thing... I have the iterators setup and I can now perform
things like;

myIter i1 = wrapper.begin();
myIter i2 = wrapper.end();

std::swap(i1, i2 );

I can also call std::sort(i1 , i2);

the above swap works fine and when I output the container contents, its
contents are correctly ordered.

however if I try to do something like;;

std::sort(i1, i2 -1 );

the (borland) compiler complains that;

[C++ Error] iterator.cpp(285): E2285 Could not find a match for
'swap<_Tp,_Alloc>(myIter,myIter)'
Can't see what the problem is there, how about posting line 285 from
iterator.cpp

I have defined operator- as

// difference
int operator-(const contIter& rhs) const
{
return abs(m_index - rhs.m_index);
}
Are you sure that's right? Surely should be

int operator-(const contIter& rhs) const
{
return m_index - rhs.m_index;
}

Your code will always return a positive value, what if rhs is further
advanced than *this?

contIter operator-(int i) const
{
return contIter(m_container, m_index-i);
}


I\m obviously missing something but can't see what. any ideas?
thanks much again

GrahamO


john
Sep 14 '05 #13
thanks Clarke/John,

I've cleared up the operator- business now (combination of typos and
errors on my behalf), thanks for your help.

I have painted "THE RETURN VALUE WHEN YOU SUBTRACT TWO ITERATORS IS AN
***INTEGER*** " onto the ceiling of my bedroom wall .. well not quite
but I've commited it to memory at least :)

Likewise the abs problem is corrected.

Everything is working nicely now, I can std::swap, std::sort, etc.
using my custom iterators.

thanks for the replies. G

Sep 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris Schadl | last post by:
Hi, I've written a simple sorted linked list class that I'm trying to implement an iterator for. I'm trying to make the interface to the iterator simmilar to the STL's iterator interface, so...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
8
by: Mateusz Łoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
1
by: Siegfried Heintze | last post by:
What is the minimum I must type to create a custom iterator that will allow me display my iterator on std::cout using std::copy? Thanks, Siegfried
3
by: joe | last post by:
I have written a custom std allocator which follows the example in Stroustrup's book. I'm seeing a behavior I don't understand. My allocate method within the allocator looks like the...
7
by: Max Odendahl | last post by:
Hi, my own declared class has a stl::list as a member variable. I now need access to those from the outside. Is a custom iterator for my class the best solution for this and how to do this? I...
16
by: arnaudk | last post by:
I'm trying to design a simple container class for some data of different types based on a vector of structs, but the vector and struct are protected so that the implemenation of my container class...
5
by: Kenneth Porter | last post by:
Is there a stylistically preferred way to apply an algorithm to a vector of vectors? For example, suppose I have: typedef vector< vector<int Array2D; Array2D array2D; I want to find the...
5
by: maverik | last post by:
Hi all. I'm implementing class AbstractCollection - just a wrap on std::vector - to hide from clients the fact of using std::vector in the base (because in the future I may change vector to a...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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 projectplanning, 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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.