C++ STL常用算法-65-拷贝和替换算法

继续学习C++中常用的算法基本使用,这篇学习拷贝和替换算法,一共有四个,用来操作容器中指定范围的元素的拷贝和替换操作。

算法简介
copy          //容器内指定范围的元素拷贝到另一容器中
replace      //将容器内指定范围的旧元素修改为新元素
replace_if  //容器内指定范围满足条件的元素替换为新元素
swap    //互换两个容器的元素

 

1.copy 拷贝算法

函数原型:copy(iterator beg, iterator end, iterator dst);

参数解释:

第一个参数容器1的迭代器开始位置,参数2 容器1的迭代器结束位置,参数3 目标容器的迭代器的开始位置

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


void Print01(int val)
{
    cout << val << " ";
};

void test01()
{
    vector<int> v1;
    v1.push_back(10);
    v1.push_back(50);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(20);

    // copy 拷贝算法
    vector<int> v2;
    v2.resize(v1.size());
    copy(v1.begin(), v1.end(), v2.begin());
    //打印v2容器内容
    for_each(v2.begin(), v2.end(), Print01);
    cout << endl;

}

int main()
{
    test01();
    system("pause");
    return 0;
}

运行结果:

 

2.replace 替换算法

替换算法就是在指定范围内把旧元素替换成新元素,例如在一个数列中,把数字5替换成55.

函数原型:replace(iterator beg, iterator end, oldValue, newValue);

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


void Print01(int val)
{
    cout << val << " ";
};

void test01()
{
    vector<int> v1;
    v1.push_back(10);
    v1.push_back(5);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(5);

    // replace 替换算法
    replace(v1.begin(), v1.end(), 5, 55);
    //打印v2容器内容
    for_each(v1.begin(), v1.end(), Print01);
    cout << endl;

}

int main()
{
    test01();
    system("pause");
    return 0;
}

测试结果:

注意,如果填写要替换的值数列中找不到,改替换函数是不起作用的。

 

3.replace_if 条件替换算法

函数原型:replace_if(iterator beg, iterator end, _Pred, newValue);

第三个函数是一个谓词,也就是返回类型是bool的仿函数,普通函数这里不行

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


class MyClass
{
public:
    bool operator()(int val)
    {
        return val < 30;
    }
};

void Print01(int val)
{
    cout << val << " ";
};

void test01()
{
    vector<int> v1;
    v1.push_back(10);
    v1.push_back(15);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(5);

    // replace_if 替换算法, 把小于30的元素都替换成200
    replace_if(v1.begin(), v1.end(), MyClass(), 200);
    //打印v2容器内容
    for_each(v1.begin(), v1.end(), Print01);
    cout << endl;

}

int main()
{
    test01();
    system("pause");
    return 0;
}

代码运行结果:

 

4.swap 交换算法

这个前面也学习过,作用就是互换两个容器的元素

函数原型:swap(容器1,容器2);

注意事项:

1.两个容器必须是同类型

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


class MyClass
{
public:
    bool operator()(int val)
    {
        return val < 30;
    }
};

void Print01(int val)
{
    cout << val << " ";
};

void test01()
{
    vector<int> v1;
    vector<int> v2;
    for(int i=0; i < 10; i++)
    {
        v1.push_back(i);
        v2.push_back(i+100);
    }

    cout << "before merge。。" << endl;
    for_each(v1.begin(), v1.end(), Print01);
    cout << endl;
    for_each(v2.begin(), v2.end(), Print01);
    cout << endl;
    cout << "==============================" << endl;
    // swap算法,合并两个容器
    swap(v1, v2);
    for_each(v1.begin(), v1.end(), Print01);
    cout << endl;
    for_each(v2.begin(), v2.end(), Print01);
    cout << endl;

}

int main()
{
    test01();
    system("pause");
    return 0;
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值