| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"yurec" |
| Date: |
11 Jan 2008 06:45:23 AM |
| Object: |
replace loop with some algo ( std & boost maybe ) |
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
.
|
|
| User: "Abhishek Padmanabh" |
|
| Title: Re: replace loop with some algo ( std & boost maybe ) |
11 Jan 2008 08:43:32 AM |
|
|
On Jan 11, 5:45=A0pm, yurec <Yurij.Zha...@materialise.kiev.ua> 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 =A0 =A0 =A0 =3D
i_bone_fragments.begin();
=A0 vector<MCoordinateSystem>::const_iterator iter_cs =3D
i_bone_fragments_to.begin();
=A0 while (iter_stl !=3D i_bone_fragments.end())
=A0 =A0 {
=A0 =A0 UIStlLink * p_stl =3D *iter_stl;
=A0 =A0 const MCoordinateSystem & cs =A0=3D *iter_cs;
=A0 =A0 TransformObjectTo(p_stl,cs);
=A0 =A0 ++iter_stl;
=A0 =A0 ++iter_cs;
=A0 =A0 }
=A0 ASSERT(iter_cs =3D=3D i_bone_fragments_to.end());
As for me I would like to see smth like :
vector<MCoordinateSystem>::const_iterator iter_cs =3D
i_bone_fragments_to.begin();
for_each(i_bone_fragments.begin(),i_bone_fragments.end(),bind(TransformObj=
e=ADctTo,
_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=ADctTo. Otherwise, just use transform (the state would not
be needed as it would be a binary functor - probably you can use
TransformObje=ADctTo straightaway).
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: replace loop with some algo ( std & boost maybe ) |
11 Jan 2008 08:21:12 AM |
|
|
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
.
|
|
|
| User: "yurec" |
|
| Title: Re: replace loop with some algo ( std & boost maybe ) |
11 Jan 2008 09:16:00 AM |
|
|
On Jan 11, 4:21=A0pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
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 =A0 =A0 =A0 =3D
i_bone_fragments.begin();
=A0vector<MCoordinateSystem>::const_iterator iter_cs =3D
i_bone_fragments_to.begin();
=A0while (iter_stl !=3D i_bone_fragments.end())
=A0 =A0{
=A0 =A0UIStlLink * p_stl =3D *iter_stl;
=A0 =A0const MCoordinateSystem & cs =A0=3D *iter_cs;
=A0 =A0TransformObjectTo(p_stl,cs);
=A0 =A0++iter_stl;
=A0 =A0++iter_cs;
=A0 =A0}
=A0ASSERT(iter_cs =3D=3D i_bone_fragments_to.end());
As for me I would like to see smth like :
vector<MCoordinateSystem>::const_iterator iter_cs =3D
i_bone_fragments_to.begin();
for_each(i_bone_fragments.begin(),i_bone_fragments.end(),bind(TransformO=
bje=ADctTo,
_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_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?
.
|
|
|
| User: "Abhishek Padmanabh" |
|
| Title: Re: replace loop with some algo ( std & boost maybe ) |
11 Jan 2008 09:27:10 AM |
|
|
On Jan 11, 8:16=A0pm, yurec <Yurij.Zha...@materialise.kiev.ua> wrote:
On Jan 11, 4:21=A0pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
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 =A0 =A0 =A0 =3D
i_bone_fragments.begin();
=A0vector<MCoordinateSystem>::const_iterator iter_cs =3D
i_bone_fragments_to.begin();
=A0while (iter_stl !=3D i_bone_fragments.end())
=A0 =A0{
=A0 =A0UIStlLink * p_stl =3D *iter_stl;
=A0 =A0const MCoordinateSystem & cs =A0=3D *iter_cs;
=A0 =A0TransformObjectTo(p_stl,cs);
=A0 =A0++iter_stl;
=A0 =A0++iter_cs;
=A0 =A0}
=A0ASSERT(iter_cs =3D=3D i_bone_fragments_to.end());
As for me I would like to see smth like :
vector<MCoordinateSystem>::const_iterator iter_cs =3D
i_bone_fragments_to.begin();
for_each(i_bone_fragments.begin(),i_bone_fragments.end(),bind(Transfor=
mObje=AD=ADctTo,
_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 te=
xt -
- 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(),
=A0 =A0 =A0 =A0 =A0 =A0 i_bone_fragments_to.begin(),i_bone_fragments.begin=
(),
=A0 =A0 =A0 =A0 =A0 =A0 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.
.
|
|
|
| User: "yurec" |
|
| Title: Re: replace loop with some algo ( std & boost maybe ) |
11 Jan 2008 09:40:38 AM |
|
|
On Jan 11, 5:27=A0pm, 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.
.
|
|
|
|
|
|
|

|
Related Articles |
|
|