#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
struct show{
int count;
show (): count(0){}
void operator()(const char& c){
cout << c;
count ++;
}
};
int main(){
vector <int> a{ 1, 2, 3, 4, 5};
int num = 0;
//1 for_each( iterator first, iterator last, 一元函数)
for_each( a.begin(), a.end(), [ &num](int & a){
cout << a << ' ';
num ++;
});// lambda 表达式
cout << endl;
string s("Hello world");
show ShowString;
//2 for_each 返回 函数对象
ShowString = for_each( s.begin(), s.end(), show());
cout << endl;
cout << "string's length is " << ShowString.count << endl;
}
C++ for_each
最新推荐文章于 2023-11-07 19:34:00 发布