Accessing multiple indexes of an array

Basically i have made an array and would like to later edit it by changing all the value of the array at once.

The way I am currently doing it is
1
2
3
4
5
6
7
heapArray[0] = 31;
heapArray[1] = 41;
heapArray[2] = 59;
heapArray[3] = 26;
heapArray[4] = 53;
heapArray[5] = 58;
heapArray[6] = 97;


This works great and all but i would like to just have one line similar to something like:

heapArray[] = {31,41,59,26,53,58,97}

and although this works to initialize it doesn't seem to work if i already have the array :\

Any help would be greatly appreciated
There exists nothing like what you are asking...not for normal arrays, at least.
You can do this with vectors if you're willing to use C++0x.

1
2
vector<int> vec;
vec={31,41,59,26,53,58,97};
Alright cool, mainly just wanted to know if something existed for arrays
thanks for the help!
for (X = 0; X < 7; X++) heapArray[X] = Value;

That only works if you want to reinitialize the array though and it can't be independent. You would have to pick a specific formula and use it. For example Value could be replaced with X * 9 + 3. So you would get this:

heapArray[0] = 3;
heapArray[1] = 12;
heapArray[2] = 21;
heapArray[3] = 30;
heapArray[4] = 39;
heapArray[5] = 48;
heapArray[6] = 57;


That's not what you're looking for though it seems. The only way is probably changing them one at a time.
For C arrays there isn't, but for C++ arrays there is:

1
2
array<int,7> heapArray;
heapArray = array<int,7>{31,41,59,26,53,58,97};
You can't quite do it in one line, but you probably could in less then 10. I'm picturing a function that takes new array as a parameter, then uses a for loop to overwrite the old array.

1
2
3
template<class DATA, class Arg1, class Arg2>
DATA SillyFunction(Arg1 TargetArray[], Arg2 NewData[], int Size)
{for(int i = 0; i <= Size; i++) TargetArray[i] = NewData[i];}


This is off the top of my head so I don't know if it truely works yet. I'll give it a shot and post back with the results.


EDIT: It appears to work. I suggest setting a variable to set the size of your array, this prevents having to change a dozen literals if you want to change the size of your arrays.

REEDIT: By the way I am in no way discouraging the use of the STL containers. They are there for a reason and a vector is a very powerful one to use.
Last edited on
Topic archived. No new replies allowed.