replace loop with some algo ( std & boost maybe )

Y

yurec

Hi all,

I've following loop and i'm interested if there is any algorithm in
stl (i see just for_each with appropriate logical meaning) maybe with
some boost classes which can replace following loop :

vector<UIStlLink*>::iterator iter_stl =
i_bone_fragments.begin();
vector<MCoordinateSystem>::const_iterator iter_cs =
i_bone_fragments_to.begin();
while (iter_stl != i_bone_fragments.end())
{
UIStlLink * p_stl = *iter_stl;
const MCoordinateSystem & cs = *iter_cs;
TransformObjectTo(p_stl,cs);
++iter_stl;
++iter_cs;
}
ASSERT(iter_cs == i_bone_fragments_to.end());

As for me I would like to see smth like :

vector<MCoordinateSystem>::const_iterator iter_cs =
i_bone_fragments_to.begin();
for_each(i_bone_fragments.begin(),i_bone_fragments.end(),bind(TransformObjectTo,
_1, *iter_cs++));

, but (*iter_cs++) is computed once as expected.

Thanks
 
V

Victor Bazarov

yurec said:
Hi all,

I've following loop and i'm interested if there is any algorithm in
stl (i see just for_each with appropriate logical meaning) maybe with
some boost classes which can replace following loop :

vector<UIStlLink*>::iterator iter_stl =
i_bone_fragments.begin();
vector<MCoordinateSystem>::const_iterator iter_cs =
i_bone_fragments_to.begin();
while (iter_stl != i_bone_fragments.end())
{
UIStlLink * p_stl = *iter_stl;
const MCoordinateSystem & cs = *iter_cs;
TransformObjectTo(p_stl,cs);
++iter_stl;
++iter_cs;
}
ASSERT(iter_cs == i_bone_fragments_to.end());

As for me I would like to see smth like :

vector<MCoordinateSystem>::const_iterator iter_cs =
i_bone_fragments_to.begin();
for_each(i_bone_fragments.begin(),i_bone_fragments.end(),bind(TransformObjectTo,
_1, *iter_cs++));

, but (*iter_cs++) is computed once as expected.

Take a look at 'std::transform'.

V
 
A

Abhishek Padmanabh

Hi all,

I've following loop and i'm interested if there is any algorithm in
stl (i see just for_each with appropriate logical meaning) maybe with
some boost classes which can replace following loop :

vector<UIStlLink*>::iterator iter_stl       =
i_bone_fragments.begin();
  vector<MCoordinateSystem>::const_iterator iter_cs =
i_bone_fragments_to.begin();
  while (iter_stl != i_bone_fragments.end())
    {
    UIStlLink * p_stl = *iter_stl;
    const MCoordinateSystem & cs  = *iter_cs;
    TransformObjectTo(p_stl,cs);
    ++iter_stl;
    ++iter_cs;
    }
  ASSERT(iter_cs == i_bone_fragments_to.end());

As for me I would like to see smth like :

vector<MCoordinateSystem>::const_iterator iter_cs =
i_bone_fragments_to.begin();
for_each(i_bone_fragments.begin(),i_bone_fragments.end(),bind(TransformObje­ctTo,
_1, *iter_cs++));

, but (*iter_cs++) is computed once as expected.

You can't do that without a unary functor having
vector<MCoordinateSystem>::const_iterator type as a member which you
initialize upon functor construction and then keep incrementing it
with each call to operator() and applying the logic as in
TransformObje­ctTo. Otherwise, just use transform (the state would not
be needed as it would be a binary functor - probably you can use
TransformObje­ctTo straightaway).
 
Y

yurec

Take a look at 'std::transform'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

For my case transform is really good approach.Thank you very much.
I used it in this manner :

transform(i_bone_fragments.begin(),i_bone_fragments.end(),
i_bone_fragments_to.begin(),i_bone_fragments.begin(),
bind(TransformObjectTo, _1, _2));

Seems to be correct.
However I can imagine situation when should not put result
of transformation into somewhere.And using transform with
binary function I have to put result somewhere, have not I?
Could you suggest another way to solve the task without output
iterator?
 
A

Abhishek Padmanabh

For my case transform is really good approach.Thank you very much.
I used it in this manner :

transform(i_bone_fragments.begin(),i_bone_fragments.end(),
            i_bone_fragments_to.begin(),i_bone_fragments.begin(),
            bind(TransformObjectTo, _1, _2));

Seems to be correct.
However I can imagine situation when should not put result
of transformation into somewhere.And using transform with
binary function I have to put result somewhere, have not I?
Could you suggest another way to solve the task without output
iterator?

Your question is not very clear. That somewhere can be the source
itself (as you have done above), no need to put it somewhere else if
you don't want to. It might be needed when you don't wish to modify
your source though. Will that be a problem for you? This flexibility
is not available with for_each but you can achieve it with a little
pain in the functor.
 
Y

yurec

Your question is not very clear. That somewhere can be the source
itself (as you have done above), no need to put it somewhere else if
you don't want to. It might be needed when you don't wish to modify
your source though. Will that be a problem for you? This flexibility
is not available with for_each but you can achieve it with a little
pain in the functor

As I say for me it's ok to put result into the source, but if I don't
want to
modify the source i will not be able to do the same with such nice (as
for me)
piece of code.
 

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
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top