C++ replace,replace_if和replace_copy函数用法详解(深入了解,一文学会)

 

C++ replace,replace_if和replace_copy函数用法详解目录

1 replace() 算法

2 replace_if()

3 replace_copy()


1 replace() 算法

会用新的值来替换和给定值相匹配的元素。它的前两个参数是被处理序列的正向迭代器,第 3 个参数是被替换的值,第 4 个参数是新的值。下面展示了它的用法:

#include <iostream>
#include <deque>
#include <vector>
#include <algorithm>
#include <list>
#include <set>
#include <functional>
#include <iterator>

using namespace std;



int main()
{
    std::deque<int> data{ 10, -5, 12, -6, 10, 8, -7, 10, 11 };
    std::replace(std::begin(data), std::end(data), 10, 99);
    // Result: 99 -5 12 -6 99 8 -7 99 11

    for (int i = 0; i < data.size(); i++)
    {
        cout << data[i] << " ";
    }
}

data 容器中和 10 匹配的全部元素都会被 99 替代。

2 replace_if()

会将使谓词返回 true 的元素替换为新的值。它的第 3 个参数是一个谓词,第 4 个参数是新的值。参数的类型一般是元素类型的 const 引用;const 不是强制性的,但谓词不应该改变元素。下面是一个使用 replace_if() 的示例:

#include <iostream>
#include <deque>
#include <vector>
#include <algorithm>
#include <list>
#include <set>
#include <functional>
#include <iterator>

using namespace std;



int main()
{
    string password{ "This is a good choice !" };
    std::replace_if(std::begin(password), std::end(password), [](char ch) {return std::isspace(ch); }, '_');
    //Result:This_is_a_good_choice!

    for (int i = 0; i < password.size(); i++)
    {
        cout << password[i] << " ";
    }
}

这个谓词会为任何是空格字符的元素返回 true,因此这里的空格都会被下划线代替。

3 replace_copy()

算法和 replace() 做的事是一样的,但它的结果会被保存到另一个序列中,而不会改变原始序列。它的前两个参数是输入序列的正向迭代器,第 3 个参数是输入序列的开始迭代器,最后两个参数分别是要被替换的值和替换值。例如:

#include <iostream>
#include <deque>
#include <vector>
#include <algorithm>
#include <list>
#include <set>
#include <functional>
#include <iterator>

using namespace std;



int main()
{
    std::vector<string> words{ "one","none", "two", "three", "none", "four" };
    std::vector<string> new_words;
    std::replace_copy(std::begin(words), std::end(words), std::back_inserter(new_words), string{ "none" }, string{ "0" });
    // Result:"one", "0", "two","three","0","four"

    for (int i = 0; i < new_words.size(); i++)
    {
        cout << new_words[i] << " ";
    }
}

 

在执行这段代码后,new_words 会包含注释中的 string 元素。 

可以在序列中有选择地替换元素的最后一个算法是 replace_copy_if(),它和 replace_if() 算法是相同的,但它的结果会被保存到另一个序列中。它的前两个参数是输入序列的迭代器,第 3 个参数是输出序列的开始迭代器,最后两个参数分别是谓词和替换值。例如:

#include <iostream>
#include <deque>
#include <vector>
#include <algorithm>
#include <list>
#include <set>
#include <functional>
#include <iterator>

using namespace std;



int main()
{
    std::deque<int> data{ 10, -5, 12, -6, 10, 8, -7, 10, 11 }; 
    std::vector<int> data_copy;
    std::replace_copy_if(std::begin(data), std::end(data), std::back_inserter(data_copy), [](int value) {return value == 10; }, 99);
    // Result:99 -5 12 -6 99 8 -7 99 11

    for (int i = 0; i < data_copy.size(); i++)
    {
        cout << data_copy[i] << " ";
    }
}

data_copy 是一个 vector 容器,这里使用它只是为了说明输出容器可以和输入容器不同。这段代码执行后,它会包含注释中所示的元素。 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

双子座断点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值