|
-
May 26th, 2020, 01:34 AM
#2
Re: Erase an stl element within the or loop
 Originally Posted by pdk5
What is the nice way to do this ?
If the "list" is an STL container it comes with an erase() method which can be used to erase elements from the container. How to do it is described in the documentation for each container but as far as I know it's done in the same way for all containers using iterators and a for-loop. It means you cannot use the range-based for-loop you had in mind but that's the only change you have to make in principle.
This is the erase() documentation for std::list. See the "Erase all even numbers" example,
https://en.cppreference.com/w/cpp/container/list/erase
You can also go for the std-algorithms library. Then you end up using the Erase-Remove idiom:
https://en.wikipedia.org/wiki/Erase%...93remove_idiom
Last edited by wolle; May 26th, 2020 at 02:04 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|