Thread: Multidimensional array question

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    Question Multidimensional array question

    Hi everyone. I have a hopefully quick question.
    I have a multidimensional array that runs parallel with a string array

    Lincoln 120 300 400
    Parks 100 500 250
    Shakespeare 0 30 50
    Ghandi 250 100 40
    Ashe 300 50 175
    Rodriguez 87 118 320
    Curie 284 0 112

    I need to sort this and I know how to do it. But I need to sort it again with the
    highest value in the first row and keep all information in that row paired with the
    name . So
    Lincoln 120 300 400
    Parks 100 500 250

    Parks 100 500 250
    Lincoln 120 300 400

    I need so swap this whole rows. I'm using dynamic array. So my question is
    Do I have to do a bunch of temps to move them? Or is there a way to move the
    whole int array row as a single unit?

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Well, an array is nothing more than a chunk of memory in the virtual memory of your machine.

    You can't really 'move' it, you'd have to copy it (use temps). If you foresee yourself doing this
    operation often, then I'd recommend you explore other options such as a linked list or a vector
    in STL.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You can use std::vector, which has an efficient swap member function.

    Also, in general for swapping you should use std::swap. In C++11, it can be faster than what you would write using temporaries.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    How exactly are you representing these rows? Are you willing to use standard containers instead of (multidimensional) arrays?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multidimensional character array question
    By barramundi9 in forum C Programming
    Replies: 3
    Last Post: 05-20-2012, 06:16 PM
  2. Copy multidimensional array to single array?
    By seepox in forum C Programming
    Replies: 9
    Last Post: 05-08-2006, 11:19 AM
  3. multidimensional array function - simple question
    By boltz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2005, 04:24 PM
  4. multidimensional array question
    By Mr_Jack in forum C++ Programming
    Replies: 10
    Last Post: 11-01-2003, 05:54 AM
  5. multidimensional array
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2002, 09:17 PM