Dynelight
Dynelight

Reputation: 2162

Removing once from C++ List

I have the following example:

// remove from list
#include <iostream>
#include <list>
using namespace std;

int main ()
{
  int myints[]= {17,89,89,7,14};
  list<int> mylist (myints,myints+5);

  mylist.remove(89);

  cout << "mylist contains:";
  for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
        cout << " " << *it;
  cout << endl;

  return 0;
}

The result is:

17, 7, 14

The problem with this is that it removes both instances of 89. Is there any easy way to just remove one instance of 89?

Upvotes: 0

Views: 92

Answers (1)

Lorlin
Lorlin

Reputation: 884

No, there isn't. You might do however:

mylist.erase(find(mylist.begin(), mylist.end(), 89));

Upvotes: 1

Related Questions