×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Iterator Problem (Printing the address problem)... Help

Iterator Problem (Printing the address problem)... Help

Iterator Problem (Printing the address problem)... Help

(OP)
Hello,

I am newbie to the topic of iterators. I need to know how many iterations the following "for loop" will execute. Iterators are used.

aend = m_SampleA.end();
for (aiter=m_SampleA.begin(), aiter!=aend, ++aiter)
{
...
}

The compile error comes up when I tried to print the address,
printf("address = %p\n", m_SampleA.begin())

Also, I have no idea what is the memory separation between iterator increments.. so I have difficulty finding the number of iterations...

Is there any special function that returns the address ?

Thank you

RE: Iterator Problem (Printing the address problem)... Help

CODE

printf("address = %p\n", m_SampleA.begin())
What makes you think an iterator is a pointer?

Assuming no objects are inserted or deleted in the for loop, I would guess it would run m_SampleA.size() iterations.

RE: Iterator Problem (Printing the address problem)... Help

As cpjust alluded to, a pointer and an iterator are different. To get a pointer from an iterator, use:

CODE

&(*aiter)
although I don't see the need for doing that.

RE: Iterator Problem (Printing the address problem)... Help

An iterator is an object (or a type, if you wish;) which has its own operator* return an object of type T for which operator& is defined and returns a pointer to T. It looks like a pointer in this aspect (and has incr/decr and -> ops too) but it's not a pointer.

So (by definition;) if aiter is an iterator (of any kind) then *aiter returns an object and &*aiter returns a pointer to this object.

RE: Iterator Problem (Printing the address problem)... Help

[quote[
I am newbie to the topic of iterators. I need to know how many iterations the following "for loop" will execute. Iterators are used.

aend = m_SampleA.end();
for (aiter=m_SampleA.begin(), aiter!=aend, ++aiter)
{
...
}
[/quote]

CODE

int i = 0;
aend = m_SampleA.end();
for (aiter=m_SampleA.begin(), aiter!=aend, ++aiter)
{
  std::cout << i++ <<end;
}

Quote:

The compile error comes up when I tried to print the address,
printf("address = %p\n", m_SampleA.begin())
This has already been answered.  BTW, why printf and not cout.

std:cout << "address = " << m_SampleA.begin() <<endl;
_should_ (IIRC) print the address of m_SampleA.begin() which will be the same address &m_SampleA which will always be the same while m_SampleA is in scope.

Quote:

Also, I have no idea what is the memory separation between iterator increments.. so I have difficulty finding the number of iterations...
Why not just count them, or get the size of the m_SampleA using m_SampleA.size() if m_SampleA is a STL container like vector, map, list, etc.

Quote:

Is there any special function that returns the address ?
uolj had  the answer to this one.  But are you looking for the address of the iterator or the address of the object to which it points?

[plug=shameless]
http://game-master.us/phpx-3.4.0/
[/plug]

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close