I was wondering if there is a for_each function in c++, I have searched for it on the internet and I cant seem to find a definitive answer, is it in the new draft for the c++x?

There is for_each in c++, but I think this is not what you mean. There probably will be extension to for statement to allow Java like foreach, but for example it is not yet included in release version of GCC (it will be in 4.6, but it is now in development), so I wouldn't jump into using it already.

I see, I think it would make certain tasks easier in C++

There is for_each in c++, but I think this is not what you mean...

What give you that indication? The std::for_each is the generic way to iterate over a sequence. Types need not be provided as they are encoded in what you are iterating over.

C++0x will include a new syntax option for the for-loops that is essentially the same as the foreach. Additionally, the use of for_each and many other <algorithm> will be made a lot nicer with the addition of Lambda expressions.

Here is a glimpse:

#include <iostream>
#include <algorithm>

int main() {
  int my_array[5] = {1,2,3,4,5};
  //this is the new for-loop syntax that can be used instead of for( ; ; )
  for (int &x: my_array) {
    x *= 2;
  };
  std::for_each(my_array, my_array + 5, [](int& x) { std::cout << x << std::endl; });

  //or equivalently, with Lambda expressions:
  std::for_each(my_array, my_array + 5, [](int& x) { x *= 2; });
  std::for_each(my_array, my_array + 5, [](int& x) { std::cout << x << std::endl; });
};

The above compiles fine using GCC 4.6.0 (experimental) (which is available btw, but you have to compile and bootstrap from source (can take a couple of hours to do)).

Where can I learn more about lambdas?
I just finished chapter 9 of my C++ book but im probably not even close to you guys, What do you guys think I should read after this book or maybe even look into right now to make me better?

CHAPTER 1: PROGRAMMING WITH VISUAL C++ 2010.

CHAPTER 2: DATA, VARIABLES, AND CALCULATIONS.

CHAPTER 3: DECISIONS AND LOOPS.

CHAPTER 4: ARRAYS, STRINGS, AND POINTERS.

CHAPTER 5: INTRODUCING STRUCTURE INTO YOUR PROGRAMS.

CHAPTER 6: MORE ABOUT PROGRAM STRUCTURE.

CHAPTER 7: DEFINING YOUR OWN DATA TYPES.

CHAPTER 8: MORE ON CLASSES.

CHAPTER 9: CLASS INHERITANCE AND VIRTUAL FUNCTIONS.

CHAPTER 10: THE STANDARD TEMPLATE LIBRARY.

CHAPTER 11: DEBUGGING TECHNIQUES.

CHAPTER 12: WINDOWS PROGRAMMING CONCEPTS.

CHAPTER 13: PROGRAMMING FOR MULTIPLE CORES.

CHAPTER 14: WINDOWS PROGRAMMING WITH THE MICROSOFT FOUNDATION CLASSES.

CHAPTER 15: WORKING WITH MENUS AND TOOLBARS.

CHAPTER 16: DRAWING IN A WINDOW.

CHAPTER 17: CREATING THE DOCUMENT AND IMPROVING THE VIEW.

CHAPTER 18: WORKING WITH DIALOGS AND CONTROLS.

CHAPTER 19: STORING AND PRINTING DOCUMENTS.

CHAPTER 20: WRITING YOUR OWN DLLs.

I don't think you could possibly find a book that talks about Lambda expressions in C++, because they are not even officially supported yet (but the new C++ standard will be there by the end of this year, at least, everyone is crossing their fingers for that).

From reading the list of chapters you posted, I think I would just recommend you get another book. Half of the book is about MFC, which is being phased out even in the Windows-only world (which is a world that is also shrinking rapidly). And only one chapter on the Standard Template Library! That can't be serious! But you should at least read that chapter before you let go of the book and go for a better one (which I can't recommend, cause I haven't learned through books and never read any other than the most advanced ones, I'm sure others will, or look at this thread).

If you have read through this book already and done enough hands on to get a good grip of the language (and I mean a lot!), then I would first recommend you go through all the C++FAQs. This is a good run-through of all the little intricacies of C++. By then if you feel up for it, go for "C++ Coding Standards" (Sutter & Alexandrescu) and then "Modern C++" (Alexandrescu) (these two, along with the actual C++ Standard documentation, are the only books I ever refer to).

Also, get familiar with the Boost libraries (they have lambdas in there too, that are usable in the current C++ version, but not as well integrated), that library is second only to the STL (and even then, some may argue that it's superior in shear awesomeness!). After that, you'll be pretty much done with the learning (well, in C++ you are never _really_ done with learning).

As for knowing about Lambdas of C++0x, just use the wiki on C++0x, or google around. They really aren't as hard as they seem (they just have a peculiar syntax, that's all).

Edited 5 Years Ago by mike_2000_17: n/a

Comments
good tips

Thanks for the tips Mike, I dont want to sound too nooby here but what would I need to know in order to program using an SDK like say an iPhone SDK and make programs for the iPhone? Or am I still far away from that?

Edited 5 Years Ago by L3gacy: n/a

Well, the iPhone uses Objective-C. And, as far as I know, it's not trivial to try an use C++ to program for it (Objective-C is an Apple thing, and we all know how monopolistic Steve Jobbs is, same as Bill Gates at Microsoft, no real difference there).

Are you far from being able to use an SDK? No. I would probably recommend a bit more ground work, at least on the STL and good OOP knowledge. But after that, you can jump right into it. Using SDKs (and multiple ones) to essentially "assemble" an application is a great way to learn and do some interesting (visual) stuff. And it really isn't that hard, but it can require some amount of patience when reading function/class documentations and troubleshooting.

Thanks once again Mike :) and thanks to everyone for answering all my questions.

This question has already been answered. Start a new discussion instead.