Using filter_iterator in a for loop
I want to use a boost::filter_iterator to iterate through a std::vector using a for-style loop. I haven't been able to find any examples of the proper way to do this in the Boost documentation (or how to do this for any other Boost iterator, for that matter).
From searching this newsgroup, I found that it is possible to do:
for (boost::filter_iterator< ... > iter(myVector.begin(), myVector.end()); iter != boost::filter_iterator< ... >(myVector.end()); ++iter ) { ... } However, iter is quite aware of when it has reached the end of the vector, since that is the second parameter in its constructor. Is it really necessary to create a second filter_iterator, set it to the end of the vector, and then test iter against it? Can't I somehow test iter alone to see if it has reached the end of the vector? If I use std::for_each, it needs both a beginning and ending iterator as parameters, leading to the same situation as above. I've also looked at the Boost Range library and BOOST_FOREACH (described at http://www.boost.org/regression-logs/cs-win32_metacomm/doc/html/foreach.html). However, from my understanding, I don't think either of these apply to or work with Boost iterators. Whatever method is appropriate, I would ask that someone please exapnd the Boost iterator documentation to include an example of using Boost iterators in a for-style loop. I feel that this is a very basic and common usage of iterators which was not addressed. Thank you. -- David Ward Communications and Networking Division Information Technology and Telecommunications Laboratory Georgia Tech Research Institute
"David Ward"
I want to use a boost::filter_iterator to iterate through a std::vector using a for-style loop. I haven't been able to find any examples of the proper way to do this in the Boost documentation (or how to do this for any other Boost iterator, for that matter).
From searching this newsgroup, I found that it is possible to do:
for (boost::filter_iterator< ... > iter(myVector.begin(), myVector.end()); iter != boost::filter_iterator< ... >(myVector.end()); ++iter ) { ... }
However, iter is quite aware of when it has reached the end of the vector, since that is the second parameter in its constructor. Is it really necessary to create a second filter_iterator, set it to the end of the vector, and then test iter against it? Can't I somehow test iter alone to see if it has reached the end of the vector?
Nope. It's one of the weaknesses of the STL iterator paradigm. istream_iterator has the same problem. It can be remedied by expanding the STL range concept to allow an end iterator of a different type. However, that's beyond the scope of boost::filter_iterator: it potentially requires changes to all algorithms, etc.
If I use std::for_each, it needs both a beginning and ending iterator as parameters, leading to the same situation as above.
I've also looked at the Boost Range library and BOOST_FOREACH (described at http://www.boost.org/regression-logs/cs-win32_metacomm/doc/html/foreach.html). However, from my understanding, I don't think either of these apply to or work with Boost iterators.
Not so; they work perfectly well with all conforming STL iterators.
Whatever method is appropriate, I would ask that someone please exapnd the Boost iterator documentation to include an example of using Boost iterators in a for-style loop. I feel that this is a very basic and common usage of iterators which was not addressed. Thank you.
I don't think there's really anything to write here; they're just iterators -- you use them the same way you'd use any other STL iterator. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams
-
David Ward