Home c++ Initialize array variables with one value without a cycle?

Initialize array variables with one value without a cycle?

Author

Date

Category

1) Tell me, please, is it possible on C++ how to initialize all the solutions of the array with the same value without using the cycle?
Or is it possible to somehow solve the following problem …
I have an array in my class

static bool result [m];

I want initially all the values ​​of this array were True . In the process, I will need to change them.
There was an idea to make a cycle

for (i = 0; i & lt; m; i ++) result [i] = True;

In the class designer, but then after all it will be performed with each creation of the object … So it’s not worth doing ..?
Please tell me what to do)

2) and question 2 (even more stupid).
Suppose I have a global variable const int n = 5 .
I need to create a global variable const int m , which will be equal to 10 to degrees N. The function Pow In this case, it is impossible to use how it can be done?


Answer 1, Authority 100%

1) You cannot avoid initialization. But you can declare static not an array, but a whole class object with a constructor:

template & lt; size_t m & gt;
Struct initializedarray
{
  BOOL DATA [M];
  InitializedArray ()
  {
    for (int i = 0; i & lt; m; i ++) data [i] = true;
  }
}
...
Static InitializedArray & LT; M & GT; result;

2) You will suit this trick with a template :

const int n = 5;
Template & lt; int m & gt;
STRUCT POWER_OF_10
{
  static const int value = 10 * Power_of_10 & lt; m - 1 & gt; :: value;
};
TEMPLATE & LT; & GT;
STRUCT POWER_OF_10 & LT; 0 & gt;
{
  static const int value = 1;
};
Const int m = Power_of_10 & lt; n & gt; :: value;

Answer 2, Authority 56%

Not knowing M, an array can be initialized only by default values ​​of the type, i.e. False . In the case of a static or global massif, this happens automatically, in the case of not static can be used in one of the equivalent methods:

bool arr [10] = {0}; // works even in with
  BOOL ARR [10] = {false};
  BOOL ARR [10] = {};

Slip value with each constructor call is certainly not worth it. I see several options for solving your problem.


Simple solution. you can change the meaning that the values ​​of your array are on the opposite. For example, if it was an ISResourceAvailable array, rename it to isresourcebusy. Then default initialization will be correct.


Universal solution. Create a method (possibly static) to initialize static variables, and make sure that this method will have the effect only when you first call. And then call it when designing each object. It will look like this:

class myclass {
Public:
 MYCLASS () {
  Maybeinitializestatic ();
 }
Private:
 Static Void MayBeinitializestatic () {
  Static bool initialized;
  if (initialized)
   Return;
  initialized = true;
  STD :: Fill_n (Arr, M, True);
 }
 STATIC BOOL ARR [M];
};

It is worth noting that such a solution is suitable only for a single-flow program. To make it Thread-Safe, you need to add synchronization:

static void maybeinitializestatic () {
  Static bool initialized;
  Static Std :: Mutex Mutex;
  if (initialized) {
   Return;
  } else {
   STD :: UNIQUE_LOCK & LT; STD :: MUTEX & GT; LOCK (Mutex);
   if (initialized) Return;
   STD :: Fill_n (Arr, M, True);
   initialized = true;
  }
 }

Another universal solution , even better than the previous one. Call a function during the initialization of another static variable.

class myclass {
Public:
 MYCLASS () {}
Private:
 Static Void Initializestatic () {
  STD :: Fill_n (Arr, M, True);
 }
 STATIC BOOL ARR [M];
 Static int Unused;
};
int myclass :: unused = (initializestatic (), 0xdeadbeef);

Do not need synchronization nor the verification of the fact that the function will only be caused once.


Answer 3, Authority 33%

1) If you fight for speed, replace the cycle on

memset (result, 0xff, sizeof (result))

In any case, someone should set a few in memory, from nowhere they will not take there. So, even if this will be done the function whose task is to do this and do the most efficiently.


Answer 4

You can use std :: vector Instead of the array, it has a constructor that fills it with the desired value:

std :: vector & lt; bool & gt; Result (M, TRUE);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions