std::copy_n
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <algorithm>
|
||
| template< class InputIt, class Size, class OutputIt > OutputIt copy_n( InputIt first, Size count, OutputIt result ); |
||
Copies exactly count values from the range beginning at first to the range beginning at result, if count>0. Does nothing otherwise.
目录 |
[编辑] 参数
| first | - | the beginning of the range of elements to copy from |
| count | - | number of the elements to copy |
| result | - | 的目标范围的开头
Original: the beginning of the destination range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| Type requirements | ||
-InputIt must meet the requirements of InputIterator.
| ||
-OutputIt must meet the requirements of OutputIterator.
| ||
[编辑] 返回值
Iterator in the destination range, pointing past the last element copied if count>0 or first otherwise.
[编辑] 复杂性
Exactly count assignments, if count>0.
[编辑] 可能的实现
template< class InputIt, class Size, class OutputIt> OutputIt copy_n(InputIt first, Size count, OutputIt result) { if (count > 0) { *result++ = *first; for (Size i = 1; i < count; ++i) { *result++ = *++first; } } return result; } |
[编辑] 为例
#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'; }
Output:
1234
[编辑] 另请参阅
| (C++11) |
某一范围的元素复制到一个新的位置 Original: copies a range of elements to a new location The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |