c++ - removing last elements from vector until condition -


I have a problem where I need to remove the last element of a certain condition, for example, this element is not zero )

I have written this code, this is the trick -

  auto next = vec.rbegin (); While (next! = Vec.rend ()) {auto current = next ++; If (* current == 0) vec.pop_back (); And break; }  

But I will use more than one STL algorithm which I can use (I can use find_if and then erase it, but I can loop once ) I want to remove the elements that I remove ...)

Besides, I am afraid that I can use some UB here, should I worry?

Your code can be simple:

  while (! .empty ()) & amp; Vec.back () == 0) Vec.pop_back (); By using   std :: removal  or  std :: removal_if , all the elements will be removed depending on the criteria, so that you get the  Std :: find_if < / Code> As Vlad has been provided in its reply. 


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -