C++大学教程(第七版)Chapter22.5标准模板库-算法:Fig22_29.cpp

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

继续学习STL算法,本文主要讲:

replace:用什么元素替代另外一个元素,比如说把10替代为100。

replace_if:如果满足一个条件,就做一个替换,比如说如果大于9,就替换为100。

replace_copy:先做替换,然后copy到另外一个地方。

replace_copy_if:满足条件,就做替换,然后copy到另外一个地方。


// Fig22_29_STL_Alg_replace_replace_if.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>//ostream_iterator
using namespace std;
bool greater9(int);//prototype

int main()
{
    const int SIZE = 10;
    int a[SIZE] = { 10,2,10,4,16,6,14,8,12,10 };
    ostream_iterator<int> output(cout, " ");
    vector<int> v1(a, a + SIZE);//copy of a
    cout << "Vector v1 before replacing all 10s:\n";
    copy(v1.begin(), v1.end(), output);

    //replace all 10s in v1 with 100
    replace(v1.begin(),v1.end(),10,100);
    cout << "\nVector v1 after replacing  all 10s with 100:\n";
    copy(v1.begin(), v1.end(), output);

    ///
    vector<int> v2(a, a + SIZE);//copy of a
    vector<int> c1(SIZE);//实例化 vector c1
    cout << "\n\nVector v2 before removing all 10s and copying:\n";
    copy(v2.begin(), v2.end(), output);
    //copy from v2 to c1,replacing 10 with 100
    replace_copy(v2.begin(), v2.end(),c1.begin(),10,100);//替换10为100,然后拷贝到c1.begin()处
    cout << "\nVector c1 after replacing all 10 s with 100 in v2:\n";
    copy(c1.begin(), c1.end(), output);
    ///

    vector<int> v3(a, a + SIZE);//copy of a    
    cout << "\n\nVector v3 before replacing values greater than 9:\n";
    copy(v3.begin(), v3.end(), output);
    //replacing values greater than 9 in v3 with 100
    replace_if(v3.begin(),v3.end(),greater9,100);
    cout << "\n\nVector v3 after replacing all values greater than 9 with 100:\n";
    copy(v3.begin(), v3.end(), output);
    //
    vector<int> v4(a, a + SIZE);//copy of a
    vector<int> c2(SIZE);//实例化 vector c2
    cout << "\n\nVector v4 before replacing all values greater than 9 and copying:\n";
    copy(v4.begin(), v4.end(), output);
    //copy from v4 to c2,replacing elements greater than 9 with 100
    replace_copy_if(v4.begin(), v4.end(), c2.begin(), greater9, 100);//替换10为100,然后拷贝到c1.begin()处
    cout << "\n\nVector v4 after replacing all values greater than 9 and copying:\n";
    copy(c2.begin(), c2.end(), output);
}
bool greater9(int x)
{
    return x > 9;
}



运行结果:
 

Vector v1 before replacing all 10s:
10 2 10 4 16 6 14 8 12 10
Vector v1 after replacing  all 10s with 100:
100 2 100 4 16 6 14 8 12 100

Vector v2 before removing all 10s and copying:
10 2 10 4 16 6 14 8 12 10
Vector c1 after replacing all 10 s with 100 in v2:
100 2 100 4 16 6 14 8 12 100

Vector v3 before replacing values greater than 9:
10 2 10 4 16 6 14 8 12 10

Vector v3 after replacing all values greater than 9 with 100:
100 2 100 4 100 6 100 8 100 100

Vector v4 before replacing all values greater than 9 and copying:
10 2 10 4 16 6 14 8 12 10

Vector v4 after replacing all values greater than 9 and copying:
100 2 100 4 100 6 100 8 100 100

总结

replace:用什么元素替代另外一个元素,比如说把10替代为100。

replace_if:如果满足一个条件,就做一个替换,比如说如果大于9,就替换为100。

replace_copy:先做替换,然后copy到另外一个地方。

replace_copy_if:满足条件,就做替换,然后copy到另外一个地方。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值