美文网首页
STL算法(拷贝/替换)

STL算法(拷贝/替换)

作者: 第八区 | 来源:发表于2017-09-05 16:51 被阅读42次

    简介

    copy
    replace
    replace_if
    swap

    copy

    集合拷贝

    • 需要保证目的集合的空间足够
    template<class _InIt,
        class _OutIt> inline
        _OutIt copy(_InIt _First, _InIt _Last,
            _OutIt _Dest)
        {   // copy [_First, _Last) to [_Dest, ...)
        _DEPRECATE_UNCHECKED(copy, _Dest);
        return (_Copy_no_deprecate(_First, _Last, _Dest));
        }
    
     #if _ITERATOR_DEBUG_ARRAY_OVERLOADS
    template<class _InIt,
        class _OutTy,
        size_t _OutSize> inline
        _OutTy *copy(_InIt _First, _InIt _Last,
            _OutTy (&_Dest)[_OutSize])
        {   // copy [_First, _Last) to [_Dest, ...)
        return (_Unchecked(
            _Copy_no_deprecate(_First, _Last,
                _Array_iterator<_OutTy, _OutSize>(_Dest))));
        }
     #endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
    

    replace

    查找替换。

    template<class _FwdIt,
        class _Ty> inline
        void replace(_FwdIt _First, _FwdIt _Last,
            const _Ty& _Oldval, const _Ty& _Newval)
        {   // replace each matching _Oldval with _Newval
        _DEBUG_RANGE(_First, _Last);
        _Replace_unchecked(_Unchecked(_First), _Unchecked(_Last),
            _Oldval, _Newval);
        }
    

    replace_if

    将指定范围内所有操作结果为true的元素用新值替换。

    template<class _FwdIt,
        class _Pr,
        class _Ty> inline
        void replace_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred, const _Ty& _Val)
        {   // replace each satisfying _Pred with _Val
        _DEBUG_RANGE_PTR(_First, _Last, _Pred);
        _Replace_if_unchecked(_Unchecked(_First), _Unchecked(_Last),
            _Pred, _Val);
        }
    

    swap

    集合元素交换

    • 集合的大小也会根据实际情况发生自动改变。
    template<class _Ty,
        class _Alloc> inline
        void swap(vector<_Ty, _Alloc>& _Left, vector<_Ty, _Alloc>& _Right)
            _NOEXCEPT_OP(_NOEXCEPT_OP(_Left.swap(_Right)))
        {   // swap _Left and _Right vectors
        _Left.swap(_Right);
        }
    

    示例

    #include "stdafx.h"
    #include "stdafx.h"
    #include "iostream"
    #include "string"
    #include "algorithm"
    #include "vector"
    #include "list"
    #include <functional>
    using namespace std;
    class Student {
    private:
        int number;
        string name;
    public:
        Student() {
    
        }
        Student(int number, string name) {
            cout << "构造 " << number << " " << name.c_str() << endl;
            this->number = number;
            this->name = name;
        }
        Student(const Student & stu) {
            //cout << "copy构造" <<stu.getNumber()<<" "<<stu.getName().c_str()<< endl;
            this->number = stu.getNumber();
            this->name = stu.getName();
        }
        ~Student() {
            //cout<<"析构 " << this->number << " " << this->name.c_str() << endl;
        }
    
        Student& operator=(const Student& stu) {
            this->number = stu.getNumber();
            this->name = stu.getName();
            return *this;
        }
    
        void print()const {
            cout << "print 》》 " << this->number << " " << this->name.c_str() << endl;
        }
    
        int getNumber() const {
            return this->number;
        }
        string getName()const {
            return this->name;
        }
    };
    
    void printStuV(vector<Student> v) {
        cout << "开始遍历vector<Student>============" << endl;
        for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) {
            it->print();
        }
        cout << "结束遍历vector<Student>============" << endl;
    }
    void printNum(vector<int>v) {
        cout << "开始遍历vector<int>============" << endl;
        for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
            cout << *it << " ";
        }
        cout << endl;
        cout << "结束遍历vector<int>============" << endl;
    }
    
    struct ReplaceFunc
    {
        bool operator()(const Student & stu1) const {
            cout << "ReplaceFunc》》" << endl;
            return stu1.getNumber() >3;
        }
    };
    
    int main()
    {
    
        vector<int> vNum;
        vNum.push_back(1);
        vNum.push_back(3);
        vNum.push_back(5);
        vector<int> vNum2;
        vNum2.push_back(10);
        vNum2.push_back(11);
        vNum2.push_back(12);
        vNum2.push_back(13);
    
        copy(vNum.begin(), vNum.end(), vNum2.begin());
        printNum(vNum2);
        replace(vNum2.begin(), vNum2.end(), 3, 20);
        printNum(vNum2);
    
    
        vector<Student> v;
        vector<Student> v2;
        v.push_back(Student(1, "one"));
        Student stu2(2, "two");
        v.push_back(stu2);
        v.push_back(Student(4, "four"));
        v.push_back(Student(3, "three"));
        v.push_back(Student(5, "five"));
        v2.resize(v.size());
        v2.push_back(Student(6, "six"));
        //拷贝需要先保证容器的大小足够。
        copy(v.begin(), v.end(), v2.begin());
        printStuV(v2);
        //按仿函数条件进行替换
        replace_if(v2.begin(), v2.end(), ReplaceFunc(), Student(10, "replace to ten"));
        printStuV(v2);
        //集合交换,容器的大小也会变化
        swap(v, v2);
        cout << endl;
        printStuV(v2);
        printStuV(v);
        return 0;
    }
    

    结果:

    replace.png

    相关文章

      网友评论

          本文标题:STL算法(拷贝/替换)

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