By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,018 Members | 870 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,018 IT Pros & Developers. It's quick & easy.

replace loop with some algo ( std & boost maybe )

P: n/a
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
Jan 11 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
yurec wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 11 '08 #2

P: n/a
On Jan 11, 5:45*pm, yurec <Yurij.Zha...@materialise.kiev.uawrote:
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).
Jan 11 '08 #3

P: n/a
On Jan 11, 4:21*pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
yurec wrote:
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.

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_fragment s.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?
Jan 11 '08 #4

P: n/a
On Jan 11, 8:16*pm, yurec <Yurij.Zha...@materialise.kiev.uawrote:
On Jan 11, 4:21*pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:


yurec wrote:
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.
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_fragment s.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.
Jan 11 '08 #5

P: n/a
On Jan 11, 5:27*pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
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.

Jan 11 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.