1

Right now, to set all items in an array to, say, 0, I have to loop through the entire thing to preset them.

Is there a function or shortcut which can defaultly set all values to a specific number, when the array is stated? Like so:

int array[100] = {0*100}; // sets to {0, 0, 0... 0}
10
  • If you are flexible in using std::vector, then you can initialize during construction like std::vector<int> v (100 /* length */, 42 /* initial value */); Commented Dec 8, 2021 at 6:03
  • {0*100} looks a bit strange. On one hand, 0 * 100 == 0 and correct (answer of Denise). On the other hand, it looks like a repetition of 0 is intended to express. That doesn't work that way. And, btw. 0 is the only possible value for an array initializer in that manner. Commented Dec 8, 2021 at 6:04
  • It's just an example. Commented Dec 8, 2021 at 6:06
  • It's just an example. Yeah, but a somehow confusing... ;-) Commented Dec 8, 2021 at 6:08
  • 1
    @SkyriderFeyrs In python it is [0]*3 -> [0, 0, 0] not [0*100] -> [0]. Unrelated: Be careful while using multiplication on lists in python you'd be victim of changes in sublist are relected across the list Commented Dec 8, 2021 at 6:18

5 Answers 5

4

If you want to set all the values to 0 then you can use:

int array[100] = {0}; //initialize array with all values set to 0

If you want to set some value other than 0 then you can use std::fill from algorithm as shown below:

int array[100];  //not intialized here
std::fill(std::begin(array), std::end(array), 45);//all values set to 45
Sign up to request clarification or add additional context in comments.

Comments

4
int array[100] = {0}; 

should do the job Please refer to cppreference

int a[3] = {0}; // valid C and C++ way to zero-out a block-scope array
int a[3] = {}; // invalid C but valid C++ way to zero-out a block-scope array

2 Comments

This one only sets it to 0 though...
Yep , sorry according to your example, I thought the specific number should be 0. int arr[100] = {1}; leads to an array with a 1 in the first position and 0 for the rest of the 99 positions.
2

Going forward you should use std::array instead of C-style arrays. So this become:

std::array<int,100> array;
array.fill(0);

Comments

1

If you want to initialize the array using a function's return value:

#include <algorithm>

int generateSomeValue( )
{
    int result { };
    // some operations here to calculate result

    return result;
}

int main( )
{
    int array[ 100 ];
    std::generate( std::begin( array ), std::end( array ), generateSomeValue );
}

Comments

1

You should use vectors, which are more flexible than array.

#include <iostream>
#include <vector>

int main()
{
std::vector<int>v(100,10); // set 100 elements to 10
}

Try running this: https://onlinegdb.com/2qy1sHcQU

3 Comments

I am pretty sure this initializes only the first element. godbolt.org/z/36e3EYKE1
Yes, only the first element.
@Jellyboy you are right let me edit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.