前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >STL算法(拷贝/替换)简介copyreplacereplace_ifswap

STL算法(拷贝/替换)简介copyreplacereplace_ifswap

作者头像
用户2929716
发布2018-08-23 13:21:00
2830
发布2018-08-23 13:21:00
举报
文章被收录于专栏:流媒体流媒体

简介

copy replace replace_if swap

copy

集合拷贝

  • 需要保证目的集合的空间足够
代码语言:javascript
复制
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

查找替换。

代码语言:javascript
复制
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的元素用新值替换。

代码语言:javascript
复制
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

集合元素交换

  • 集合的大小也会根据实际情况发生自动改变。
代码语言:javascript
复制
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);
    }

示例

代码语言:javascript
复制
#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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.09.05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • copy
  • replace
  • replace_if
  • swap
    • 示例
    相关产品与服务
    容器服务
    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档