[C++] is the behaviour of Iterators the same as pointers?

Z

ZikO

Hi

Are iterators' behaviour the same as pointers? I mean I know i can go
through the container with the same manner as if i use pointers (ptr++)
and i can derefference iterators the same way.

But

Can I add an integer number to them and skip a few elements like I would
use pointers (ptr + 4 etc.) ?

Regards
 
B

Bo Persson

ZikO said:
Hi

Are iterators' behaviour the same as pointers? I mean I know i can
go through the container with the same manner as if i use pointers
(ptr++) and i can derefference iterators the same way.

But

Can I add an integer number to them and skip a few elements like I
would use pointers (ptr + 4 etc.) ?

It's actually the other way round - a pointer is a kind of random
access iterator.

There are different kinds of iterators, you can add an offset to a
random access iterator, but not to others.



Bo Persson
 
Z

ZikO

ZikO said:
Hi

Are iterators' behaviour the same as pointers? I mean I know i can go
through the container with the same manner as if i use pointers (ptr++)
and i can derefference iterators the same way.

But

Can I add an integer number to them and skip a few elements like I would
use pointers (ptr + 4 etc.) ?

Regards

thanks a lot =)
 
Z

ZikO

Hi again

Hmm i thought I have understood iterators but there's still something
wrong :///

I have the code where and I am trying to remove element at 2nd position
so I am doing this:

Code:
typedef multimap<int, string> MMap;
typedef multimap<int, string>::iterator MMapIter;
typedef pair<int, string> PairMMap;

MMap m_map;

int index = 2
MMapIter it = m_map.begin();
for(int i=0; i<index; i++) it++;
m_map.erase((*it).first);

Unfortunately it = it + 1 is not working and this way in the code
compiler idicates iterator is out of range :// . I have 2 items (pairs
of key and string) in multimap when i am trying to do so. Can someone
tell me why this code is not working please?

Thanks
 
O

Obnoxious User

Hi again

Hmm i thought I have understood iterators but there's still something
wrong :///

I have the code where and I am trying to remove element at 2nd position
so I am doing this:

Code:
typedef multimap<int, string> MMap;
typedef multimap<int, string>::iterator MMapIter; typedef pair<int,
string> PairMMap;

MMap m_map;

int index = 2
MMapIter it = m_map.begin();[/QUOTE]

'it' now points to the first item in 'm_map'.
But unfortunately you never insert anything into it,
so at this point begin() equals end().
[QUOTE]
for(int i=0; i<index; i++) it++;[/QUOTE]

Now you iterate twice, thus incrementing the iterator two times.
It would now point to the third item in 'm_map' if it weren't empty,
but instead it's completely invalid.
[QUOTE]
m_map.erase((*it).first);

Unfortunately it = it + 1 is not working and this way in the code
compiler idicates iterator is out of range :// . I have 2 items (pairs
of key and string) in multimap when i am trying to do so. Can someone
tell me why this code is not working please?

Try this:

for(int i = 0; i < index; ++i) {
if(it == m_map.end()) throw "out of range";
++it;
}
if(it != m_map.end()) m_map.erase(it->first);

Post proper compilable code.
 
J

Jerry Coffin

Hi

Are iterators' behaviour the same as pointers? I mean I know i can go
through the container with the same manner as if i use pointers (ptr++)
and i can derefference iterators the same way.

But

Can I add an integer number to them and skip a few elements like I would
use pointers (ptr + 4 etc.) ?

That depends on the class of the iterator -- a random access iterator
allows you to add/subtract arbitrary amounts, but most other iterators
don't.
 
Z

ZikO

Try this:
for(int i = 0; i < index; ++i) {
if(it == m_map.end()) throw "out of range";
++it;
}
if(it != m_map.end()) m_map.erase(it->first);

It's true.
Iterator was aiming to the item which did not exist. I have sort it out now.
Exceptions are not my strong side :p I am still beginner but as I can see
I need to catch up the stuff.
Thanks for explanation.
Post proper compilable code.

This is a bit complicated. I have almost done program in VC++ .NET and I
cant provide all code (I wish I could). I have simply given what was the
most important and fortunately it's working now =).
Honestly, it was one big mistake I have done stuff in VC++ .NET :/. All
these things with individual "managed" Objects and components provided
by CLI / Framework .NET driven me mad. I couldnt code like I was doing
in simple C++. I coudn't simply convert variables. Gosh. It was
nightmare. Anyway I have almost finished.

Thanks for help guys a few words of explanation.

Regards.
 
Z

ZikO

That depends on the class of the iterator -- a random access iterator
allows you to add/subtract arbitrary amounts, but most other iterators
don't.

I have found the book "Thinking In C++" second tom in internet and have
read already about iterators how they are classified.

Thanks
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top