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

S

Steven T. Hatton

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
 
V

Victor Bazarov

Steven said:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top