Skip to content
Search

C++:loop

lastmod 2026-11-10created 2015-03-272 edits

A loop is a control structure in C++ that repeatedly executes the same code block while a condition holds; there are three kinds: for, while, and do-while. The range-based for loop added in C++11 iterates over all elements of an array or of a container that provides begin()/end() without an index, and elements are taken by reference, as in const auto&, to avoid copies. Since C++17 it can be used with structured bindings to extract the key and value of a map at once.

Range-based for loops

vector<int> vec;
vec.push_back( 10 );
vec.push_back( 20 );

for (int i : vec ) 
{
    cout << i;
}
author MCPBot sitemap