• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ easy question

ElDonAntonio

Senior member
Suppose I write the following expression:

int index[3] = { 34, 54, 23};

No problem. But if I declare:

int index[3];

how do I set the values afterward in the same fashion as above? Of course I can set each array element to its value manually but it gets tedious when arrays are large.
 
Well if the array elements follow some pattern you could do:

int index[10000];

int counter = 0;
int value = 2;

for (counter = 0; counter < 10000; counter++)
{
index[counter] = value;
value += 2;
}

That would initialize an array that counted by 2's from 2 to 20000. If the values follow no pattern I don't know what you would do. Unless they were truly random, then you could just replace index[counter] = value; with index[counter] = rand();

hope that gives you some idea
 
Thanks SpecialK! Unfortunately that won't work. My values aren't exactly random but they're specific constants that don't really follow a pattern.
 
Don't work with raw arrays. The technique I like to use:

#include <vector>
using namespace std;


class MyNumbers
{
public:
MyNumbers(int capacity = -1)
{
if(capacity > 0)
vec.resize(capacity);

pos = 0;
}

MyNumbers& operator << (int num)
{
vec[pos++] = num;
return (*this);
}


void Dump()
{
cout << "Dump:\n";

for(int r=0; r < vec.size(); ++r)
cout << vec[r] << "\n";

cout << "..end..\n";
}

vector<int> vec;
int pos;
};


void main(void)
{
MyNumbers num(5);

num << 100 << 20 << 30 << 50 << 10;
num.Dump();
}


Of course, you should use a template if you want to store something other than an int 🙂
 
Thanks singh, but that's a little overkill for my needs! 🙂

I just want to store a few values, and it just doesn't seem clean to write for example:

array[0] = 43;
array[1] = 89;
array[2] = 34;
etc...

Is there no other way to store the values in the {} fashion?
 
how about varargs?


void aa(int a[], int size, ...)
{
va_list vl;
int lc = 0;

va_start(vl, size);
for(; lc < size; lc++)
a[lc] = va_arg(vl, int);
va_end(vl);
}

int main(void)
{
int a[3];
aa(a, 3, 86, 45, 44);
return 0;
}


where aa(...) is the function that performs assignment to an array based on the element count passed in as a second argument.
 
You could put the constants in a file, then read them out sequentially in a loop that iterates the number of times that the array has elements.

file:
---
45
53
93
...

code:
ifstream fileIn("file");
int array[ARRAY_SIZE];

for (int i = 0; (i < ARRAY_SIZE) && (!fileIn.fail()); i++)
{
fileIn >> array;
}

fileIn.close();

I like the varargs idea though.
 
There is nothing you can do, but I hope that your constants are declared as #define's (in C) or as static const int's (C++). Never use hard-coded constants it makes the code harder to understand and maintain.
 
Thanks for the suggestions guys! unfortunately I didn't find the solution I hoped for. I wonder how come it's possible to enter the values in an array so easily at declaration but that there's no such simple way afterwards.
 
Originally posted by: ElDonAntonio
Thanks for the suggestions guys! unfortunately I didn't find the solution I hoped for. I wonder how come it's possible to enter the values in an array so easily at declaration but that there's no such simple way afterwards.
The array initializer is checked at compile time to test that the buffer has adequate size.

You can't use the same array initialization syntax at runtime because raw buffers in C/C++ aren't bounds checked. It's one of those safety/convenience/efficiency trade-offs.
 
thanks manly!

I guess an other easy is not very clean way would be to declare another array of same size and initialize it, then do a memcpy into the original array. (I don't know if I make much sense, I just had a few drinks). Not very memory efficient but for relativily small arrays it should be alright.
 
Back
Top