您的位置:首页 > 其它

常用STL算法1_遍历

2017-04-24 23:31 615 查看
#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_each
void 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: