std::fill_n

cppreference.com

Example

main: () = {
    v1: std::vector<int> = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

    // replace values of the first 5 elements with -1
    std::fill_n(v1.begin(), 5, -1);

    std::copy_n(v1.cbegin(), v1.size(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';

    nums: std::vector = (
        :std::complex<double>=(1, 3),
        :std::complex<double>=(2, 2),
        :std::complex<double>=(4, 8));
    std::fill_n(nums.begin(), 2, std::complex<double>(4, 2));
    std::copy_n(nums.cbegin(), nums.size(),
                std::ostream_iterator<std::complex<double>>(std::cout, " "));
    std::cout << '\n';
}

Output

-1 -1 -1 -1 -1 5 6 7 8 9 
(4,2) (4,2) (4,8) 

Back to top

cpp2reference.com licensed under CC-BY-SA and GFDL.