- Using Designated Initializers
// or don't specify the size
int arr[] = {[0 ... 4] = 1};
- Using std::fill_n function
Finally, we can use std::fill_n in C++, which assigns a value to the first n array elements.
#include <iostream>
#include <algorithm>
int main()
{
int n = 5;
int val = 1;
int arr[n];
std::fill_n (arr, n, val);
// always prints 1
std::cout << arr[rand() % n];
return 0;
}
标签:std,arr,elements,int,value,C++,array,fill
From: https://www.cnblogs.com/uceec00/p/16906421.html