for_each algor

for (std::vector<char>::iterator i = myBuffer.begin(); i != myBuffer.end(); ++i)
            *i -= 1;

for_each(myBuffer.begin(), myBuffer.end(), bind1st(std::plus<char>(), -1));

Are above two equivalent, why?
There's a new form of the for that came with C++11 that does this too called the range based for. Personally, I've never used for_each.
There is also something called the auto type from the C++11 standard which saves you from writing std::vector<char>::iterator and just write auto. Taking your for statement, it simply becomes:
for (auto i = myBuffer.begin(); i != myBuffer.end(); i ++)
   *i -= 1;

Or even simpler with the for-range:
for (char myChar : myBuffer)
   myChar -= 1;
I think I have to take time to learn some C++11 feature
Might want to make sure your compiler supports them first. What are you using? Version number as well if you know how to get it.
My compiler 's version is 4.4. It's pretty old. Can you provide me fast track to build C++11 environment? thanks.
I always suggest the g++ compiler. I have no clue which compiler you're using, but I'm currently using g++ 4.7.0 and has adequate C++11 support. Obviously, a more recent version would have a better support, and same goes with the MSVC compiler, the newer the better.

There was also a bunch of stuff added that has been slowly introduced in over the years, but you'll just have to find what they are. Two others that I can think of off the top of my head are lambda functions and the function data type. Both are extremely powerful, and fun to mess around with.

Oh, and I almost forgot, it also added some new random functions. I haven't even played with them yet though.
> Are above two equivalent, why?
┬┐too hard to test?

the `for_each' will not modify the elements (there is no assignment)
Thank you, you solved my problem. I just lost my patience in debug.
Topic archived. No new replies allowed.