using if_then lambda with transform

Hi, I have a vector of Rect. and I would like to copy those where Rect::getX() equals to certain value. typedef RectList list<Rect>; void handleX(const RectList& rectList, int value, RectList& destList) { transform(rectList.begin(), rectList.end(), back_inserter(destList), if_then ( bind(&Rect::getX, _1) == value , _1 )); } But I can't get it compile. Any help is appreciate. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

yinglcs2@yahoo.com wrote
I have a vector of Rect. and I would like to copy those where Rect::getX() equals to certain value.
typedef RectList list<Rect>;
void handleX(const RectList& rectList, int value, RectList& destList) {
transform(rectList.begin(), rectList.end(), back_inserter(destList), if_then ( bind(&Rect::getX, _1) == value , _1 )); }
It seems what you want is copy_if. transform won't help in this case because it will execute the predicate on every element and assign its return value to the output iterator, try this (not tested): for_each(rectList.begin(), rectList.end(), if_then(bind(&Rect::getX, _1) == value, bind(&RectList::push_back, ref(destList), _1))); -delfin

Delfin Rojas wrote:
yinglcs2@yahoo.com wrote
I have a vector of Rect. and I would like to copy those where Rect::getX() equals to certain value. ... It seems what you want is copy_if. transform won't help in this case because it will execute the predicate on every element and assign its return value to the output iterator, try this (not tested):
Or the OP can use transform with filter_iterator form the iterator library. See http://www.boost.org/libs/iterator/doc/filter_iterator.html Jeff Flinn

Jeff Flinn wrote:
Delfin Rojas wrote:
yinglcs2@yahoo.com wrote
I have a vector of Rect. and I would like to copy those where Rect::getX() equals to certain value. ... It seems what you want is copy_if. transform won't help in this case because it will execute the predicate on every element and assign its return value to the output iterator, try this (not tested):
Or the OP can use transform with filter_iterator form the iterator
I should have said std::copy.
library. See http://www.boost.org/libs/iterator/doc/filter_iterator.html
Jeff Flinn
Thanks, Jeff
participants (3)
-
Delfin Rojas
-
Jeff Flinn
-
yinglcs2@yahoo.com