12345678910111213141516171819202122232425262728293031323334353637383940
#include <iostream> #include <algorithm> #include <vector> void myfunction (int i) { // here is your 'int i' std::cout << ' ' << i; } int main () { std::vector<int> myvector; myvector.push_back(10); myvector.push_back(20); myvector.push_back(30); for_each( myvector.begin(), myvector.end(), myfunction ); std::cout << '\n'; for ( int x : myvector ) myfunction( x ); std::cout << '\n'; for ( auto it = myvector.begin(); it < myvector.end(); ++it ) myfunction( *it ); std::cout << '\n'; for_each( myvector.begin(), myvector.end(), [](int i){std::cout << ' ' << i;} ); std::cout << '\n'; for ( int i : myvector ) std::cout << ' ' << i; std::cout << '\n'; for ( size_t it = 0; it < myvector.size(); ++it ) { int i = myvector[it]; std::cout << ' ' << i; } std::cout << '\n'; return 0; }