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. |
Заголовочный файл <algorithm>
|
||
template< class InputIt, class Size, class OutputIt > OutputIt copy_n( InputIt first, Size count, OutputIt result ); |
||
Производит копирование
count
значений из диапазона начинающегося с first
в диапазон начинающийся с result
, если count>0
. И больше ни чего.Original:
Copies exactly
count
values from the range beginning at first
to the range beginning at result
, if count>0
. Does nothing otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Содержание |
[править] Параметры
first | - | начало копируемого диапазона
Original: the beginning of the range of elements to copy from The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
count | - | количество копируемых элементов
Original: number of the elements to copy The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
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 .
|
[править] Возвращаемое значение
Возвращает итератор на элемент, следующий за последним элементом диапазона в который производилось копирование.
Original:
Iterator in the destination range, pointing past the last element copied if
count>0
or first
otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[править] Сложность
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'; }
Вывод:
1234
[править] См. также
(C++11) |
копирует диапазон элементов (шаблон функции) |