美文网首页
STL算法(for_each/transform)

STL算法(for_each/transform)

作者: 第八区 | 来源:发表于2017-08-31 17:52 被阅读23次

    for_each

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

    函数的定义:

    template<class _InIt,
        class _Fn1> 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<int> v) {
        for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
            cout << *it << "  ";
        }
    }
    int main()
    {
        vector<int> 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;
    }
    
    for_each

    transform

    函数定义

    template<class _InIt,
        class _OutIt,
        class _Fn1> 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<class _InIt,
        class _OutTy,
        size_t _OutSize,
        class _Fn1> 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<int> v) {
        for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
            cout << *it << "  ";
        }
        cout << endl;
    }
    int main()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(3);
        v.push_back(2);
        v.push_back(5);
        v.push_back(4);
        vector<int> v1=v;
        v1.push_back(100);
        cout << "遍历v" << endl;
        print(v);
        cout << "遍历v1" << endl;
        print(v1);
        vector<int>::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;
    }
    

    结果

    transform.png

    相关文章

      网友评论

          本文标题:STL算法(for_each/transform)

          本文链接:https://www.haomeiwen.com/subject/ikfcjxtx.html