In my the cs106b book we use the expression \"foreach\" to go through a list of words in a Map. I implemented the code and banged my head against the wall facing mysterious erro
What book are you using?
foreach
is not a C++ keyword, and I think the closest extension that introduces it, with that specific syntax, into the language is in Visual C++, as described in this link: http://blogs.msdn.com/b/arich/archive/2004/09/08/227139.aspx
There is for_each
in <algorithm>
, but its signature is very different from what you're using (which is a very Java for-each syntax).
Also I notice that you're using Map
which is different from std::map
?
foreach
is not a standard C++ feature. This was something Eric Roberts and I developed for the Stanford introductory programming sequence and predates the more modern C++11 range-based for loop. Now that C++11 compiler support is more widespread, we've stopped using foreach
and just opted to go with the standard C++ enhanced for
loop.
I would generally not advice using foreach
going forward as it's nonstandard. However, if you're compiling older code that uses it, you'll need to include one of the header files from the Stanford C++ Libraries that defines it.
foreach is not a construct in C++!