std::fill_n
From cppreference.com
Defined in header
<algorithm>
|
||
(1) | ||
template< class OutputIt, class Size, class T >
void fill_n( OutputIt first, Size count, const T& value ); |
(until C++11) | |
template< class OutputIt, class Size, class T >
OutputIt fill_n( OutputIt first, Size count, const T& value ); |
(since C++11) | |
template< class ExecutionPolicy, class OutputIt, class Size, class T >
OutputIt fill_n( ExecutionPolicy&& policy, OutputIt first, Size count, const T& value ); |
(2) | (since C++17) |
1) Assigns the given
value
to the first count
elements in the range beginning at first
if count > 0
. Does nothing otherwise.
2) Same as (1), but executed according to
policy
. This overload does not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true
Contents |
[edit] Parameters
first | - | the beginning of the range of elements to modify |
count | - | number of elements to modify |
value | - | the value to be assigned |
policy | - | the execution policy to use. See execution policy for details. |
Type requirements | ||
-
OutputIt must meet the requirements of OutputIterator .
|
[edit] Return value
(none) | (until C++11) |
Iterator one past the last element assigned if count > 0 , first otherwise.
|
(since C++11) |
[edit] Complexity
Exactly count
assignments, for count > 0
.
[edit] Exceptions
The overload with a template parameter named ExecutionPolicy
reports errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception,
-
- if
policy
is std::parallel_vector_execution_policy, std::terminate is called - if
policy
is std::sequential_execution_policy or std::parallel_execution_policy, the algorithm exits with an std::exception_list containing all uncaught exceptions. If there was only one uncaught exception, the algorithm may rethrow it without wrapping in std::exception_list. It is unspecified how much work the algorithm will perform before returning after the first exception was encountered. - if
policy
is some other type, the behavior is implementation-defined
- if
- If the algorithm fails to allocate memory (either for itself or to construct an std::exception_list when handling a user exception), std::bad_alloc is thrown.
[edit] Possible implementation
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; } |
[edit] Example
The following code uses fill_n()
to assign -1 to the first half of a vector of integers:
Run this code
#include <algorithm> #include <vector> #include <iostream> #include <iterator> int main() { std::vector<int> v1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::fill_n(v1.begin(), 5, -1); std::copy(begin(v1), end(v1), std::ostream_iterator<int>(std::cout, " ")); std::cout << "\n"; }
Output:
-1 -1 -1 -1 -1 5 6 7 8 9
[edit] See also
assigns a range of elements a certain value (function template) |
|
(parallelism TS)
|
parallelized version of std::fill_n (function template) |