algorithm_fill
algorithm_fill (범위 채우기)
std::fill은 대상 범위 [first, last)의 모든 요소에 지정한 값 value를 할당해요.
출처: cppreference
본문
std::fill은 <algorithm> 헤더에 정의되어 있어요.
template< class ForwardIt, class T >
void fill( ForwardIt first, ForwardIt last, const T& value );
- 1번 오버로드 — 대상 범위
[first, last)의 모든 요소에value를 할당해요. - 2번 오버로드(병렬) — 실행 정책
policy에 따라 수행해요.
복잡도 (Complexity)
정확히 std::distance(first, last)번의 배정이 필요해요.
예제 (Example)
#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
void println(const auto& seq)
{
for (const auto& e : seq)
std::cout << e << ' ';
std::cout << '\n';
}
int main()
{
std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::fill(v.begin(), v.end(), -1);
println(v);
std::vector<std::complex<double>> c{{1, 2}, {3, 4}};
std::fill(c.begin(), c.end(), {4, 2});
println(c);
}
출력:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
(4,2) (4,2)