STL算法(拷貝/替換)

簡介

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

將指定范圍內(nèi)所有操作結(jié)果為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

集合元素交換

  • 集合的大小也會根據(jù)實際情況發(fā)生自動改變。
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 << "構(gòu)造 " << number << " " << name.c_str() << endl;
        this->number = number;
        this->name = name;
    }
    Student(const Student & stu) {
        //cout << "copy構(gòu)造" <<stu.getNumber()<<" "<<stu.getName().c_str()<< endl;
        this->number = stu.getNumber();
        this->name = stu.getName();
    }
    ~Student() {
        //cout<<"析構(gòu) " << 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 << "結(jié)束遍歷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 << "結(jié)束遍歷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);
    //按仿函數(shù)條件進行替換
    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;
}

結(jié)果:

replace.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評論 19 139
  • STL算法部分主要由頭文件,,組成。要使用STL中的算法函數(shù)必須包含頭文件,對于數(shù)值算法須包含,中則定義了一些模板...
    eb51589b1211閱讀 676評論 0 1
  • 算法 頭文件 (STL算法部分主要由頭文件,,組成。要使用STL中的算法函數(shù)必須包含頭文件,對于數(shù)值算法須包含,中...
    Wancho閱讀 683評論 0 1
  • 讀書時,如果大聲讀出來,不但可以加深印象,還可以培養(yǎng)孩子的膽量哦。你家孩子讀書時,是在心里默念,還是大聲朗讀出來的...
    陽光所指閱讀 194評論 0 2
  • 11月7日 周一 晴 談一個老話題,什么是婚姻幸福! 一個人如果永遠都在不斷的向外索取,不去關(guān)注伴侶或...
    墨香盈袖堂主閱讀 377評論 7 6

友情鏈接更多精彩內(nèi)容