469,086 Members | 1,273 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,086 developers. It's quick & easy.

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 2184
dev7060
485 Expert 256MB
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.