472,119 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

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>::iterator i=a[v].begin(); i!=a[v].end(); i++)
{ cout << *i << " ";
k.remove(*i);}

Please help. Thanks in advance
Jul 5 '21 #1
2 2834
dev7060
624 Expert 512MB
myset is a user defined class. I am trying to delete the element using "remove" but it gives an error

Expand|Select|Wrap|Line Numbers
  1. for (list<myset>::iterator i=a[v].begin(); i!=a[v].end(); i++)
  2. { cout << *i << " ";
  3. 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.
Jul 6 '21 #2
iamkajal
2 2Bits
you should try this

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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<list> // for list operations
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     // initializing list of integers
  8.     list<int> list1={10,15,20,25,30,35};
  9.  
  10.     // declaring list iterators
  11.     list<int>::iterator it = list1.begin();
  12.     list<int>::iterator it1 = list1.begin();
  13.  
  14.     // incrementing the positions of iterators
  15.     advance(it,2);
  16.     advance(it1,5);
  17.  
  18.     // printing original list
  19.     cout << "The original list is : ";
  20.     for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
  21.        cout << *i << " ";
  22.  
  23.     cout << endl;
  24.  
  25.     // using erase() to erase single element
  26.     // erases 20
  27.     list1.erase(it);
  28.  
  29.     // list after deletion 1 element
  30.     cout << "The list after deleting 1 element using erase() : ";
  31.     for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
  32.        cout << *i << " ";
  33.  
  34.     cout << endl;
  35.  
  36.     it = list1.begin();
  37.  
  38.     // incrementing the positions of iterators
  39.     advance(it,2);
  40.  
  41.     // using erase() to erase multiple elements
  42.     // erases 25,30
  43.     list1.erase(it,it1);
  44.  
  45.     // list after deletion of multiple elements
  46.     cout << "The list after deleting multiple elements using erase() : ";
  47.     for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
  48.        cout << *i << " ";
  49.  
  50.     cout << endl;
  51.  
  52.  
  53. }
Jul 7 '21 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by chreo | last post: by
17 posts views Thread by Steve R. Hastings | last post: by
3 posts views Thread by Hallvard B Furuseth | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.