• 在 C++ STL 中复制的不同方法 | std::copy()、copy_n()、copy_if()、copy_backward()


    传送门 ==>> AutoSAR实战系列300讲「糖果Autosar」总目录

    1 C++ STL 中复制的不同方法及实例解析

    C++ STL 中存在各种不同的 copy(),它们允许以不同的方式执行复制操作,它们都有自己的用途。这些都在标题 中定义。本文向大家介绍这些函数,供日常编程使用。

    1. copy(strt_iter1, end_iter1, strt_iter2) :用于将一系列元素从一个容器复制到另一个容器的通用复制函数。它需要 3 个参数:
      strt_iter1 :指向源容器开头的指针,必须从那里开始复制元素。
      end_iter1 :指向源容器末尾的指针,直到必须复制元素为止。
      strt_iter2 :指向目标容器开头的指针,指向必须开始复制元素的位置。

    2. copy_n(strt_iter1, num, strt_iter2) :此版本的副本允许自由选择必须在目标容器中复制多少元素。IT 还需要 3 个参数:
      strt_iter1 :指向源容器开头的指针,必须从那里开始复制元素。
      num :整数,指定从 strt_iter1 开始将多少个数字复制到目标容器。如果输入负数,则不执行任何操作。
      strt_iter2 :指向目标容器开头的指针,指向必须开始复制元素的位置。

    // C++ code to demonstrate the working of copy()
    // and copy_n()
     
    #include
    #include // for copy() and copy_n()
    #include
    using namespace std;
     
    int main()
    {
         
       // initializing source vector
       vector<int> v1 = { 1, 5, 7, 3, 8, 3 };
        
       // declaring destination vectors
       vector<int> v2(6);
       vector<int> v3(6);
        
       // using copy() to copy 1st 3 elements
       copy(v1.begin(), v1.begin()+3, v2.begin());
        
       // printing new vector
       cout << "The new vector elements entered using copy() : ";
       for(int i=0; i<v2.size(); i++)
       cout << v2[i] << " ";
        
       cout << endl;
        
       // using copy_n() to copy 1st 4 elements
       copy_n(v1.begin(), 4, v3.begin());
        
       // printing new vector
       cout << "The new vector elements entered using copy_n() : ";
       for(int i=0; i<v3.size(); i++)
       cout << v3[i] << " ";
       
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    输出: 
    
    The new vector elements entered using copy() : 1 5 7 0 0 0 
    The new vector elements entered using copy_n() : 1 5 7 3 0 0
    
    • 1
    • 2
    • 3
    • 4
    1. copy_if():顾名思义,这个函数根据“条件”的结果进行复制。这是在第四个参数的帮助下提供的,一个返回布尔值的函数。
      这个函数有 4 个参数,其中 3 个类似于 copy() 和一个附加函数,当返回 true 时,复制一个数字,否则不复制数字。
    2. copy_backward():该函数从向后开始将元素复制到目标容器中,并继续复制,直到所有数字都没有复制。复制从“ strt_iter2 ”开始,但方向相反。它也采用与 copy() 类似的参数。
    // C++ code to demonstrate the working of copy_if()
    // and copy_backward()
     
    #include
    #include // for copy_if() and copy_backward()
    #include
    using namespace std;
     
    int main()
    {
         
        // initializing source vector
        vector<int> v1 = { 1, 5, 6, 3, 8, 3 };
             
        // declaring destination vectors
        vector<int> v2(6);
        vector<int> v3(6);
             
        // using copy_if() to copy odd elements
        copy_if(v1.begin(), v1.end(), v2.begin(), [](int i){return i%2!=0;});
             
        // printing new vector
        cout << "The new vector elements entered using copy_if() : ";
        for(int i=0; i<v2.size(); i++)
        cout << v2[i] << " ";
             
        cout << endl;
             
        // using copy_backward() to copy 1st 4 elements
        // ending at second last position
        copy_backward(v1.begin(), v1.begin() + 4, v3.begin()+ 5);
             
        // printing new vector
        cout << "The new vector elements entered using copy_backward() : ";
        for(int i=0; i<v3.size(); i++)
        cout << v3[i] << " ";
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    输出: 
    
    The new vector elements entered using copy_if() : 1 5 3 3 0 0 
    The new vector elements entered using copy_backward() : 0 1 5 6 3 0 
    
    • 1
    • 2
    • 3
    • 4
    1. 使用 inserter() 复制:
      在 copy() 操作之前让我们了解 inserter() 的语法。
      inserter() 用作我们要复制容器元素的目的地。
      inserter() 接受两个参数。第一个是任意类型的容器,第二个是容器的迭代器。
      它返回一个在任意类型容器上工作的 insert_iterator 实例。这个包装函数有助于创建 insert_iterator 实例。键入 %iterator 的名称需要知道容器的精确完整类型,这可能很乏味并妨碍泛型编程。使用此功能可以让您利用自动模板参数推导,使编译器为您匹配正确的类型。
    inserter() 的语法:
    
    std::inserter(Container& x, typename Container::iterator it);
    
    
    x:将插入新元素的目标容器。it:指向插入点的迭代器。
    
    返回: 一个 insert_iterator 将元素插入到x 所指示的位置。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    使用 inserter() 进行复制的语法:

    copy(strt_iter1, end_iter1, inserter(Container& x, typename Container::iterator it));
    
    • 1
    // C++ code to demonstrate the working of copy() using inserter()
     
    #include 
    #include 
    #include 
    using namespace std;
     
    int main()
    {
     
        vector<int> v1 = {1, 5, 7, 3, 8, 3};
        vector<int>::iterator itr;
        vector<int> v2;
     
        //using inserter()
        copy(v1.begin(), v1.end(), inserter(v2, itr));
     
        cout << "\nThe new vector elements entered using inserter: ";
        for (int i = 0; i < v2.size(); i++)
            cout << v2[i] << " ";
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    输出:

    The new vector elements entered using inserter: 1 5 7 3 8 3 
    
    • 1
  • 相关阅读:
    day6_C++
    同时看过 unreal4 和 Unity 源代码的人觉得哪个引擎架构更好?
    Bean实例
    企业微信机器人还能这么玩?
    java毕业设计保险公司客户信息管理系统Mybatis+系统+数据库+调试部署
    Kotlin的基本使用
    3.Linux文件管理命令-----ls显示文件名
    低代码助力教培机构管理,数字化+智能化
    ipv6地址概述——了解ipv6与ipv4不同
    字节技术面都过了,薪资都谈好了20K*13结果还是被刷了,问HR原因是。。。
  • 原文地址:https://blog.csdn.net/huihuige092/article/details/126308836