C++11新语法
不仅适用于array,还适用于vector
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 using namespace std; 5 6 void main() 7 { 8 vector<int>myvector; 9 myvector.push_back(11); 10 myvector.push_back(12); 11 myvector.push_back(13); 12 13 int sum = 0; 14 15 //[地址](参数) {函数; } 16 //直到地址,等价于操作变量,等价于返回值 17 for_each(myvector.begin(), myvector.end(), [&sum](int x) {sum += x; }); 18 19 cout << sum; 20 21 system("pause"); 22 }
时间: 2024-10-12 13:43:56