How to delete a specific item from a list only once in C ++?

advertisements

I am trying to make a simple win32 console application in C++, that would count how many matches are with same scores.

Example:
match one score is 1:1
match two score is 1:1
match three score is 1:2

The program should print in the console this:
There are 2 matches with score 1:1
There are 1 matches with score 1:2

I have created "match" object.

class match
{   

 public:
    int hostScore;
    int guestScore;

    match();
   ~match();
    void input();//This method is to input data in the object.
};

Now I want to create function to count the matches with same score.

void count(list<match> a)
{
     match game;
     int counter;

     do{
           game = a.front(); // I want to take the value of the first element in the list and assign it to "game".

           do{
                  a.deleteSpecificValueOnlyOneTime(game);// I want to delete list element with the value of "game" 1 time
                  counter++

           } while (I can DeleteSpecificValuesOneTime)

           cout<<"There are <<counter<<" matches with score <<game.hostScore<< ":" << game.guestScore<<endl;
           counter = 0;

      } while (a != a.Empty();)// While a is not empty.
}

The idea is to take the value from the first element from the list, delete it one time, count it, and repeat the process until all elements with same values are deleted and to count how many time this happen.

Take the value of the n-th element and do the same.


You could use erase method in a while loop:

std::list<match>::iterator it = a.begin();
while (it != a.end())
{
    if (*it EQUALS game)
        it = a.erase(it);
    }
    else {
        it++;
    }
}