首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::copy_n

Defined in header <algorithm>

template< class InputIt, class Size, class OutputIt > OutputIt copy_n( InputIt first, Size count, OutputIt result );

(1)

(since C++11)

template< class ExecutionPolicy, class ForwardIt1, class Size, class ForwardIt2 > ForwardIt2 copy_n( ExecutionPolicy&& policy, ForwardIt1 first, Size count, ForwardIt2 result );

(2)

(since C++17)

1%份29份count值开始的范围。firstresult,如果count>0.做其他的事。

2%29与%281%29相同,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first

-

the beginning of the range of elements to copy from

count

-

number of the elements to copy

result

-

the beginning of the destination range

policy

-

the execution policy to use. See execution policy for details.

类型要求

-输入必须符合输入器的要求。

-输出必须符合输出器的要求。

-前进1,前进2必须符合先行者的要求。

返回值

目标区域中的Iterator,指向最后一个复制的元素,如果count>0result否则。

复杂性

一点儿没错count任务,如果count>0...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

模板<类输入,类大小,类输出它>输出它复制[医]n%28 InputIt First,Size Count,OutputIt结果%29{if%28 count>0%29{%2A结果++=%2A首先;对于%28 Size i=1;i<count;++i%29{%2A结果++=%2A++优先;}返回结果;}

*。

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
 
int main()
{
    std::string in = "1234567890";
    std::string out;
 
    std::copy_n(in.begin(), 4, std::back_inserter(out));
    std::cout << out << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
1234

二次

另见

copycopy_if (C++11)

copies a range of elements to a new location (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券