Does filter_iterator work for maps?
Hi, I don't see a reason for filter_iterator not to work with map iterators. However, the program below doesn't work. I get a compiler error "Cannot generate template specialization from 'make_filter_iterator<Predicate,Iterator> (Iterator,Iterator,const Predicate &)' in function main()" Any ideas? Thanks, Rodrigo #include <map> #include <ostream> #include "boost/iterator_adaptors.hpp" #include <algorithm> using namespace std; using namespace boost; struct pair_is_active { bool operator()(const pair<const size_t,float>& p) { return 0 < p.second; } }; ostream& operator<<(ostream& o, pair<const size_t, float> p) { return o << "(" << p.first << ", " << p.second << ")"; } int main() { map<size_t,float> m; m[0] = 10; m[1] = 15; m[2] = 0; m[3] = 13; copy(boost::make_filter_iterator<pair_is_active>(m.begin, m.end()), boost::make_filter_iterator<pair_is_active>(m.end(), m.end()), ostream_iterator<pair<const size_t,float> >(cout,",")); return 0; }
At 3:01 AM +0000 3/15/02, rodrigo_braz wrote:
#include <map> #include <ostream>
#include <iostream>
#include "boost/iterator_adaptors.hpp" #include <algorithm>
using namespace std; using namespace boost;
struct pair_is_active { bool operator()(const pair<const size_t,float>& p) { return 0 < p.second; } };
ostream& operator<<(ostream& o, pair<const size_t, float> p) { return o << "(" << p.first << ", " << p.second << ")"; }
int main() { map<size_t,float> m; m[0] = 10; m[1] = 15; m[2] = 0; m[3] = 13;
copy(boost::make_filter_iterator<pair_is_active>(m.begin, m.end()),
copy(boost::make_filter_iterator<pair_is_active>(m.begin(), m.end()),
boost::make_filter_iterator<pair_is_active>(m.end(), m.end()), ostream_iterator<pair<const size_t,float> >(cout,","));
return 0; }
-- Jon Kalb Kalb@LibertySoft.com
--- In Boost-Users@y..., Jon Kalb <kalb@L...> wrote:
At 3:01 AM +0000 3/15/02, rodrigo_braz wrote:
copy(boost::make_filter_iterator<pair_is_active>(m.begin, m.end ()),
copy(boost::make_filter_iterator<pair_is_active>(m.begin(), m.end ()),
Ouch. Ok, sorry about that. Actually my main problem with this iterator is in the code below: #include <vector> #include "boost/iterator_adaptors.hpp" using namespace std; using namespace boost; struct is_active { bool operator()(int p) { return 0 < p; } }; struct A { typedef filter_iterator_generator<is_active,vector<int>::iterator>::type active_iterator; active_iterator i; // PROBLEM }; int main() { A a; return 0; } I get the error messages: 15: Type name expected 15: Multiple declaration for 'A::active_iterator' 14: Earlier declaration of 'A::active_iterator' 15: Declaration missing ; Thanks, Rodrigo
On 3/15/02 4:37 PM, "rodrigo_braz" <rodrigo_braz@yahoo.com> wrote:
--- In Boost-Users@y..., Jon Kalb <kalb@L...> wrote: Actually my main problem with this iterator is in the code below:
#include <vector> #include "boost/iterator_adaptors.hpp"
using namespace std; using namespace boost;
struct is_active { bool operator()(int p) { return 0 < p; } };
struct A { typedef filter_iterator_generator<is_active,vector<int>::iterator>::type active_iterator;
CodeWarrior doesn't complain about this code, but I think that it should be: typedef typename filter_iterator_generator<is_active,vector<int>::iterator>::type active_iterator; -- Jon Kalb Kalb@LibertySoft.com
On Fri, 15 Mar 2002, Jon Kalb wrote: kalb> > struct A kalb> > { kalb> > typedef kalb> > filter_iterator_generator<is_active,vector<int>::iterator>::type kalb> > active_iterator; kalb> kalb> kalb> CodeWarrior doesn't complain about this code, but I think that it should be: kalb> kalb> typedef typename kalb> filter_iterator_generator<is_active,vector<int>::iterator>::type kalb> active_iterator; "typename" is required when the type of the left-hand side of the "::" is somehow dependent on a template parameter, which is not the case in the above code. Also, "typename" is not allowed when the left-hand side is not dependent on a template paraemter, so adding "typename" to the above code results in an illegal program. Cheers, Jeremy ---------------------------------------------------------------------- Jeremy Siek http://php.indiana.edu/~jsiek/ Ph.D. Student, Indiana Univ. B'ton email: jsiek@osl.iu.edu C++ Booster (http://www.boost.org) office phone: (812) 855-3608 ----------------------------------------------------------------------
Hi Rodrigo, The above program compiles OK for me with g++ 2.95.2. What compiler are you using? (there's a good chance the compiler error you are getting is a bug in your compiler, not in your code or in the Boost code). Cheers, Jeremy On Sat, 16 Mar 2002, rodrigo_braz wrote: rodrig> --- In Boost-Users@y..., Jon Kalb <kalb@L...> wrote: rodrig> > At 3:01 AM +0000 3/15/02, rodrigo_braz wrote: rodrig> > > copy(boost::make_filter_iterator<pair_is_active>(m.begin, m.end rodrig> ()), rodrig> > rodrig> > copy(boost::make_filter_iterator<pair_is_active>(m.begin(), m.end rodrig> ()), rodrig> rodrig> Ouch. Ok, sorry about that. Actually my main problem with this rodrig> iterator is in the code below: rodrig> rodrig> #include <vector> rodrig> #include "boost/iterator_adaptors.hpp" rodrig> rodrig> using namespace std; rodrig> using namespace boost; rodrig> rodrig> struct is_active { rodrig> bool operator()(int p) { return 0 < p; } rodrig> }; rodrig> rodrig> struct A rodrig> { rodrig> typedef rodrig> filter_iterator_generator<is_active,vector<int>::iterator>::type rodrig> active_iterator; rodrig> active_iterator i; // PROBLEM rodrig> }; rodrig> rodrig> int main() rodrig> { rodrig> A a; rodrig> return 0; rodrig> } rodrig> rodrig> I get the error messages: rodrig> rodrig> 15: Type name expected rodrig> 15: Multiple declaration for 'A::active_iterator' rodrig> 14: Earlier declaration of 'A::active_iterator' rodrig> 15: Declaration missing ; rodrig> rodrig> Thanks, rodrig> rodrig> Rodrigo rodrig> rodrig> rodrig> rodrig> rodrig> Info: <http://www.boost.org> rodrig> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> rodrig> Unsubscribe: <mailto:boost-users-unsubscribe@yahoogroups.com> rodrig> rodrig> rodrig> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ rodrig> rodrig> ---------------------------------------------------------------------- Jeremy Siek http://php.indiana.edu/~jsiek/ Ph.D. Student, Indiana Univ. B'ton email: jsiek@osl.iu.edu C++ Booster (http://www.boost.org) office phone: (812) 855-3608 ----------------------------------------------------------------------
participants (3)
-
Jeremy Siek
-
Jon Kalb
-
rodrigo_braz