九、STL算法-变值算法(copy、transform)
来源:互联网 发布:怎样把nginx部署到公网 编辑:程序博客网 时间:2024/05/01 20:27
STL算法-变值算法(copy、transform)
一、变值算法
此类算法会修改源区间或目标区间元素的值,值被修改的那个区间,不可以是属于关联容器的。
二、程序示例
#include <iostream>#include <algorithm>#include <vector>#include <iterator>using namespace std;class outputSqure{ public: int operator()(int n) { return n*n; }};class outputSqure2{ public: void operator()(int n) { cout<<n*n<<" "; }};int main(){ int a[] = {5, 7, 8, 2, 4, 1, 6, 3, 9, 0}; vector<int> v(a, a+10); vector<int> result(10); transform(v.begin(), v.end(), result.begin(), outputSqure()); sort(result.begin(), result.end(), less<int>()); vector<int>::iterator i; for(i=result.begin(); i!=result.end(); i++) cout<<*i<<" "; cout<<endl<<endl; sort(v.begin(), v.end(), less<int>()); for_each(v.begin(), v.end(), outputSqure2()); cout<<endl<<endl; ostream_iterator<int> output(cout, " "); copy(v.begin(), v.end(), output); return 0;}
输出:
0 1 4 9 16 25 36 49 64 81
0 1 4 9 16 25 36 49 64 81
0 1 2 3 4 5 6 7 8 9
ostream_iterator<int> output(cout ,“ ”);//定义了一个 ostream_iterator<int> 对象,//可以通过cout输出以" "(空格) 分隔的一个个整数copy (v.begin(), v.end(), output);//导致v的内容在output上输出
三、copy函数模板
3.1 copy 的源代码
template<class _II, class _OI>inline _OI copy(_II _F, _II _L, _OI _X){ for (; _F != _L; ++_X, ++_F) *_X = *_F; return (_X);}
3.2 程序填空题
#include <iostream>#include <algorithm>#include <iterator>#include <fstream>using namespace std;//在此处补充你的代码int main(){ int a[] = {1, 2, 3, 4}; My_ostream_iterator<int> oit(cout, " "); ostream_iterator<int> oit(cout, " "); copy(a, a+4, oit); ////输出 1*2*3*4* ofstream oFile("test.txt", ios::app); My_ostream_iterator<int> oitf(oFile, " "); copy(a, a+4, oitf); ////向test.txt文件中写入 1*2*3*4* oFile.close(); return 0;}
如何编写My_ostream_iterator?
上面程序中调用语句 “copy( a,a+4,oit)” 实例化后得到copy如下:
My_ostream_iterator<int> copy(int * _F, int * _L, My_ostream_iterator<int> _X){ for (; _F != _L; ++_X, ++_F) *_X = *_F; return (_X);}
由此可知,My_ostream_iterator至少要重载*、=、++三个运算符,即:
template <class T>class My_ostream_iterator:public iterator<output_iterator_tag, T>{ private: string sep; ostream & os; public: My_ostream_iterator(ostream & os, string s):sep(s), os(os) { } void operator++() { } My_ostream_iterator & operator * () { return * this; } My_ostream_iterator & operator = (const T & val){ os<<val<<sep; return *this; }};
0 0
- 九、STL算法-变值算法(copy、transform)
- STL 算法之transform
- C++STL-算法transform
- C++ STL 算法:transform
- STL算法 ------- transform()
- STL算法(18)-transform()
- C++ STL算法transform
- C++--STL--transform算法
- STL-transform算法
- 九、STL算法-变序算法(random_shuffle、reverse、next_permutation)
- STL中copy算法
- STL算法之copy
- 【STL】算法 — copy
- stl之算法copy
- stl算法copy
- STL algorithm算法transform(57)
- STL遍历算法 “for_each ”,“transform”
- STL-变序算法(2)
- Android性能优化之ViewStub
- UVA - 1586 Molar mass?
- UVA - 1588 Kickdown
- 自定义TTF多语言版本之台湾繁体
- UVA - 10361 Automatic Poetry
- 九、STL算法-变值算法(copy、transform)
- Cocos2D-ObjC:在RPG游戏中混合Swift代码
- UVA - UVA - 537 Artificial
- 基于Yarn平台的MapReduce运行机制
- UVA - 409 Excuses, Excuses!
- UVA - 10878 Decode the tape
- NOIP提高组A组7.7模拟总结
- Dynamic len
- UVA - 644 Immediate Decodability