STL范例

来源:互联网 发布:zabbix windows不稳定 编辑:程序博客网 时间:2024/05/16 08:01


#include <functional>#include <list>#include <iostream>#include <algorithm>//下文中的 for_each 在该头文件using namespace std;template <typename T>void print_elements(T elem){cout<<elem <<" ";}void (*pf)(int) = print_elements;//函式指标void main(){int ia[7] = {0,1,2,3,4,5,6};list<int> ilist(ia, ia+7);// 定义并初始化一个 list容器,以阵列作为 list 的初值for_each(ilist.begin(), ilist.end(), pf);// 0 1 2 3 4 5 6 对区间内每个元素调用传入的操作 pfilist.push_front(-1);ilist.push_back(7);ilist.push_back(0);ilist.push_back(7);ilist.push_back(9);for_each(ilist.begin(), ilist.end(),pf);// -1 0 1 2 3 4 5 6 7 0 7 9//ilist.remove_if(bind2nd(modulus<int>(), 2));// 去除所有奇数//for_each(ilist.begin(), ilist.end(), pf);}

#include <iostream>#include <string>#include <algorithm>#include <fstream>#include <iterator>using namespace std;int main(){string file_name;cout<<"Please enter a file to open:";cin>>file_name;if (file_name.empty() || !cin)//测试档名{cerr<<"Unable to read file name\n";return -1;}ifstream infile(file_name.c_str());if (!infile)//测试开档成功否{cerr<<"Unable to open"<<file_name<<endl;return -2;}istream_iterator<string> ins(infile), eos;ostream_iterator<string> outs(cout, "");copy(ins, eos, outs);//把档案内容 copy 到屏幕上,Copy 是一个泛型算法,它将文件中的内容显示到屏幕return 0;}


	
				
		
原创粉丝点击