std::fill_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 OutputIt, class Size, class T > void fill_n( OutputIt first, Size count, const T& value ); |
(до C++11) (начиная с C++11) |
|
Назначение данного значения
value
к первому элементов count
в диапазоне начиная с first
если count>0
. Ничего не иначе.Original:
Assigns the given value
value
to the first count
elements in the range beginning at first
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 modify 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 elements to modify The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
value | - | значение, которое будет назначен
Original: the value to be assigned The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Type requirements | ||
-OutputIt must meet the requirements of OutputIterator .
|
[править] Возвращаемое значение
(Нет) (до C++11)
Original:
(none) (до C++11)
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.
Iterator за последним элементом присваивается, если
count>0
, first
иначе. (начиная с C++11)Original:
Iterator one past the last element assigned if
count>0
, first
otherwise. (начиная с C++11)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.
[править] Сложность
Именно
count
заданий, для count>0
.Original:
Exactly
count
assignments, for count>0
.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.
[править] Возможная реализация
template<class OutputIt, class Size, class T> OutputIt fill_n(OutputIt first, Size count, const T& value) { for (Size i = 0; i < count; i++) { *first++ = value; } return first; } |
[править] Пример
Следующий код использует
fill_n()
назначить -1 до первой половины вектор целых чисел
Original:
The following code uses
fill_n()
to assign -1 to the first half of a vector of integers:
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.
#include <algorithm> #include <vector> #include <iostream> int main() { std::vector<int> v1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::fill_n(v1.begin(), 5, -1); for (vector<int>::iterator it = v1.begin(); it != v1.end(); ++it) { std::cout << *it << " "; } std::cout << "\n"; }
Вывод:
-1 -1 -1 -1 -1 5 6 7 8 9
[править] См. также
назначает диапазон элементов определенного значения Original: assigns a range of elements a certain value The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (шаблон функции) |