shift characters inside array

This is a discussion on shift characters inside array within the C++ Programming forums, part of the General Programming Boards category; hi, i have an unsigned char array...say my_array[3] and i'm trying to shift every content in the array by 2. ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    84

    shift characters inside array

    hi,

    i have an unsigned char array...say my_array[3] and i'm trying to shift every content in the array by 2.

    for example, i have: my_array[1] = 'aa'
    my goal is to shift the 'aa' by 2 to the right so its ' aa'

    can anyone give me any suggestions? i thought about using memcopy but i figured using the shift operators (<< and >>) will be much faster since my overall goal is to reduce processing time.

    thanks,
    bg742
    Last edited by barneygumble742; 07-17-2009 at 06:35 PM. Reason: forgot a space

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,440
    Use a deque, instead of an array. Safer and allows for fast insertions and access. For the example you provided, you'd want to then use the push_front member function.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    hi. how will it work with pointers? for example i have this:
    Code:
    unsigned char *array;
    array = (unsigned char *)malloc(SIZE);
    fread(array, 1, SIZE, fin);                // reading from file

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    This is C rather than C++. Which are you actually using?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    only the file IO part is in C but the rest of the code is C++.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    It would be a lot faster if you just read the file with a std::string. You could then insert spaces wherever you want with one of the insert functions.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    very true. i thought about that. but for whatever reason, using C style file IO reported slightly faster times than C++.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Can you prove that to me?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-24-2008, 06:16 PM
  2. copying the first 10 characters to an array?
    By arya6000 in forum C Programming
    Replies: 3
    Last Post: 09-20-2008, 10:39 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

Tags for this Thread


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