C++ easy question

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
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.
 

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
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
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
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.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
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 :)
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
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?
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
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.
 

Turkey

Senior member
Jan 10, 2000
839
0
0
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.
 

PCHPlayer

Golden Member
Oct 9, 2001
1,053
0
0
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.
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
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.
 

manly

Lifer
Jan 25, 2000
13,316
4,091
136
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.
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
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.