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}
  • If you are flexible in using std::vector, then you can initialize during construction like `std::vector v (100 /* length */, 42 /* initial value */);` – Wander3r Dec 08 '21 at 06:03
  • `{0*100}` looks a bit strange. On one hand, 0 * 100 == 0 and correct ([answer of Denise](https://stackoverflow.com/a/70270595/7478597)). 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. – Scheff's Cat Dec 08 '21 at 06:04
  • It's just an example. – Skyrider Feyrs Dec 08 '21 at 06:06
  • _It's just an example._ Yeah, but a somehow confusing... ;-) – Scheff's Cat Dec 08 '21 at 06:08
  • Sorry about that. I just wasn't sure if that would work because it works with Python. – Skyrider Feyrs Dec 08 '21 at 06:08
  • Oh oh. Please, don't try to apply Python knowledge to C++ too much. That may lead you into a wrong direction. C++ and Python are both imperative languages, and certain things might look similar but they are still very different languages where C++ especially has a lot of subtleties you have to care about (but would never consider such stuff in Python). First of all, memory management... – Scheff's Cat Dec 08 '21 at 06:11
  • Okay. Dart and C++ are pretty similar though – Skyrider Feyrs Dec 08 '21 at 06:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239937/discussion-between-skyrider-feyrs-and-scheffs-cat). – Skyrider Feyrs Dec 08 '21 at 06:14
  • 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](https://stackoverflow.com/q/240178/12416453) – Ch3steR Dec 08 '21 at 06:18
  • Thanks for the help! Now I have to review my homework... – Skyrider Feyrs Dec 08 '21 at 06:19

5 Answers5

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
Jason
  • 36,170
  • 5
  • 26
  • 60
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
Denise P
  • 81
  • 12
  • This one only sets it to 0 though... – Skyrider Feyrs Dec 08 '21 at 06:28
  • 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. – Denise P Dec 08 '21 at 07:12
2

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

std::array<int,100> array;
array.fill(0);
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

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 );
}
digito_evo
  • 3,216
  • 2
  • 14
  • 42