1 #include <algorithm>
2 #include <stdio.h>
3
4 void print(int &a)
5 {
6 printf("%d ", a);
7 }
8
9 int main()
10 {
11 int arr[]={1,2,3,4,5,6,7,8};
12
13 std::for_each(arr, arr+sizeof(arr)/sizeof(int), print);//stl
14
15 printf("\n");
16
17 for(auto a: arr)//c++11
18 {
19 printf("%d ", a);
20 }
21
22 return 0;
23 }
24
~
"foreach.cpp" 24L, 261C
编译:
stl: -lstdc++
c++11: -std=c++0x
gcc -Wall -lstdc++ -std=c++0x foreach.cpp