C++ algorithm fill() function
Example
Fill a vector with a value:
vector<int> numbers(6);
fill(numbers.begin(), numbers.end(), 5);
for (int number : numbers) {
cout << number << " ";
}
Try it Yourself »
Definition and Usage
The fill()
function fills a data range with a specified value.
The range of data is specified by iterators.
Syntax
fill(iterator start, iterator end, <type> value);
<type>
refers to the type of the data that the range contains.
Parameter Values
Parameter | Description |
---|---|
start | Required. An iterator pointing to the start of the data range. |
end | Required. An iterator pointing to the end of the data range. Elements up to this position will be included, but the element at this position will not be. |
value | Required. The value to write into every element in the data range. |
Related Pages
Read more about data structures in our Data Structures Tutorial.
Read more about iterators in our Iterators Tutorial.
Read more about algorithms in our Algorithms Tutorial.