efficient array

This is a discussion on efficient array within the C++ Programming forums, part of the General Programming Boards category; hi, i have created an array, but i am still using it inefficiently. can anyone help me make this code ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    efficient array

    hi, i have created an array, but i am still using it inefficiently. can anyone help me make this code more efficient?

    Code:
    	fIn2.get(xCh[0]);
    	fIn2.get(xCh[1]);
    	fIn2.get(xCh[2]);
    	fIn2.get(xCh[3]);
    
    	fout.put(xCh[1]);
    	fout.put(xCh[0]);
    	fout.put(xCh[3]);
    	fout.put(xCh[2]);
    I am basically reading in four characters at a time and writing them out in different order. I know there's gotta be a way to make this shorter, but my brain is frozen right now.


    thanks,
    keith

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: efficient array

    Code:
    	fIn2.get(xCh[1]);
    	fIn2.get(xCh[0]);
    	fIn2.get(xCh[3]);
    	fIn2.get(xCh[2]);
    
    	fout.write(xCh,4);
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Just posting this to demonstrate using a for loop to access an array (you'll do this lots in the future)
    Code:
    for(int i=0; i<4;i ++)
    {
    	fIn2.get(xCh[i]);
    }
    
    	fout.put(xCh[1]);
    	fout.put(xCh[0]);
    	fout.put(xCh[3]);
    	fout.put(xCh[2]);
    Away.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I would definitely rather never use put or get. You make WAAAY more function calls that way and a single disk access is much better than 100 disk accesses. Disk access can be one of the slowest things you can do. Now, I will admit that fstream does buffering so it isn't as bad as I'm making it, however you still make more function calls that way. So an even better way to do this would be to read the entire buffer in and write the entire buffer out. Like so:

    Code:
               char buf[4];
               fIn2.read(buf,4);
               xCh[0] = buf[1];
               xCh[1] = buf[0];
               xCh[2] = buf[3];
               xCh[3] = buf[2];
               fout.write(xCh,4);
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21