前面一节我们学习了转换算法 transform() 函数,读者是否还记得它的功能是什么吗?没错,transform() 函数能够对序列中的元素进行转换操作并将结果存储到目标位置。本节我们将学习替换算法——replace()、replace_if() 和 replace_copy() 函数。"replace" 意为"替换",顾名思义,这些函数能够对序列中满足条件的元素进行替换操作。

replace() 系列函数的语法格式如下:

template< class ForwardIt, class T >
void replace( ForwardIt first, ForwardIt last,
              const T& old_value, const T& new_value );
template< class ForwardIt, class UnaryPredicate, class T >
void replace_if( ForwardIt first, ForwardIt last,
                 UnaryPredicate p, const T& new_value );
template< class InputIt, class OutputIt, class T >
OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first,
                       const T& old_value, const T& new_value );

replace(first, last, old_value, new_value) 函数的功能是将区间 [first, last) 中所有等于 old_value 的元素替换为 new_value。

下面我们通过代码的方式实操一下 replace() 函数。

#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator> 
#include<string>
using namespace std;
/*replace()函数*/
void test() {
      
    vector<int> v{1, 2, 3, 2, 4, 2, 5}; 
     
    cout << "替换前:";
    for_each(v.begin(), v.end(), [](const int& n){
        cout << n << " ";
    });
    cout << '\n';
     
    /*将v容器中所有的2替换为0*/ 
    replace(v.begin(), v.end(), 2, 0);
     
    cout << "替换后:";
    for_each(v.begin(), v.end(), [](const int& n){
        cout << n << " ";
    });
    cout << '\n';
      
} 
int main(){
    system("title dotcpp.com");
    test();
    return 0;
}

编译结果如下:

replace()函数

通过 replace(first, last, old_value, new_value) 函数我们成功将序列 {1, 2, 3, 2, 4, 2, 5} 中的所有2替换为0,得到了新序列 {1, 0, 3, 0, 4, 0, 5},实现了对特定值的批量替换,输出完全符合我们的预期。

当然,我们并不止步于此,如果我们想要根据条件来替换元素,而不是固定的值,我们就可以使用 replace_if() 函数。

replace_if() 函数语法格式如下:

template< class ForwardIt, class UnaryPredicate, class T >
void replace_if( ForwardIt first, ForwardIt last,
                 UnaryPredicate p, const T& new_value );

replace_if(first, last, p, new_value) 函数表示将区间 [first, last) 中所有满足谓词 p 的元素替换为 new_value。

下面我们通过代码的方式来实操 replace_if() 函数:

#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator> 
#include<string>
using namespace std;
/*replace_if()函数*/
void test() {
       
    vector<int> v{1, 2, 3, 4, 5}; 
     
    cout << "替换前:";
    for_each(v.begin(), v.end(), [](const int& n){
        cout << n << " ";
    });
    cout << '\n';
     
    /*将v容器中所有的偶数替换为0*/ 
    replace_if(v.begin(), v.end(), [](int x) {
        return x % 2 == 0;
    }, 0);
     
    cout << "替换偶数后:";
    for_each(v.begin(), v.end(), [](const int& n){
        cout << n << " ";
    });
    cout << '\n';
  
} 
int main(){
    system("title dotcpp.com");
    test();
    return 0;
}

编译结果如下:

replace_if()函数

通过输出可以看出,对于序列 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},我们将所有偶数替换为0,得到了结果序列 {1, 0, 3, 0, 5, 0, 7, 0, 9, 0},输出完全符合我们的预期。

如果我们想要在替换的同时保留原序列不变,创建新的替换后的序列,我们就可以使用 replace_copy() 函数。

replace_copy() 函数语法格式如下:

template< class InputIt, class OutputIt, class T >
OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first,
                       const T& old_value, const T& new_value );

replace_copy(first, last, d_first, old_value, new_value) 函数表示将区间 [first, last) 中所有等于 old_value 的元素替换为 new_value,并将结果复制到以 d_first 开始的目标序列中,原序列保持不变。

下面我们通过代码的方式来实操 replace_copy() 函数:

#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator> 
#include<string>
using namespace std;
/*replace_copy()函数*/
void test() {
       
    vector<int> v1{1, 2, 3, 2, 4, 2, 5}; 
    vector<int> v2;
     
    cout << "原序列v1:";
    for_each(v1.begin(), v1.end(), [](const int& n){
        cout << n << " ";
    });
    cout << '\n';
     
    /*将v1中所有的2替换为0,结果存储到v2*/ 
    replace_copy(v1.begin(), v1.end(), back_inserter(v2), 2, 0);
     
    cout << "替换后v2:";
    for_each(v2.begin(), v2.end(), [](const int& n){
        cout << n << " ";
    });
    cout << '\n';
    cout << "原序列v1保持不变:";
    for_each(v1.begin(), v1.end(), [](const int& n){
        cout << n << " ";
    });
    cout << '\n';
      
} 
int main(){
    system("title dotcpp.com");
    test();
    return 0;
}

编译结果如下:

replace_copy()函数

通过输出可以看出,原序列 {1, 2, 3, 2, 4, 2, 5} 保持不变,而新序列 v2 中所有的2被替换为0,得到了结果序列 {1, 0, 3, 0, 4, 0, 5},输出完全符合我们的预期。

总结,如果我们想要在原序列中直接替换特定值,我们可以使用 replace() 函数;如果想要根据条件替换元素,我们可以使用 replace_if() 函数;如果想要保留原序列并创建替换后的新序列,我们可以使用 replace_copy() 函数;同时,在蓝桥杯、ACM等算法竞赛上,replace系列函数常应用于数据清洗、条件过滤和序列变换等场景。

点赞(0)

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

Dotcpp在线编译      (登录可减少运行等待时间)