Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Foreach loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== C++ === [[C++11]] provides a foreach loop. The syntax is similar to that of [[Foreach loop#Java|Java]]: <syntaxhighlight lang="Cpp"> #include <iostream> int main() { int myint[] = {1, 2, 3, 4, 5}; for (int i : myint) { std::cout << i << '\n'; } } </syntaxhighlight> C++11 range-based for statements have been implemented in [[GNU Compiler Collection]] (GCC) (since version 4.6), [[Clang]] (since version 3.0) and [[Visual C++]] 2012 (version 11 <ref>{{cite web|url=http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx |title=C++11 Features in Visual C++ 11 - Visual C++ Team Blog - Site Home - MSDN Blogs |publisher=Blogs.msdn.com |date=2011-09-12 |access-date=2013-08-04}}</ref>) The range-based <code>for</code> is [[syntactic sugar]] equivalent to: <syntaxhighlight lang="Cpp"> for (auto __anon = begin(myint); __anon != end(myint); ++__anon) { auto i = *__anon; std::cout << i << '\n'; } </syntaxhighlight> The compiler uses [[argument-dependent lookup]] to resolve the <code>begin</code> and <code>end</code> functions.<ref>{{cite web|url=https://en.cppreference.com/w/cpp/language/range-for |title=Range-based for loop (since C++11) |publisher=en.cppreference.com |access-date=2018-12-03}}</ref> The [[C++ Standard Library]] also supports <code>for_each</code>,<ref>{{cite web|url=http://en.cppreference.com/w/cpp/algorithm/for_each |title=std::for_each - cppreference |publisher=en.cppreference.com |access-date=2017-09-30}}</ref> that applies each element to a function, which can be any predefined function or a lambda expression. While range-based for is only from the start to the end, the range or direction can be changed by altering the first two parameters. <syntaxhighlight lang="Cpp"> #include <iostream> #include <algorithm> // contains std::for_each #include <vector> int main() { std::vector<int> v {1, 2, 3, 4, 5}; std::for_each(v.begin(), v.end(), [](int i) { std::cout << i << '\n'; }); std::cout << "reversed but skip 2 elements:\n"; std::for_each(v.rbegin()+2, v.rend(), [](int i) { std::cout << i << '\n'; }); } </syntaxhighlight> [[Qt (software)|Qt]], a C++ framework, offers a macro providing foreach loops<ref>{{cite web |url=http://doc.qt.digia.com/4.2/containers.html#the-foreach-keyword |title=Qt 4.2: Generic Containers |publisher=Doc.qt.digia.com |access-date=2013-08-04 |archive-url=https://web.archive.org/web/20151123090839/http://doc.qt.digia.com/4.2/containers.html#the-foreach-keyword |archive-date=2015-11-23 |url-status=dead }}</ref> using the STL iterator interface: <syntaxhighlight lang="Cpp"> #include <QList> #include <QDebug> int main() { QList<int> list; list << 1 << 2 << 3 << 4 << 5; foreach (int i, list) { qDebug() << i; } }</syntaxhighlight> [[Boost (C++ libraries)|Boost]], a set of free peer-reviewed portable C++ libraries also provides foreach loops:<ref>{{cite web|author=Eric Niebler |url=http://www.boost.org/doc/libs/1_53_0/doc/html/foreach.html |title=Chapter 9. Boost.Foreach - 1.53.0 |publisher=Boost.org |date=2013-01-31 |access-date=2013-08-04}}</ref> <syntaxhighlight lang="Cpp"> #include <boost/foreach.hpp> #include <iostream> int main() { int myint[] = {1, 2, 3, 4, 5}; BOOST_FOREACH(int &i, myint) { std::cout << i << '\n'; } } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)