|
-
Aug 9th, 2015, 10:53 AM
#1
Array indices, change the element order?
If I have a function that accepts an array as a parameter, e.g.
double DoStuff(int arr[])
Are there any ways of specifiying the indices of that array to effect a change in the array element order?
So doing something like...
double DoStuff(int arr[3,1,0,2])
or
double DoStuff(int arr[3], arr[1], arr[0], arr[2])
or
something similar?
I can of course create a new array with the elements in the revised order and pass that, but just wondered if there is a shortcut for this?
-
Aug 9th, 2015, 06:23 PM
#2
Re: Array indices, change the element order?
Not that I'm aware of.
If I really needed that type of access to an array (and I have as part of a MasterMind game), I would use a second array that would pass the indexes in the order needed.
That gives you a level of indirection, essentially replicating the functionality of pointers into the array.
double DoStuff(int arr[], int idx[])
Loop (example. by i) and access the array in idx order
arr[idx[i]]
-
Aug 10th, 2015, 03:55 AM
#3
Re: Array indices, change the element order?
If you are using c++11 then another possibility could be an initializer list. See http://www.cplusplus.com/reference/i...tializer_list/
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Aug 29th, 2015, 10:56 AM
#4
Re: Array indices, change the element order?
How big is your array? Why don't you just pass a container to the function which contains the order of the read?
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|