Array manipulation.

This is a discussion on Array manipulation. within the C++ Programming forums, part of the General Programming Boards category; Is there any way to go about deleting a single item from an array, without altering the other iems?...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    Array manipulation.

    Is there any way to go about deleting a single item from an array, without altering the other iems?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    In a standard array like this
    Code:
    int Array[10];
    You could create a new array and copy the items that you want to it and delete the old array.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    28
    You could always do something like this:

    Code:
    // Init new array (5 elements)
    int *pnArray = (int*)calloc(5, sizeof(int));
    
    for (int i = 0; i < 5; i++)
        pnArray[i] = i;
    
    // Array now: {0, 1, 2, 3, 4}
    // Delete element ... 2 (value = 1)
    int *pTempArr = (int*)calloc(4, sizeof(int));
    int added = 0;
    
    for (int i = 0; i < 5; i++)
    {
        if (i != 2)
        {
            pTempArr[added++] = pnArray[i];
        }
    }
    
    free(pnArray);
    pnArray = pTempArr; // Copies pointer. NOTE: Don't foget to free later! (free either, points to same data)
    I'm sure you can figure out how to apply a function to the second part. I just whipped up some really really quick code.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    hmm, well, I'm not too familiar with pointers right now, any other way to do it?

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    The answer to your question is no! There is no way to delete an element from an array. You must create a second array and copy all the elements minus the one(s) you want to delete. All of the standard containers allow you to delete elements. I would suggest vector or deque.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    ok, I'll have to look up a few more things then, thnx for the help fellas

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    I've looked through and have started to use vectors instead of arrays, but how would I go about adding a value to a certain element of a structure through an element. I know that with an array I can type in
    Code:
    cin >> file[n].custname;
    But how would I go about it with a vector?

  8. #8
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    vectors are LIFO (last in first out) data structures, therefore you need to push and pop your data into the vector, i.e.
    Code:
    file.push('A')
    file.pop()
    these functions push 'A' to the top of the stack (vector), and 'pop()' pops off the top element in the stack (vector). There are many different things you can do with a vector, such as sort, search etc though im not sure on their syntax as i havent used vectors all that much. If anyone could correct me please do, i dont want to hand out bad advice

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>how would I go about adding a value to a certain element of a structure through an element. I know that with an array I can type in

    cin >> file[n].custname;

    The syntax is exactly the same for vectors. Assuming file is the name of a vector of structs/classes and the struct/class contain custname as a member with public access, the above syntax should work

    >>vectors are LIFO (last in first out) data structures, therefore you need to push and pop your data into the vector,

    This is wrong. Vectors allow random access with random insertion/deletion(erasures). Stacks are LIFO, queues are FIFO, deques can add or remove at either end, and lists/vectors are random insertion/deletion. It may be more efficient to add/remove from front/back using a vector/list, but it isn't necessary.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

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