Connecting Tech Pros Worldwide Help | Site Map

STL:invoking a member of an object not in the container

Steven T. Hatton
Guest
 
Posts: n/a
#1: Jul 23 '05
I have a std::vector<Vec3*> _vertices and an object BoundingBox _bbox. _bbox
has a member function called void expandBy(Vec3& v); I want to call
_bbox.expandBy on every member of _vertices. I know I can loop through and
do that. I also know I can create a function object and pass it. The
latter can be pretty slick, if I will be doing it a lot. It seems more
trouble than it's worth to do it for only one function in one class.
AFAIK, there is no way to use things such as mem_fun to do that. They
apply to the objects in the container being iterated over by the STL
algorithm.

Is there a way to accomplish what I want with the STL algorithms? Boost?
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: STL:invoking a member of an object not in the container


Steven T. Hatton wrote:[color=blue]
> I have a std::vector<Vec3*> _vertices and an object BoundingBox _bbox. _bbox
> has a member function called void expandBy(Vec3& v); I want to call
> _bbox.expandBy on every member of _vertices. I know I can loop through and
> do that. I also know I can create a function object and pass it. The
> latter can be pretty slick, if I will be doing it a lot. It seems more
> trouble than it's worth to do it for only one function in one class.
> AFAIK, there is no way to use things such as mem_fun to do that. They
> apply to the objects in the container being iterated over by the STL
> algorithm.
>
> Is there a way to accomplish what I want with the STL algorithms? Boost?[/color]

Actually, using 'bind1st' and 'mem_fun' should be enough, no need for
Boost. I can never remember the right order of things, takes me a couple
of tries to get it right, but the direction is something like

BoundingBox _bbox;
for_each(_vertices.begin(), _vertices.end(),
bind1st(mem_fun(&BoundingBox::expandBy), &_bbox));

V
Closed Thread