Erasing Array elements in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hefty1985
    New Member
    • Jul 2007
    • 7

    Erasing Array elements in C

    Hi Guys,

    I am trying to eliminate certain elements off an array or vector in C without affecting other elements. Any idea of how this can be done neatly. For example:

    Input Array : 8 7 6 5 **0** 9 **0** 8 7

    Ouput Array: 8 7 6 5 9 8 7

    Any idea how a C function can be written to execute such functionality.

    Best regards,

    Hefty
  • ravenspoint
    New Member
    • Jul 2007
    • 111

    #2
    There is no need to write such a function. The vector and other STL containers provide the method erase() to do this.

    Comment

    • hefty1985
      New Member
      • Jul 2007
      • 7

      #3
      Originally posted by ravenspoint
      There is no need to write such a function. The vector and other STL containers provide the method erase() to do this.
      Hi ravenpoint,

      I am working in C and I am not sure if it has an STL as this is available in C++. Could you please advise on how would I use erase in this context and how would I index the rest of the array members following eliminating one of them.

      For example:

      Input array: 0 1 7 9 8 **4** 3
      Ouptut array: 0 1 7 9 8 3

      Thanks,

      Hefty

      Comment

      • getchar
        New Member
        • Feb 2007
        • 1

        #4
        just a suggestion:
        y don't u replace the elements tat u don't want with a specific character which u r not expecting to be part of ur input eg: '?'
        then do a loop to print every thing but the '?'

        warning: can b a slow program with large data

        best of luck

        Comment

        • ravenspoint
          New Member
          • Jul 2007
          • 111

          #5
          Originally posted by hefty1985
          Hi ravenpoint,

          I am working in C and I am not sure if it has an STL as this is available in C++.
          Since you mentioned vector in your opriginal post ( OP ) I assumed that you had STL.

          You should check to see if you do, since it is extremely helpful and will save you many hours of work.

          If you do not have STL, then getchar's scheme sounds workable. Have you tried it?

          Comment

          Working...