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

Question on using for_each

P: n/a
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<Telements;
....
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!
Nov 6 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 11月6日, 下午10时47分, Yan <yvinogra...@gmail.comwrote:
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<Telements;
...
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

Nov 6 '08 #2

P: n/a
On Nov 6, 2:47*pm, Yan <yvinogra...@gmail.comwrote:
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<Telements;
...
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
Nov 6 '08 #3

P: n/a
On Nov 6, 9:47 am, Yan <yvinogra...@gmail.comwrote:
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<Telements;
...
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<MySerializableelements;

// ... fill in elements ...

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

Sean
Nov 6 '08 #4

P: n/a
Yan
On Nov 6, 11:35*am, sean_in_rale...@yahoo.com wrote:
On Nov 6, 9:47 am, Yan <yvinogra...@gmail.comwrote:
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<Telements;
...
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<MySerializableelements;

* * // ... fill in elements ...

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

}

Sean
Nov 6 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.