Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

shong9

Programmer
Aug 31, 2007
4
0
0
US
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
 
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.
 
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.
 
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.
 
[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;
}

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.

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.

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]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top