Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/fill"

From cppreference.com
< cpp‎ | algorithm
(Replaced the for loop with a range based for loop)
(See also: std::copy)
Line 64: Line 64:
 
{{dsc begin}}
 
{{dsc begin}}
 
{{dsc inc | cpp/algorithm/dsc fill_n}}
 
{{dsc inc | cpp/algorithm/dsc fill_n}}
 +
{{dsc inc | cpp/algorithm/dsc copy}}
 
{{dsc inc | cpp/algorithm/dsc generate}}
 
{{dsc inc | cpp/algorithm/dsc generate}}
 
{{dsc inc | cpp/algorithm/dsc transform}}
 
{{dsc inc | cpp/algorithm/dsc transform}}

Revision as of 08:01, 17 March 2015

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
fill
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Defined in header <algorithm>
template< class ForwardIt, class T >
void fill( ForwardIt first, ForwardIt last, const T& value );

Assigns the given value to the elements in the range [first, last).

Contents

Parameters

first, last - the range of elements to modify
value - the value to be assigned
Type requirements

Template:par req concept

Return value

(none)

Complexity

Exactly last - first assignments.

Possible implementation

template< class ForwardIt, class T >
void fill(ForwardIt first, ForwardIt last, const T& value)
{
    for (; first != last; ++first) {
        *first = value;
    }
}

Example

The following code uses fill() to set all of the elements of a vector of integers to -1:

#include <algorithm>
#include <vector>
#include <iostream>
 
int main()
{
    int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    std::vector<int> v1(data, data+10);
 
    std::fill(v1.begin(), v1.end(), -1);
 
    for (auto elem : v1) {
        std::cout << elem << " ";
    }
    std::cout << "\n";
}

Output:

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

See also

copy-assigns the given value to N elements in a range
(function template) [edit]
copies a range of elements to a new location
(function template) [edit]
assigns the results of successive function calls to every element in a range
(function template) [edit]
applies a function to a range of elements, storing results in a destination range
(function template) [edit]