473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question on using for_each

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
4 2442
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: flyaflya | last post by:
i use a class static member function to for_each: class t1 { static test(string s); } main() { list<string> slist; ...
5
by: ogerchikov | last post by:
Hi, I am reading a Boost.lambda example http://www.awprofessional.com/articles/article.asp?p=400651&seqNum=4. Here is the example: void demoVector() { vector<int> myvector;...
5
by: marius lazer | last post by:
I have an STL container of functors and I want to execute them using std::for_each. In the code snippet below the line marked "// good" works fine and the one marked "// nothing" compiles fine but...
3
by: PolkaHead | last post by:
I was wondering if there's a way to traverse a two-dimensional vector (vector of vectors) with a nested for_each call. The code included traverses the "outer" vector with a for_each, than it...
3
by: hn.ft.pris | last post by:
I've got following code test C++'s functor. For the sake of easy-reading, I omit some declearations. #include <algorithm> #include <functional> using namespace std;
13
by: Sarath | last post by:
What's the advantage of using for_each than the normal iteration using for loop? Is there any 'tweak' to use stream objects as 'Function' parameter of for_each loop. Seems copy function can do...
3
by: Yan | last post by:
Hi, I don't seem to be able to use for_each if it should replace a 'for' loop in a method (constructor in my case) and inside that 'for' loop a class member variable is being accessed. The...
3
by: Nan Li | last post by:
Hello, Can any one explain why the following code cannot get compiled ?? Thanks. #include <iostream> #include <string> #include <vector> #include <iterator> #include <algorithm>
4
by: suman.nandan | last post by:
Hi C++ Experts ! I have a little weird requirement. I have a base class, say B and lots of classes D1 .. Dn publicly derived from it. Over the course of development the number of derived...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.