The C++ function std::algorithm::fill_n() assigns value to the first n elements of the sequence pointed by first.
DeclarationFollowing is the declaration for std::algorithm::fill_n() function form std::algorithm header.
C++98template <class OutputIterator, class Size, class T> OutputIterator fill_n (OutputIterator first, Size n, const T& val);Parameters
first − Output iterators to the initial positions.
n − Number of elements to fill.
val − Value to be used to fill the range.
Returns an iterator pointing to the element that follows the last element filled.
ExceptionsThrows an exception if either element assignment or an operation on an iterator throws exception.
Please note that invalid parameters cause undefined behavior.
Time complexityLinear.
ExampleThe following example shows the usage of std::algorithm::fill_n() function.
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(void) { vector<int> v(5, 1); fill_n(v.begin() + 2, 3, 4); cout << "Vector contains following elements" << endl; for (auto it = v.begin(); it != v.end(); ++it) cout << *it << endl; return 0; }
Let us compile and run the above program, this will produce the following result −
Vector contains following elements 1 1 4 4 4
algorithm.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4