• 【C++11算法】find_if_not、 copy_if、copy_n



    前言

    C++11引入了许多方便且强大的算法,其中包括find_if_not、copy_if和copy_n。这些算法为我们提供了更高效、更简洁的方式来处理容器中的元素。本文将详细介绍这些算法的概念、使用方式以及提供多个示例代码,帮助读者更好地理解和运用这些算法。


    一、find_if_not(查找第一个不满足条件的元素):

    1.1概念

    find_if_not是C++标准库中的算法之一,在给定范围内查找第一个不满足指定条件的元素。
    使用方式:find_if_not接受一个迭代器范围和一个谓词函数作为参数,返回第一个不满足条件的元素的迭代器,如果没有找到满足条件的元素,则返回范围的结束迭代器。

    1.2示例代码

    #include 
    #include 
    #include 
    
    int main() {
        std::vector<int> numbers = {1, 2, 3, 4, 5};
    
        auto it = std::find_if_not(numbers.begin(), numbers.end(), [](int num) {
            return num % 2 == 0;
        });
    
        if (it != numbers.end()) {
            std::cout << "First odd number found: " << *it << std::endl;
        } else {
            std::cout << "No odd number found." << std::endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    这个示例代码中,我们使用find_if_not在给定的向量中查找第一个不是偶数的元素。谓词函数(num % 2 == 0)判断元素是否为偶数,find_if_not返回满足条件的第一个元素的迭代器,我们通过输出语句打印了结果。

    1.3更多示例

    #include 
    #include 
    #include 
    
    int main() {
        std::vector<int> numbers = {2, 4, 6, 7, 8, 10};
    
        auto it = std::find_if_not(numbers.begin(), numbers.end(), [](int num) {
            return num % 2 == 0;
        });
    
        if (it != numbers.end()) {
            std::cout << "First odd number found: " << *it << std::endl;
        } else {
            std::cout << "No odd number found." << std::endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    输出:

    First odd number found: 7
    
    • 1

    在这个示例中,我们在给定的向量中查找第一个不是偶数的元素。由于向量中存在满足条件的元素7,因此输出了相应的结果。

    这是find_if_not的基本用法,可以通过传递不同的谓词函数来查找不同的元素。

    二、copy_if(复制满足条件的元素):

    2.1概念

    copy_if是C++标准库中的算法之一,用于将满足指定条件的元素复制到另一个容器中。

    2.2使用方式

    copy_if接受一个迭代器范围、一个目标容器的起始位置和一个谓词函数作为参数,将满足条件的元素复制到目标容器中。

    2.3示例代码

    #include 
    #include 
    #include 
    
    int main() {
        std::vector<int> source = {1, 2, 3, 4, 5};
        std::vector<int> destination;
    
        std::copy_if(source.begin(), source.end(), std::back_inserter(destination), [](int num) {
            return num % 2 == 0;
        });
    
        std::cout << "Copied elements: ";
        for (int num : destination) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    这个示例代码中,我们使用copy_if从源向量中复制所有的偶数到目标向量。谓词函数(num % 2 == 0)判断元素是否为偶数,通过std::back_inserter将元素插入到目标容器中。

    输出:

    Copied elements: 2 4
    
    • 1

    2.4更多示例

    #include 
    #include 
    #include 
    
    int main() {
        std::vector<int> source = {1, 2, 3, 4, 5};
        std::vector<int> destination;
    
        std::copy_if(source.begin(), source.end(), std::back_inserter(destination), [](int num) {
            return num > 2;
        });
    
        std::cout << "Copied elements: ";
        for (int num : destination) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    输出:

    Copied elements: 3 4 5
    
    • 1

    在这个示例中,我们使用copy_if从源向量中复制所有大于2的元素到目标向量。

    2.5优势

    copy_if提供了一个简单的方式来选择性地复制容器中的元素,我们只需通过传递不同的谓词函数来修改复制的条件。

    三、copy_n(复制前N个元素):

    3.1概念

    copy_n是C++标准库中的算法之一,用于在指定的元素数目内从一个位置复制元素到另一个位置。

    3.2使用方式

    copy_n接受一个源容器的起始位置、需要复制的元素数目和目标容器的起始位置作为参数,将源容器中指定数目的元素复制到目标容器中。

    3.3示例代码

    #include 
    #include 
    #include 
    
    int main() {
        std::vector<int> source = {1, 2, 3, 4, 5};
        std::vector<int> destination(3);
    
        std::copy_n(source.begin(), 3, destination.begin());
    
        std::cout << "Copied elements: ";
        for (int num : destination) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    这个示例代码中,我们使用copy_n从源向量中复制前3个元素到目标向量的前3个位置。

    输出:

    Copied elements: 1 2 3
    
    • 1

    3.4更多示例

    #include 
    #include 
    #include 
    
    int main() {
        std::vector<int> source = {1, 2, 3, 4, 5};
        std::vector<int> destination(5);
    
        std::copy_n(source.begin() + 1, 4, destination.begin() + 1);
    
        std::cout << "Copied elements: ";
        for (int num : destination) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    输出:

    Copied elements: 0 2 3 4 5
    
    • 1

    在这个示例中,我们使用copy_n从源向量中复制从第二个元素开始的连续4个元素到目标向量的第二个位置。

    3.5优势

    copy_n为我们提供了一种方便的方法来复制指定数目的元素。我们可以通过修改源容器的起始位置和目标容器的起始位置以及指定复制的元素数目来达到不同的复制需求。


    总结

    在本文中,我们介绍了C++11中的find_if_not、copy_if和copy_n这三个算法。find_if_not用于查找第一个不满足条件的元素,copy_if用于复制满足条件的元素,copy_n用于复制指定数目的元素。这些算法可以帮助我们更高效地处理容器中的元素,并提供了简洁的接口来满足不同的需求。

    通过多个示例代码,我们展示了这些算法的基本用法,并通过不同的谓词函数和参数配置,展示了这些算法的灵活性和可定制性。当在实际代码中遇到需要查找满足特定条件的元素或复制一部分元素的需求时,这些算法将为我们提供便利而高效的解决方案。

    了解和熟练掌握这些算法可以提高我们的编程能力,并使我们的代码更加简洁和易于管理。在实际工作中,建议多加练习和应用这些算法,并深入阅读C++标准库的相关文档,以掌握更多的算法和库函数,提高自己的编程水平。

    希望本文能够帮助读者理解和运用find_if_not、copy_if和copy_n这三个算法,提升你的C++编程技能。祝你编程愉快!

  • 相关阅读:
    Linux系统之编译安装python3
    百度测开初面面试题分享
    Freertos学习第三天-ESP32基于Freertos任务共享全局变量
    spark hdfs azure对象存储
    【信号处理】基于优化算法的 SAR 信号处理(Matlab代码实现)
    常见项目管理中npm包操作总结
    【单线图的系统级微电网仿真】基于 PQ 的可再生能源和柴油发电机组微电网仿真(Simulink)
    功能基础篇8——图形用户界面
    基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的零售柜商品检测软件(Python+PySide6界面+训练代码)
    常见插件 tomcat插件
  • 原文地址:https://blog.csdn.net/m0_62599305/article/details/133574927