For Programmers: Free Programming Magazines  


Home > Archive > VC STL > March 2005 > using 'while' for traversing with iterator.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author using 'while' for traversing with iterator.
Corey Wirun

2005-03-02, 9:00 pm

Hi All,

I'm porting a whole 'swack' of legacy code that uses the Borland BIDS
containers to STL under Visual Studio. In this environment, I have hundreds
of code constructs that look like this:

TIterator * pIter = obj->InitIterator();

while ( *pIter )
{
Object * pListObj = ( *pIter)++;
...
}

If I carry this forward to an std::list and using list<T>::iterator, is
there a way I can 'while' my way through the objects in a std::list using
the list<T>::iterator? All the examples I've seen in text books, etc., show
something like this for traversing the list:

for (iter = collection.begin(); iter != collection.end(); ++iter )
{
cout << *iter << ' ';
}

I don't have any problem converting all the constructs to use the 'for', but
it can save me ws of time to be able to somehow 'while' my way through.

Thanks in Advance!
Corey.


Igor Tandetnik

2005-03-02, 9:00 pm

"Corey Wirun" <corey.wirun@nospam.ca> wrote in message
news:uX4Z5H2HFHA.3776@TK2MSFTNGP10.phx.gbl
> I'm porting a whole 'swack' of legacy code that uses the Borland BIDS
> containers to STL under Visual Studio. In this environment, I have
> hundreds of code constructs that look like this:
>
> TIterator * pIter = obj->InitIterator();
>
> while ( *pIter )
> {
> Object * pListObj = ( *pIter)++;


Are the parentheses really placed this way? This code never increments
the iterator, so I'm not sure how it ever breaks out of the loop.

> If I carry this forward to an std::list and using list<T>::iterator,
> is there a way I can 'while' my way through the objects in a
> std::list using the list<T>::iterator? All the examples I've seen in
> text books, etc., show something like this for traversing the list:
>
> for (iter = collection.begin(); iter != collection.end(); ++iter )
> {
> cout << *iter << ' ';
> }


Well, you can always change this to

iter = collection.begin();
while (iter != collection.end())
{
cout << *iter++ << ' ';
}

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Doug Harrison [MVP]

2005-03-02, 9:00 pm

Corey Wirun wrote:

>Hi All,
>
>I'm porting a whole 'swack' of legacy code that uses the Borland BIDS
>containers to STL under Visual Studio. In this environment, I have hundreds
>of code constructs that look like this:
>
>TIterator * pIter = obj->InitIterator();
>
>while ( *pIter )
>{
> Object * pListObj = ( *pIter)++;
> ...
>}
>
>If I carry this forward to an std::list and using list<T>::iterator, is
>there a way I can 'while' my way through the objects in a std::list using
>the list<T>::iterator? All the examples I've seen in text books, etc., show
>something like this for traversing the list:
>
>for (iter = collection.begin(); iter != collection.end(); ++iter )
>{
> cout << *iter << ' ';
>}
>
>I don't have any problem converting all the constructs to use the 'for', but
>it can save me ws of time to be able to somehow 'while' my way through.


You have to arrange for a comparison with the right container.end(). It
might be possible to write a TIterator replacement that requires at most a
new constructor that takes a reference to a container or iterator range.
That way, you could avoid changing your loop patterns.

--
Doug Harrison
Microsoft MVP - Visual C++
Corey Wirun

2005-03-02, 9:00 pm

> > TIterator * pIter = obj->InitIterator();
>
> Are the parentheses really placed this way? This code never increments
> the iterator, so I'm not sure how it ever breaks out of the loop.
>


Yeah, Igor, that's what I thought when I saw it as well. Apparently the
Borland BIDS containers do some funky stuff with iterators. That's why
(*pIter) == NULL works to detect the end of the list, I guess.

Thanks for your suggestion.
C.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com