#include <iostream>
#include <map>
#include <string>
#include <vector>
std::vector<int> numbers;
// *** before: ***
void oldWay() {
for (std::vector<int>::iterator it = numbers.begin();
it != numbers.end(); ++it)
std::cout << *it << " ";
std::cout << "\n";
// a plain array needed the size in the head of the loop
int arr[3] = { 1, 2, 3 };
for (std::size_t i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i)
std::cout << arr[i] << " ";
std::cout << "\n";
}
// *** in version C++11: ***
void newWay() {
for (int n : numbers) // a copy of every element
std::cout << n << " ";
std::cout << "\n";
for (int& n : numbers) // a reference allows changing it
n *= 2;
for (const auto& n : numbers) // the usual form: no copy, no change
std::cout << n << " ";
std::cout << "\n";
int arr[3] = { 1, 2, 3 };
for (int n : arr) // the size is known to the compiler
std::cout << n << " ";
std::cout << "\n";
}
int main() {
numbers.push_back(1);
numbers.push_back(2);
numbers.push_back(3);
oldWay();
newWay();
}