[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Question on using for_each

Yan

11/6/2008 2:47:00 PM

I have a vector of elements which I iterate through and call a method
on each of the elements. I want to do it using std::for_each
algorithm, but having a problem implementing it since the method that
I call on each element takes an argument and I don't know how to pass
this argument through. Here is the code using an old fashioned loop:

.......
std::vector<unsigned char> bytes;
std::vector<T> elements;
....
for (int i = 0; i < elements.size(); ++i) {
elements[i].serialize(bytes);
}
..........

could someone please help me out rewrite that using std::for_each?

Thanks!
4 Answers

Barry

11/6/2008 4:28:00 PM

0

On 11?6?, ??10?47?, Yan <yvinogra...@gmail.com> wrote:
> I have a vector of elements which I iterate through and call a method
> on each of the elements. I want to do it using std::for_each
> algorithm, but having a problem implementing it since the method that
> I call on each element takes an argument and I don't know how to pass
> this argument through. Here is the code using an old fashioned loop:
>
> ......
> std::vector<unsigned char> bytes;
> std::vector<T> elements;
> ...
> for (int i = 0; i < elements.size(); ++i) {
> elements[i].serialize(bytes);}
>
> .........
>
> could someone please help me out rewrite that using std::for_each?
>

struct Op
{
Op(std::vector<unsigned char>& bytes)
: bytes_(&bytes)
{}

void operator() (T& t)
{
t.serialize(*bytes_);
}

private:
std::vector<unsigned char>* bytes_;
};

std::for_each(elements.begin(), elements.end(), Op(bytes));

--
Best Regards
Barry

Maxim Yegorushkin

11/6/2008 4:31:00 PM

0

On Nov 6, 2:47 pm, Yan <yvinogra...@gmail.com> wrote:
> I have a vector of elements which I iterate through and call a method
> on each of the elements. I want to do it using std::for_each
> algorithm, but having a problem implementing it since the method that
> I call on each element takes an argument and I don't know how to pass
> this argument through. Here is the code using an old fashioned loop:
>
> ......
> std::vector<unsigned char>  bytes;
> std::vector<T> elements;
> ...
> for (int i = 0; i < elements.size(); ++i) {
>   elements[i].serialize(bytes);}
>
> .........
>
> could someone please help me out rewrite that using std::for_each?

It may be better to use a straight forward loop, because this way it
is much more easier to read and debug than for_each loops with complex
functors.

Compare your loop with:

std::for_each(elements.begin(), elements.end(),
boost::bind(&T::serialize, _1, boost::ref(bytes)));

--
Max

sean_in_raleigh

11/6/2008 4:35:00 PM

0

On Nov 6, 9:47 am, Yan <yvinogra...@gmail.com> wrote:
> I have a vector of elements which I iterate through and call a method
> on each of the elements. I want to do it using std::for_each
> algorithm, but having a problem implementing it since the method that
> I call on each element takes an argument and I don't know how to pass
> this argument through. Here is the code using an old fashioned loop:
>
> ......
> std::vector<unsigned char> bytes;
> std::vector<T> elements;
> ...
> for (int i = 0; i < elements.size(); ++i) {
> elements[i].serialize(bytes);}
>
> .........
>
> could someone please help me out rewrite that using std::for_each?
>
> Thanks!

Here's the standard-C++ way, though the boost::bind method
mentioned earlier is more flexible.

class MySerializable
{
public:
void serialize(vector<unsigned char> *b)
{
b->push_back(my_bits);
}
private:
unsigned char my_bits;
};

int
main(int argc, char **argv)
{
vector<unsigned char> bytes;
vector<MySerializable> elements;

// ... fill in elements ...

for_each(elements.begin(), elements.end(),
bind2nd(mem_fun_ref(&MySerializable::serialize),
&bytes));
}

Sean

Yan

11/6/2008 7:34:00 PM

0

On Nov 6, 11:35 am, sean_in_rale...@yahoo.com wrote:
> On Nov 6, 9:47 am, Yan <yvinogra...@gmail.com> wrote:
>
>
>
> > I have a vector of elements which I iterate through and call a method
> > on each of the elements. I want to do it using std::for_each
> > algorithm, but having a problem implementing it since the method that
> > I call on each element takes an argument and I don't know how to pass
> > this argument through. Here is the code using an old fashioned loop:
>
> > ......
> > std::vector<unsigned char>  bytes;
> > std::vector<T> elements;
> > ...
> > for (int i = 0; i < elements.size(); ++i) {
> >   elements[i].serialize(bytes);}
>
> > .........
>
> > could someone please help me out rewrite that using std::for_each?
>
> > Thanks!
>
> Here's the standard-C++ way, though the boost::bind method
> mentioned earlier is more flexible.
>
> class MySerializable
> {
> public:
>     void serialize(vector<unsigned char> *b)
>     {
>         b->push_back(my_bits);
>     }
> private:
>     unsigned char my_bits;
>
> };
>
> int
Thanks everyone!

Indeed, in this case the good old loop seems easier to write, read,
and, if needed, to debug :)


> main(int argc, char **argv)
> {
>     vector<unsigned char>  bytes;
>     vector<MySerializable> elements;
>
>     // ... fill in elements ...
>
>     for_each(elements.begin(), elements.end(),
>              bind2nd(mem_fun_ref(&MySerializable::serialize),
> &bytes));
>
> }
>
> Sean