Results 1 to 4 of 4

Thread: Array indices, change the element order?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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?

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    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]]

  3. #3
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,170

    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)

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    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?
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .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
  •  



Click Here to Expand Forum to Full Width