[C++STL]常用遍历算法
生活随笔
收集整理的這篇文章主要介紹了
[C++STL]常用遍历算法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
代碼如下:
#include <iostream> #include <algorithm> #include <vector> using namespace std;void print01(int val) {cout << val << " "; }class print02 { public:void operator()(int val){cout << val << " ";} };void test01() {vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), print01);cout << endl;for_each(v.begin(), v.end(), print02());cout << endl;}int main() {test01();return 0; }測試結(jié)果:
總結(jié):
代碼如下:
#include <iostream> #include <vector> #include <algorithm> using namespace std;class TransForm { public:int operator()(int val){return val;} };class MyPrint { public:void operator()(int val){cout << val << " ";} };void test01() {vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>vTarget;vTarget.resize(v.size());transform(v.begin(), v.end(), vTarget.begin(), TransForm());for_each(vTarget.begin(), vTarget.end(), MyPrint());}int main() {test01();return 0; }測試結(jié)果:
總結(jié):
總結(jié)
以上是生活随笔為你收集整理的[C++STL]常用遍历算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10怎么新建网络映射驱动器?
- 下一篇: 怎么把视频的声音转成音频iphone怎么