当前位置: 首页 > news >正文

(8)STL算法之替换

在STL算法中可以使用替换函数replace()、replace_if()完成替换操作。先来看下replace函数的底层实现。

// replace()  声明与实现
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,const T& old_value, const T& new_value);

template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,const T& old_value, const T& new_value)
{
    while (first!=last) 
    {
        if (*first == old_value) 
            *first=new_value;
        ++first;
    }
}

再来看看replace_if()的底层实现...

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

template < class ForwardIterator, class UnaryPredicate, class T >
void replace_if (ForwardIterator first, ForwardIterator last,UnaryPredicate pred, const T& new_value)
{
    while ( first != last ) 
    {
        if ( pred( *first ) ) 
            *first = new_value;
        ++first;
    }
}

越看越觉得C语言真牛逼

这两个函数都是有条件的替换,replace()是将容器[first, last]范围中与old_value相等的元素,替换成new_value;而replace_if()是将容器[first, last]范围中能使函数pred为真的元素,替换成new_value。下来看一个例子。

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>   //  bind2nd(less<int>(), 7);
using namespace std;

void main()
{
	vector<int> v;
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	v.assign(arr, arr + 9);
	for (int val : v)
		cout << val << " ";   // 1 2 3 4 5 6 7 8 9
	cout << endl;
	replace(v.begin(),v.end(),5,10); //将等于 5 的元素替换成 10
	for (int val : v)
		cout << val << " ";   // 1 2 3 4 10 6 7 8 9
	cout << endl;
	replace_if(v.begin(), v.end(), bind2nd(less<int>(), 7), 11);  //将小于 7 的元素换成 11
	for (int val : v)
		cout << val << " ";   // 11 11 11 11 10 11 7 8 9
	cout << endl;
}

===========================================================================================================================================

再加两个替换函数replace_copy()、replace_copy_if()

template <class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy (InputIterator first, InputIterator last,OutputIterator result,const T& old_value, const T& new_value);

template <class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy (InputIterator first, InputIterator last,OutputIterator result,const T& old_value, const T& new_value)
{
    while (first!=last) 
    {
        *result = (*first==old_value)? new_value: *first;
        ++first; 
        ++result;
    }
    return result;
}
template <class InputIterator, class OutputIterator, class UnaryPredicate, class T>
OutputIterator replace_copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred,const T& new_value);

template <class InputIterator, class OutputIterator, class UnaryPredicate, class T>
OutputIterator replace_copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred,const T& new_value)
{
    while (first!=last) 
    {
        *result = ( pred(*first)) ? new_value : *first;
        ++first; 
        ++result;
    }
    return result;
}

用一个例子来学会它

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

bool fun(int elem)
{
    return ((elem % 2 == 1));
}

int main() {
    int arr[] = { 10, 11, 12, 13, 14, 15, 11, 11 };
    vector<int> v1(8),v2;
    replace_copy(arr, arr + 8, v1.begin(), 11, 21);
    for (auto it = v1.begin(); it != v1.end(); ++it)
        cout << *it <<" ";    //  10 21 12 13 14 15 21 21
    cout << endl;
    v2.resize(v1.size());
    replace_copy_if(v1.begin(), v1.end(), v2.begin(), fun, 0);  //复制容器并将单数全部置 0 
    for (auto it = v2.begin(); it != v2.end(); ++it)
        cout << *it << " ";   //  10 0  12  0 14 0  0  0
    cout << endl;
    return 0;
}

 

相关文章:

  • (9)STL算法之逆转旋转
  • NFS安装使用
  • STL之map(关联式容器)
  • STL之unordered_map
  • 动态规划
  • 算法分析
  • 编写一个函数,实现将char类型的字符串,循环右移n个位置
  • 类构造、析构、赋值函数示例
  • 数组指针指针数组
  • LeeCode 20.有效的括号
  • LeeCode 26 删除排序数组中的重复项,返回数组新长度
  • LeeCode 27 移除元素,返回数组新长度
  • LeeCode 2125 合并两个(K个)有序链表
  • (10)STL算法之搜索(二) 二分查找
  • 单例模式(懒汉模式、饿汉模式)
  • 【mysql】环境安装、服务启动、密码设置
  • 2019.2.20 c++ 知识梳理
  • Android优雅地处理按钮重复点击
  • JS笔记四:作用域、变量(函数)提升
  • Linux各目录及每个目录的详细介绍
  • Map集合、散列表、红黑树介绍
  • php的插入排序,通过双层for循环
  • Python实现BT种子转化为磁力链接【实战】
  • React中的“虫洞”——Context
  • Redis中的lru算法实现
  • zookeeper系列(七)实战分布式命名服务
  • 阿里云Kubernetes容器服务上体验Knative
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 前端工程化(Gulp、Webpack)-webpack
  • 前端之Sass/Scss实战笔记
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 学习笔记:对象,原型和继承(1)
  • 怎样选择前端框架
  • AI算硅基生命吗,为什么?
  • MPAndroidChart 教程:Y轴 YAxis
  • 函数计算新功能-----支持C#函数
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • (30)数组元素和与数字和的绝对差
  • (DFS + 剪枝)【洛谷P1731】 [NOI1999] 生日蛋糕
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (一)为什么要选择C++
  • (转载)hibernate缓存
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。
  • .NET Framework 服务实现监控可观测性最佳实践
  • .net 后台导出excel ,word
  • .net 简单实现MD5
  • .vimrc php,修改home目录下的.vimrc文件,vim配置php高亮显示
  • /使用匿名内部类来复写Handler当中的handlerMessage()方法
  • [ IO.File ] FileSystemWatcher
  • [ 代码审计篇 ] 代码审计案例详解(一) SQL注入代码审计案例
  • [20140403]查询是否产生日志
  • [2015][note]基于薄向列液晶层的可调谐THz fishnet超材料快速开关——
  • [3300万人的聊天室] 作为产品的上游公司该如何?