CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #2
    Join Date
    Feb 2017
    Posts
    674

    Re: Erase an stl element within the or loop

    Quote Originally Posted by pdk5 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured