Thread: Searching Array

  1. #1
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499

    Searching Array

    I am having some problems with this function I am trying to write and could use a little help. I have an array of pointers to base class Person as.......Person* people[count] which is used to create and store objects of 3 derived classes. I am trying to figure out how I can remove all objects of a certain type from the array.
    I have been trying something like the following but this does not work.

    Code:
     
    for(i=0;i<count;i++)	
    {
    	if(typeid(*people[i])==typeid(Student))
    	{
    	   for(j=i;j<count-1;j++)
                   people[j]=people[j+1];
                count--;
    	}
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Assuming you allocated each person in the people array with new, then your code should work.

    But it should also free up memory as stuff is moved.
    Code:
    for(i=0;i<count;i++)	
    {
    	if(typeid(*people[i])==typeid(Student))
    	{
    	   delete people[i];
    	   for(j=i;j<count-1;j++)
                   people[j]=people[j+1];
                count--;
    	}
    }

  3. #3
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    I am allocating with new and I knew that I should be freeing the memory as I remove elements of the array. I tried it with the delete after the if statement and I get an abnormal program termination. MSVC++6.
    Code:
    switch(p) 
    { 
       case 1:for(i=0;i<count;i++)
       	      if(typeid(*people[i])==typeid(Student))
    	      {
    		 delete people[i]; 
    		 for(j=i;j<=count-1;j++)
    		     people[j]=people[j+1];
                     count--;
    	      }break; 
       case 2:for(i=0;i<count;i++)	
       	      if(typeid(*people[i])==typeid(Teacher))
    	      {
    		 delete people[i]; 
    		 for(j=i;j<=count-1;j++)
    		     people[j]=people[j+1];
                     count--;
    	      }break; 
       case 3:for(i=0;i<count;i++)	
       	      if(typeid(*people[i])==typeid(Manger))
    	      {
    		 delete people[i]; 
    		 for(j=i;j<=count-1;j++)
    		     people[j]=people[j+1];
                     count--;
    	      }break;
    }
    This is basically what I am trying to do(above). I have gotten it to work to remove one type without using delete. But say after removing all students, I want to go back in and remove all teachers.....that's just not happening for me. I have drawn this out several times and on paper it seems like it should work just fine. Any ideas on what I am doing wrong here?
    Last edited by DISGUISED; 04-12-2002 at 10:40 AM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Does you people allocation look anything like this? I know you are using classes, so the allocation may be in a constructor. I did a simple example which looked like this:
    Code:
    Person *people[10];
    .
    .
    count = 0;
    for (i=0; i<10; i++)
    {
       people[count] = new Person;
       people[count]->Id = i;
       count++;
    }

  5. #5
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    I just have menu selection of what type of person to create then pass an integer to a function and allocate like this.

    Code:
     
    if(n == 1)
       people[count] = new Student; 
    else if(n == 2) 
    	people[count] = new Teacher; 
         else 
    	people[count] = new Manager;
    people[count++]->getData();
    This isn't in a loop, I am just creating one at a time, and of course getData() is virtual. I got the delete to run now so I am making progress but I still can not get this to do exactly what I want it to do. I just want to be able to fill the array with people...then delete all the students, then delete all the teachers and so on. Right now I can delete all of one type but it won't work when I go back in to delete another type which makes no sense to me. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. searching thru a c++ class array
    By stanleyw in forum C++ Programming
    Replies: 1
    Last Post: 05-29-2002, 09:15 PM