1
int i;
int Input;

cin >> Input;

 for(i = 0; i < Size ; i ++ )
  if (List[i].PersonID == Input) {

 }

I am trying to make a function that deletes a record from the array based on the id input provided. I am not sure what to do beyond this here. Will I also need to shift the values in the array after a record is removed?

2
  • List is most probably not int, but rather a container of int, what concrete type is it? Commented Jan 30, 2011 at 23:45
  • Looks like List is a user-defined class. The bigger question is is it a pointer or not? OP could simply set it to NULL as long as memory is freed. If he wants to resize his array (or simply bump things down) it's a little bit different (to resize, List will need to be dynamically allocated). Without knowing anymore context it's hard to give any concrete direction. Commented Jan 31, 2011 at 6:49

4 Answers 4

1

I can't tell what type your List is. But you should go for something like this:

List.RemoveAt(i--);
List.DeleteAt(i--);

i-- will decrement i AFTER the function has been called.

You should not need to shift any values in the array if you are using the standard containers. If you are responsible for the array, then you do need to shift your values.

** EDIT

Here is a link to an introduction to the standard containers. If you are managing your own dynamic array you should consider using these instead.

Sign up to request clarification or add additional context in comments.

1 Comment

What are DeleteAt and RemoveAt?
1

Here I'm assuming List is a primitive array of ints.

#include<algorithm>  // where std::remove() resides
#include<iterator>   // where std::distance() resides (not strictly necessary)

struct BadPerson {
    BadPerson(int Bad) : Bad_(Bad) { }
    bool operator()(const Element& Elem) const {
        return Elem.PersonID == Bad_;
    }
};

// ...
int *NewEnd = std::remove_if(List, List + ListLength, BadPerson);

// the list now has a new end, because elements were "removed".
// but they weren't really removed; the array still has the same fixed size.
int ListLength = std::distance(List, NewEnd);

4 Comments

I made an effort to maintain the variables naming conventions. But gosh, it hurt my fingers. :-S
The uglyness of std::remove_if approach is probably going to disappear at least partially once we get lambdas with C++0X. With C++ one is not even allowed to use a local struct for that because local structures cannot be used with templates :-(.
It's not ugly. It's missing locality of code, but it's not ugly. Between using a loop (and a conditional!) and reducing on locality of code, I choose standard algorithms. With not a moment of hesitation. Also, I think the std::remove_if() line reads perfectly fine if I choose a good name for the functor. And it's collapsed into a single line of code, too.
This is just to specify that the uglyness of std::remove_if is not your fault: you did the best that can be done with C++ and <algorithm>, and IMO still sucks because of lack of expressiveness of the C++ language. Creating a top-level class just to be able to specify a test is IMO absurd and the resulting code will be harder to read because the test will be out of context. "locality of code" is an important thing and is for example one of the main benefits of using classes! ... C++0X with the ability to pass code as [=](Person& p){return p.id==id;} will make this approach reasonable.
0

I think the best way to remove elements from an array/vector is to use a copy approach with something like:

int write_ptr = 0;
for (int read_ptr=0; read_ptr < n; read_ptr++)
{
    if (... keep element array[write_ptr] ? ...)
    {
        if (read_ptr != write_ptr)
            array[write_ptr] = array[read_ptr];
        write_ptr++;
    }
}
// The new size of the array is write_ptr

This will allow to remove even multiple elements with just one pass.

The standard library includes this approach as std::remove_if, however until C++0X arrives it's annoying to use because of limitations of the language (the code needed to be able to specify the test becomes easily quite ugly).

1 Comment

When a declarative approach is harder to read than explicit code then IMO it's better to use explicit code. Loops are not inherently bad... but of course it's stupid to use them if there are better alternatives (e.g. python list comprehensions). The uglyness of std::remove_if is not your fault, but just lacking of expressiveness of C++. STL algorithm are horrible to use because in C++ it's impossible to pass code in a reasonable way and you for example must create a top-level class just to specify a test... it's clear that this can't scale. C++0X hopefully fixes this using lambdas.
0
int i;

int Input;

cin >> Input;

 for(i = 0; i < Size ; i ++ )
 {
   if (List[i].PersonID == Input)
   {
    for(int j=i;j<size-1;j++)
    {
     List[j]=List[j+1];
    }
 }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.