foreach not recognized in C++

前端 未结 9 1272
离开以前
离开以前 2021-01-21 08:07

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

相关标签:
9条回答
  • 2021-01-21 08:38

    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?

    0 讨论(0)
  • 2021-01-21 08:41

    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.

    0 讨论(0)
  • 2021-01-21 08:45

    foreach is not a construct in C++!

    0 讨论(0)
提交回复
热议问题