[C++] Occupy array at once

Started by
5 comments, last by DevFred 13 years, 9 months ago
I believed this was valid syntax, but it's not working the way I thought it should;

        const unsigned int digits = 3;        unsigned int result[digits] = { digits };        for (unsigned int i = 0; i < digits; i++)        {            printf("%u ", result);        }        std::cerr << "\n";


This outputs:
3 0 0
while I had hoped it would output
3 3 3


What is a good way to fill the whole array with one specific value (I guess memset isn't enough for classes which need to be constructed).

Second question, how can an array be filled in an initialiser list?

class A{public:    int array[5];    A() :        array({1})    {        for (unsigned int i = 0; i < 5; i++)        {            printf("%u ", array);        }        std::cerr << "\n";    }};


This complains that it is implemented only in C++0x (warning). Is this true, or is that 'another feature'?
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
unsigned int result[digits] = { digits,digits,digits };

would do it

better is

for(int i = 0; i < digits; i++)
{
myarray = digits;
}
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy

There's also std::fill().
Quote:
This outputs:
3 0 0
while I had hoped it would output
3 3 3

When you omit elements from an array initialiser, the remainging elements are initialised to zero, or default constructed.

Quote:
What is a good way to fill the whole array with one specific value (I guess memset isn't enough for classes which need to be constructed).

A loop is the best way. For small values, the loop will be totally unrolled. For large values, you wouldn't want to unroll it anyway.

Quote:
Second question, how can an array be filled in an initialiser list?

Arrays cannot be initalised in initialiser list, no such syntax exists. You can wrap the array in a struct, which can be initialsed with a function call:
struct Wrapper{    int data[2];};Wrapper make_wrapper(int x, int y){    Wrapper result = { {x, y} };    return resilt;}class Example{public:    Example(int x, int y) : wrapper(make_wrapper(x, y))    {    }private;    Wrapper wrapper;}

Ultimately, this isn't much better and you might as well just do the work in the constructor. There is no especially elegant way to do this.
If you provide fewer elements than the size of the array, the remaining elements are default-constructed. Since the value of a default-constructed unsigned integer is 0, the contents of your array ends up as 3 0 0.

The most straightforward way would probably just be to use std::fill:
std::fill(results, results + digits, digits);


As for your second question, I don't think you can. Your best bet would be to, again, use std::fill in your constructor.
NextWar: The Quest for Earth available now for Windows Phone 7.
Thanks guys. I was under the impression that passing one value would make each element initialised by that value. Not sure where I read that or whether I remembered it wrong...
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
You could use a vector:
const unsigned int digits = 3;std::vector<unsigned int> result(digits, digits);

The first argument is the initial size of the vector, the second argument is the initial value of the elements.

This topic is closed to new replies.

Advertisement