常用STL算法1_遍历
来源:互联网 发布:js canva绘制渐变色线 编辑:程序博客网 时间:2024/06/06 00:07
#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <vector>#include <list>#include <algorithm>#include <functional>#include <string>#include <iterator> //输出流using namespace std;//函数对象struct MyShow{public: MyShow() { m_count = 0; } void operator()(const int & elem) { cout<<elem<<" "; m_count++; } void printCount() { cout<<"m_count:"<<m_count<<endl; }private: int m_count;};//一般情况下,for_each所使用的函数对象,参数是引用,没有返回值//回调函数void show(const int &iItem){ cout << iItem << " ";}//for_eachvoid main061_for_each(){ int iArray[] = {1,2,3,4,5}; vector<int> vecInt(iArray,iArray+sizeof(iArray)/sizeof(iArray[0])); //1. 回调函数 for_each(vecInt.begin(), vecInt.end(), show); cout<<endl; //2. 函数对象 for_each(vecInt.begin(), vecInt.end(), MyShow()); cout<<endl; //3. 函数对象可以记录状态 for_each返回值:_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func) MyShow myshow; //for_each(vecInt.begin(), vecInt.end(), myshow);//no_ok //myshow = for_each(vecInt.begin(), vecInt.end(), myshow);//ok myshow = for_each(vecInt.begin(), vecInt.end(), MyShow());//ok cout<<endl; cout<<endl; myshow.printCount(); /* _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func) { // perform function for each element _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Func); _For_each(_Unchecked(_First), _Unchecked(_Last), _Func); return (_STD move(_Func)); } */}//Transform所使用的函数对象,参数一般不使用引用,而是使用元素,并且有返回值int op_increase2 (int i) { return ++i; } int op_sum2 (int i, int j) { return i+j; } template <typename T>void printVectorElem(T &v){ T::iterator it; for (it=v.begin(); it!=v.end(); ++it) { cout <<*it <<" "; } cout << endl; }//transform 两种应用void main062_transform(){ vector<int> first; vector<int> second; vector<int>::iterator it; // 1.set some values: for (int i=1; i<6; i++) { first.push_back (i*10); // first: 10 20 30 40 50 } second.resize(first.size()); // allocate space //2. 一元函数 回调 //_OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func) // transform [_First, _Last) with _Func transform (first.begin(), first.end(), second.begin(), op_increase2); //回调 // second: 11 21 31 41 51 //2. 二元函数 回调 //_OutIt transform(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _OutIt _Dest, _Fn2 _Func) // transform [_First1, _Last1) and [_First2, ...) with _Func transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum2); //回调 // first: 21 41 61 81 101 cout << "first contains:"; printVectorElem(first);; //3. 预定义函数对象 /* struct negate: public unary_function<_Ty, _Ty> { // functor for unary operator- _Ty operator()(const _Ty& _Left) const { // apply operator- to operand return (-_Left); } }; */ transform(first.begin(), first.end(), first.begin(), negate<int>()); cout<<"负数:"; printVectorElem(first); //4. 函数适配器 list<int> list1; list1.resize(first.size()); /* struct multiplies: public binary_function<_Ty, _Ty, _Ty> { // functor for operator* _Ty operator()(const _Ty& _Left, const _Ty& _Right) const { // apply operator* to operands return (_Left * _Right); } }; */ transform(first.begin(), first.end(), list1.begin(), bind2nd(multiplies<int>(),10)); cout<<"适配器(x10):"; printVectorElem(list1); //5 输出到屏幕 ostream_iterator<int>(cout, " ") transform(first.begin(),first.end(),ostream_iterator<int>(cout, " "), negate<int>());}void main063_for_each_pk_transform(){ //一般情况下,for_each所使用的函数对象,参数是引用,没有返回值 //Transform所使用的函数对象,参数一般不使用引用,而是使用元素,并且有返回值 /* transform()所需要的函数对象要求有返回值*/ /* _OutIt _Transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func) { // transform [_First, _Last) with _Func for (; _First != _Last; ++_First, ++_Dest) { *_Dest = _Func(*_First);//返回值 } return (_Dest); } */}int main(){ //main061_for_each(); main062_transform(); main063_for_each_pk_transform(); cout<<"\nhello"<<endl; system("pause"); return 0;}
0 0
- 常用STL算法1_遍历
- stl的string的典型操作_初始化_遍历_连接_和字符指针转化_查找替换_删除_插入_常用算法
- stl常用算法_算法和生成
- C++ STL/ (13) 常用遍历算法
- stl常用算法_查找相关
- stl常用算法_排序相关
- stl常用算法_集合运算
- 常用STL算法2_查找
- 常用STL算法3_排序
- 常用STL算法6_集合
- STL常用遍历算法for_each和transform的比较
- stl常用算法_拷贝和替换相关
- 常用STL算法4_拷贝和替换
- 常用STL算法5_算术和生成
- STL算法常用算法
- STL遍历算法 “for_each ”,“transform”
- 44STL之遍历算法
- STL常用容器和算法1
- 第2章 IPC机制
- 基于 Django1.10 文档的深入学习(6)—— Translation 之 short_description
- java注解
- Kaggle入门——使用scikit-learn解决DigitRecognition问题
- 详解SQLite中的数据类型
- 常用STL算法1_遍历
- nandflash控制器
- DeepLearning tutorial(1)Softmax回归原理简介+代码详解
- Joda-Time的使用以及其与jdk中时间类型的相互转换
- codeforces 779A
- springboot项目创建问题
- 有符号整数的移位运算
- Download data from new EOSDIS system
- 一个工作分配的算法解析