STL算法(for_each/transform)

for_each

用于逐个遍历容器元素,它对迭代器区间[first,last)所指的每一个元素,执行由单参数函数对象f所定义的操作。方法返回函数对象。

函数的定义:

template inline
    _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
    {   // perform function for each element
    _DEBUG_RANGE_PTR(_First, _Last, _Func);
    _For_each_unchecked(_Unchecked(_First), _Unchecked(_Last), _Func);
    return (_Func);
    }

示例:

#include "stdafx.h"
#include "algorithm"
#include "iostream";
using namespace std;
#include "vector";

//函数对象
struct PrintV {
private:
    int count;
public:
    void operator()( int & value) {
        count++;
        value++;
        cout << value << " ";
    }
    void print() {
        cout << "调用次数 " << count << endl;
    }
};
void print(vector v) {
    for (vector::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << "  ";
    }
}
int main()
{
    vector v;
    v.push_back(1);
    v.push_back(3);
    v.push_back(2);
    v.push_back(5);
    v.push_back(4);
    print(v);
    cout << endl;
    PrintV pv=for_each(v.begin(), v.end(), PrintV());
    cout << endl;
    pv.print();
    print(v);
    return 0;
}
STL算法(for_each/transform)_第1张图片
for_each

transform

函数定义

template inline
    _OutIt transform(_InIt _First, _InIt _Last,
        _OutIt _Dest, _Fn1 _Func)
    {   // transform [_First, _Last) with _Func
    _DEPRECATE_UNCHECKED(transform, _Dest);
    return (_Transform_no_deprecate(_First, _Last, _Dest, _Func));
    }

 #if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template inline
    _OutTy *transform(_InIt _First, _InIt _Last,
        _OutTy (&_Dest)[_OutSize], _Fn1 _Func)
    {   // transform [_First, _Last) with _Func, array dest
    return (_Unchecked(
        _Transform_no_deprecate(_First, _Last,
            _Array_iterator<_OutTy, _OutSize>(_Dest), _Func)));
    }
 #endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */

有两个重载函数,一个可以传入一元谓词,一个可以传入二元谓词。

与for_each区别
  • for_each的仿函数可以返回void。transform的仿函数必须返回值。
  • for_each的仿函数的参数是传入的引用。transform的仿函数的参数最好是值传递
    示例
struct TranFunc2 {
private:
    int count;
public:
    int operator()(const int & left,const int & right) {
        count++;
        cout << "left right " << left << "  " << right << endl;
        return left+right;
    }
    void print() {
        cout << "调用次数 " << count << endl;
    }
};
void print(vector v) {
    for (vector::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}
int main()
{
    vector v;
    v.push_back(1);
    v.push_back(3);
    v.push_back(2);
    v.push_back(5);
    v.push_back(4);
    vector v1=v;
    v1.push_back(100);
    cout << "遍历v" << endl;
    print(v);
    cout << "遍历v1" << endl;
    print(v1);
    vector::iterator it=transform(v.begin(), v.end(), v1.begin(), TranFunc());
    //v1的元素比v多一个所以,当执行transform后,v
    //的迭代器到了末尾,而v1并没有
    cout << "it==v1.end():" << (it==v1.end()) << endl;
    print(v1);
    //遍历v从begin到end。同时从v1的begin开始。最后
    //结果存入到v中,从v的begin开始,结果返回v的迭代器
    it=transform(v.begin(), v.end(), v1.begin(), v.begin(), TranFunc2());
    //返回v的迭代器,v的迭代器已到末尾。
    cout << "it==v1.end():" << (it == v.end()) << endl;
    print(v);
    return 0;
}

结果

STL算法(for_each/transform)_第2张图片
transform.png

你可能感兴趣的:(STL算法(for_each/transform))