zoukankan      html  css  js  c++  java
  • 泛型算法(九)之替换算法

    1、replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value):把序列中为给定值的元素替换为新值

        std::vector<int> c;
        c.reserve(10);
        //向c中添加元素
        for (int i = 0; i < 10; i++)
        {
            c.push_back(i);
        }
        //把序列c中值等于5的元素替换成100
        std::replace(c.begin(), c.end(), 5, 100);
        //输出c
        for (auto var : c)
        {
            std::cout << var << ",";
        }
        //打印结果:0,1,2,3,4,100,6,7,8,9,

    2、replace_if(ForwardIterator first, ForwardIterator last, UnaryPredicate pred, const T& new_value):把序列中满足给定谓词pred的元素替换为新值

        std::vector<int> c;
        c.reserve(10);
        //向c中添加元素
        for (int i = 0; i < 10; i++)
        {
            c.push_back(i);
        }
        //把序列c中值大于5的元素替换成100
        std::replace_if(c.begin(), c.end(), [](int element){
            return element > 5;
        }, 100);
        //输出c
        for (auto var : c)
        {
            std::cout << var << ",";
        }
        //打印结果:0,1,2,3,4,5,100,100,100,100,

    3、replace_copy(InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value)复制序列,对于等于老值的元素复制时使用新值

        std::vector<int> c;
        std::vector<int> result;
        c.reserve(10);
        result.resize(10);
        //向c中添加元素
        for (int i = 0; i < 10; i++)
        {
            c.push_back(i);
        }
        //复制c到result中,对于c中等于5的元素在复制时用100代替
        std::replace_copy(c.begin(), c.end(), result.begin(), 5, 100);
        //输出result
        for (auto var : result)
        {
            std::cout << var << ",";
        }
        //打印结果:0,1,2,3,4,100,6,7,8,9,

    4、replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred, const T& new_value):复制序列,对于满足给定谓词pred的元素复制新值

        std::vector<int> c;
        std::vector<int> result;
        c.reserve(10);
        result.resize(10);
        //向c中添加元素
        for (int i = 0; i < 10; i++)
        {
            c.push_back(i);
        }
        //复制c到result中,对于c中大于5的元素在复制时用100代替
        std::replace_copy_if(c.begin(), c.end(), result.begin(), [](int element){
            return element > 5;
        }, 100);
        //输出result
        for (auto var : result)
        {
            std::cout << var << ",";
        }
        //打印结果:0,1,2,3,4,5,100,100,100,100,
  • 相关阅读:
    clickhouse 多数据源
    maven-dependency-plugin maven-assembly-plugin
    maven shade plugin
    远程服务器,无法复制粘贴 (通过mstsc复制粘贴失败需要重新启动RDP剪切板监视程序rdpclip.exe)
    Mysql导入大sql文件方法
    MySQL5.7更新json类型字段中的某个key的值 函数json_replace()
    mysq json类型
    增强mybatis-plus的typeHandler,可以支持List<T> 中嵌套对象
    Windows中查看端口占用及关闭对应进程
    Hibernate中继承体现
  • 原文地址:https://www.cnblogs.com/dongerlei/p/5142065.html
Copyright © 2011-2022 走看看