deletion of user defined <list>

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kathlyn
    New Member
    • Jul 2021
    • 1

    deletion of user defined <list>

    myset is a user defined class. I am trying to delete the element using "remove" but it gives an error

    for (list<myset>::i terator i=a[v].begin(); i!=a[v].end(); i++)
    { cout << *i << " ";
    k.remove(*i);}

    Please help. Thanks in advance
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    myset is a user defined class. I am trying to delete the element using "remove" but it gives an error

    Code:
    for (list<myset>::iterator i=a[v].begin(); i!=a[v].end(); i++)
    { cout << *i << " ";
    k.remove(*i);}
    Please help. Thanks in advance
    - What error?
    - What's a[v]?
    - What's with the usage of *i?
    - What's k?
    - 'remove()' removes all the elements in the list container that are equal to the value passed in the argument. But it probably needs an operator== implementation to know how to compare the two objects.

    Comment

    • iamkajal
      New Member
      • Jun 2021
      • 2

      #3
      you should try this

      / C++ code to demonstrate the working of erase()

      Code:
      #include<iostream>
      #include<list> // for list operations
      using namespace std;
        
      int main()
      {
          // initializing list of integers
          list<int> list1={10,15,20,25,30,35};
            
          // declaring list iterators
          list<int>::iterator it = list1.begin();
          list<int>::iterator it1 = list1.begin();
            
          // incrementing the positions of iterators
          advance(it,2);
          advance(it1,5);
            
          // printing original list
          cout << "The original list is : ";
          for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
             cout << *i << " ";
             
          cout << endl;
            
          // using erase() to erase single element
          // erases 20
          list1.erase(it);
            
          // list after deletion 1 element
          cout << "The list after deleting 1 element using erase() : ";
          for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
             cout << *i << " ";
             
          cout << endl;
            
          it = list1.begin();
            
          // incrementing the positions of iterators
          advance(it,2);
            
          // using erase() to erase multiple elements
          // erases 25,30
          list1.erase(it,it1);
            
          // list after deletion of multiple elements
          cout << "The list after deleting multiple elements using erase() : ";
          for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
             cout << *i << " ";
             
          cout << endl;
        
            
      }
      Last edited by Niheel; Jul 7 '21, 10:59 AM.

      Comment

      Working...