Boost logo

Boost Users :

From: Stuart Dootson (stuart.dootson_at_[hidden])
Date: 2005-09-13 10:43:20


On 9/13/05, Stephen Gross <sgross_at_[hidden]> wrote:
> 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<is_positive, std::vector<int>::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-synopsis)

Stuart Dootson


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net