从for_each开始说起 回调函数与仿函数
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> #include <iostream> #include <algorithm> using namespace std; //回调函数 void call_back(char elem) { cout << elem << endl; } //仿函数 struct Functor { void operator() (char elem) { cout << elem << endl; } }; int main() { string strA = "hello"; string strB = "world"; for_each(strA.begin(),strA.end(),Functor()); cout<<"===========GAP==============="<<endl; for_each(strB.begin(),strB.end(),call_back); getchar(); return 0; } h e l l o ===========GAP=============== w o r l d 可能会有疑问两者有什么区别? 假如我要for_each遍历的不是字符串而是int类型的vector呢? 是不是又要重写一个int类型作为参数的回调函数