Reset position of a vector to ensure there is no dirt

2

How do I zero the position of an integer vector, for example, to ensure there is no "dirt" when allocating the position in memory in C ++?

Some language function or algorithm.

    
asked by anonymous 16.03.2016 / 13:31

2 answers

1

If you are going to use array yourself, just do this:

std::fill_n(array, 100, 0);

Some compilers may adopt an extra alternative syntax:

int array[100] = {0};

or

int array[100] = {};

or

int array[100] = { [0 ... 99] = 0 };

Test which produces the result in your compiler if you want one of them. But this is non-standard, so it should be avoided.

If you want to adopt the C way of doing this (I do not recommend):

int array[100];
memset(array, 0, sizeof(array));

But in C ++ the array is not recommended, you may prefer:

std::vector<int> vector1(100, 0);
    
16.03.2016 / 13:43
0

It's a case where I really like to use a macro ... but deep down it's a loop:

#define INIT_ARRAY(array,value) {\
    for(s32 i=0; i<lSIZEOF(array);i++){\
        array[i] = value;\
    }\
}

With this I can do INIT_ARRAY by passing the array and the value with which I want to initialize.

    
16.03.2016 / 13:36