Franky N.
Franky N.

Reputation: 79

How to delete some values in an array using c++

I have a system that read automatically the information stored on an access card of an employee in his company. These informations are stored in an array. When the card reader doesn't function, the employe have to enter his name and his Pin on the pinpad near the card reader to access the building, and the card reader will create automatically the informations to store in the array. The third first cells of the array are always filled with correct value and the rest of the cells with 0. My work is to access to this array and delete all the non-relevant zero after the third elements even though it exists a 0 before the third element, this have to be keep.

I have the following code:

    #include <iostream>  
    #include <string>      
    using namespace std;  
    int main(){  
      int const Taille=5;  
      int Track2[Taille], i;      
      Track2[0]=1;  
      Track2[1]=0;  
      Track2[2]=3;  
      Track2[3]=0;  
      Track2[4]=0;      
      cout<<"voici le contenu du tableau: \n";  
      for(i=0;i<Taille;i++){  
        if(Track2[i]!=0){  
        cout<<"Track2["<<i<<"]= "<<Track2[i]<<"\n";  
    }  
     }  
    return 0;  
    }  

When executing this I get the following result: voici le contenu du tableau: Track2[0]= 1 Track2[2]= 3 And I want to get this as result: voici le contenu du tableau: Track2[0]= 1 Track2[1]= 0 Track2[2]= 3 This means that, only the values that = 0, after the tird element of my array(here Track2[2]) have to be delete from my array. How can I do this please? Thanks

Upvotes: 0

Views: 1330

Answers (4)

Denis Ermolin
Denis Ermolin

Reputation: 5556

Use std::vector if you use C++. std::vector has erase method, use it.

#include <iostream>
#include <vector>




int main ()
{
  std::vector<int> myvector;

  // set some values (from 1 to 10)
  for (int i=1; i<=10; i++) myvector.push_back(i);

  // erase the 6th element
  myvector.erase (myvector.begin()+5);

  // erase the first 3 elements:
  myvector.erase (myvector.begin(),myvector.begin()+3);

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size(); ++i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}

sts::vector::erase documentation.

Upvotes: 3

Zane
Zane

Reputation: 926

There are ways to delete elements from an array, but you will not find a function that deletes all the elements at the end that are zero.

You need to take care of this yourself. What you need is to find the length of the array up to the last non-zero element. You can do this but traversing the array backwards:

int length;
for(length=Taille; i>=0; --length) {
   if (Track2[index] != 0) {
      break;
   }
}

lengthwill have the length of the array, and you can use this as upper bound in printing your array

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409482

Your program doesn't erase parts of the array, it should simply not print the last part of it (if I'm reading your question correct).

For that you can keep another variable, containing the last usable index, and loop until you reach that instead.

If you really want to erase entries from an array, I suggest you to use std::vector instead.

Upvotes: 1

ChrisW
ChrisW

Reputation: 56123

It's not easy to delete from an array, because an array has a fixed size (for example, 5) which is determined when the array is created.

Instead of deleting elements, copy elements down within the same array to replace the elements you don't want, for example:

// Move element 2 to element 1, erasing the old element 1
Track[1] = Track[2];
Track[2] = 0;

Another solution is to use a std::vector instead of an array. If Track2 is a vector, then you can use the vector::erase method to remove an element.

Another solution is to copy elements into a smaller array, for example:

// New temporary smaller array
int Temp[2];
// Copy elements we want to keep
Temp[0] = Track2[0];
Temp[1] = Track2[2];

Upvotes: 0

Related Questions