Jump to content
  • Advertisement
Sign in to follow this  
Decrius

[C++] Occupy array at once

This topic is 2908 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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'?

Share this post


Link to post
Share on other sites
Advertisement
unsigned int result[digits] = { digits,digits,digits };

would do it

better is

for(int i = 0; i < digits; i++)
{
myarray = digits;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!