需要引入头文件#include<algorithm>
1.for_each
#include<iostream> using namespace std; #include <vector> #include <algorithm> class MyPrint { public: void operator()(int val) const{ cout << val << " "; } }; void printVector(int val) { cout << val << ; } void test() { vector<int> v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } //利用普通函数 for_each(v1.begin(),v1.end(),printVector); cout << endl; 利用仿函数 endl; } main() { test(); system("pause); return 0; }
2.transform:将容器搬运到另一个容器中
#include<iostream> Transform { int { 这里可以对val进行一些判断 return val; } }; { cout << val << ; } }; ) { v1.push_back(i); } vector< v2; 目标容器需要先开辟空间 v2.resize(v1.size()); transform(v1.begin(),v2.begin(),Transform()); for_each(v2.begin(),v2.end(),1)">; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。