在 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<iostream>
#include<algorithm> // for copy() and copy_n()
#include<vector>
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] << " ";
   
}
输出: 

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. 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<iostream>
#include<algorithm> // for copy_if() and copy_backward()
#include<vector>
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] << " ";
 
}
输出: 

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. 使用 inserter() 复制:
    在 copy() 操作之前让我们了解 inserter() 的语法。
    inserter() 用作我们要复制容器元素的目的地。
    inserter() 接受两个参数。第一个是任意类型的容器,第二个是容器的迭代器。
    它返回一个在任意类型容器上工作的 insert_iterator 实例。这个包装函数有助于创建 insert_iterator 实例。键入 %iterator 的名称需要知道容器的精确完整类型,这可能很乏味并妨碍泛型编程。使用此功能可以让您利用自动模板参数推导,使编译器为您匹配正确的类型。
inserter() 的语法:

std::inserter(Container& x, typename Container::iterator it);


x:将插入新元素的目标容器。it:指向插入点的迭代器。

返回: 一个 insert_iterator 将元素插入到x 所指示的位置。

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

copy(strt_iter1, end_iter1, inserter(Container& x, typename Container::iterator it));
// C++ code to demonstrate the working of copy() using inserter()
 
#include <iostream>
#include <algorithm>
#include <vector>
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] << " ";
 
}

输出:

The new vector elements entered using inserter: 1 5 7 3 8 3 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

糖果Autosar

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

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

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

打赏作者

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

抵扣说明:

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

余额充值