Newbie question on filter_iterators

Ok, I'm trying to learn the basics of filter_iterators. I've got a vector of
ints. I want to iterate across the vector and print out every positive value
in the vector. Currently, my code doesn't work because g++ doesn't like me
assigning a vector::iterator to a filtered_iterator. Obviously, I've missed
some crucial point on how the filtered_iterator is supposed to work. The
example code in the boost library doesn't really address this specific use.
Can someone show me how to make the code below work as intended?
Thanks in advance,
--Steve
==== CODE FOLLOWS ====
struct is_positive { bool operator() (int i) { return i >= 0; } };
int main()
{
std::vector<int> i;
i.push_back(-5);
i.push_back( 5);
i.push_back(-4);
i.push_back( 4);
i.push_back(-3);
i.push_back( 3);
i.push_back(-2);
i.push_back( 1);
typedef boost::filter_iterator

On 9/13/05, Stephen Gross
Ok, I'm trying to learn the basics of filter_iterators. I've got a vector of ints. I want to iterate across the vector and print out every positive value in the vector. Currently, my code doesn't work because g++ doesn't like me assigning a vector::iterator to a filtered_iterator. Obviously, I've missed some crucial point on how the filtered_iterator is supposed to work. The example code in the boost library doesn't really address this specific use. Can someone show me how to make the code below work as intended?
Thanks in advance, --Steve
==== CODE FOLLOWS ====
struct is_positive { bool operator() (int i) { return i >= 0; } };
int main() { std::vector<int> i;
i.push_back(-5); i.push_back( 5); i.push_back(-4); i.push_back( 4); i.push_back(-3); i.push_back( 3); i.push_back(-2); i.push_back( 1);
typedef boost::filter_iterator
filter_itr; for(filter_itr f = i.begin(); f != i.end(); ++f) { std::cout << *f << std::endl; }
return 0; }
I think you'll need to use something like: for(filter_itr f(i.begin(), i.end()); f != filter_itr(i.end()); ++f) filter_iterator doesn't have an assignment operator, you have to go through the relevant constructor (see http://www.boost.org/libs/iterator/doc/filter_iterator.html#filter-iterator-...) Stuart Dootson

Stuart Dootson
I think you'll need to use something like:
for(filter_itr f(i.begin(), i.end()); f != filter_itr(i.end()); ++f)
Or use make_filter_iterator.
filter_iterator doesn't have an assignment operator,
Whoa there, it certainly does! All iterators are required to support assignment. It doesn't have an implicit conversion from the underlying iterator type, though, which rules out using copy initialization from anything other than another filter_iterator.
you have to go through the relevant constructor (see http://www.boost.org/libs/iterator/doc/filter_iterator.html#filter-iterator-...)
-- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (4)
-
Alexey Bakhvalov
-
David Abrahams
-
Stephen Gross
-
Stuart Dootson