博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STL: for_each
阅读量:2297 次
发布时间:2019-05-09

本文共 1345 字,大约阅读时间需要 4 分钟。

#include 
#include
#include
#include
using namespace std; class GenByTwo { public: GenByTwo (int _seed = 0):seed(_seed){} int operator() () {return seed += 2;} private: int seed; }; void operate(int &rhs) { cout << rhs << " "; rhs += 1; } int main(int argc, char* argv[]) { vector
v(10); generate(v.begin(), v.end(), GenByTwo(2)); for_each(v.begin(), v.end(), operate); std::cout << std::endl; //print copy(v.begin(), v.end(), ostream_iterator
(cout, " ")); return 0; }

4 6 8 10 12 14 16 18 20 22

5 7 9 11 13 15 17 19 21 23  

#include 
// std::cin, std::cout#include
// std::istream_iterator#include
#include
#include
#include
#include
using namespace std;map
m;void print( const pair
&r){ cout << r.first << " " << r.second << endl;}void record( const string &s){ m[ s ] ++;}int main(){ double beginTime = clock(); fstream in("test.txt"); istream_iterator
ii(in); istream_iterator
eos; for_each(ii,eos,record); for_each( m.begin(), m.end(), print); double endTime = clock(); cout << " time: " << endTime - beginTime << " ms" << endl; return 0;}
time: 66 ms



转载地址:http://balnb.baihongyu.com/

你可能感兴趣的文章
vi run as root gives error accessing .exrc
查看>>
Lessons from Google Wave and REST vs. SOAP: Fighting Complexity of our own Choosing
查看>>
Unix cut command
查看>>
Splashtop Remote——让你在iPad上也能用Windows下的应用
查看>>
Linux下如何删除已安装的realplayer
查看>>
Tcl/Tk的Switch模块中慎用注释
查看>>
How to Clone VMs in ESXi
查看>>
How to turn on RSH and RLOGIN in Linux?
查看>>
Ksh中的$*和$@
查看>>
Java 理论与实践: 使用通配符简化泛型使用 (二)
查看>>
Java深度历险(三)——Java线程:基本概念、可见性与同步
查看>>
一致性hash算法 - consistent hashing
查看>>
java文件中为什么会有serialVersionUID
查看>>
总结一致性哈希(Consistent Hashing)
查看>>
回滚段探究
查看>>
Oracle Redo and Undo
查看>>
Oracle Flashback 闪回查询功能操作范例(9i and 10g)
查看>>
parentNode、parentElement,childNodes、children 它们有什么
查看>>
prototype.js用法
查看>>
MySQL实用命令
查看>>