Question on using for_each

Y

Yan

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.serialize(bytes);
}
..........

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

Thanks!
 
B

Barry

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.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));
 
M

Maxim Yegorushkin

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.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)));
 
S

sean_in_raleigh

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.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
 
Y

Yan

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.serialize(bytes);}
.........

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


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 :)
 

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

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top