Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erase all elements from vector until first non-zero element C++

Tags:

c++

vector

I have a vector that will store a variable number of zero elements at its beginning. These need to be erased.

I have tried:

while(v.at(0) == 0)
{
    v.erase(v.begin());
}

But this throws out an std::out_of_range error.

Any assistance would be appreciated.

like image 851
MJV Avatar asked Aug 13 '18 11:08

MJV


People also ask

How do you clear all vector elements?

All the elements of the vector are removed using clear() function. erase() function, on the other hand, is used to remove specific elements from the container or a range of elements from the container, thus reducing its size by the number of elements removed.

How do I erase an element from std::vector <> by value?

You need to use std::remove algorithm to move the element to be erased to the end of the vector and then use erase function. Something like: myVector. erase(std::remove(myVector. begin(), myVector.

How do I remove multiple elements from a vector file?

If you need to remove multiple elements from the vector, the std::remove will copy each, not removed element only once to its final location, while the vector::erase approach would move all of the elements from the position to the end multiple times.

How to delete an element from a vector in C++?

Get the vector and the element to be deleted Initialize a reverse iterator on the vector Erase the required element with the help of base () and erase () Reason for using base (): erase () returns a valid iterator to the new location of the element which follows the one, which was just erased, in a forward sense.

What is the difference between erase and remove in a vector?

What happens is that remove compacts the elements that differ from the value to be removed ( number_in) in the beginning of the vector and returns the iterator to the first element after that range. Then erase removes these elements (whose value is unspecified). Show activity on this post.

How to remove the first element of a vector in Python?

//Now the vector (v) has 2 elements in it, with size 2 v.pop_front(); // This method will remove the first element for(int i=0;i<v.size();i++){ cout << v[i] << " "; } //Prints [1] return 0; } Removes either a single element or a range of elements.

What is the difference between remove () and erase () in C++?

What happens is that remove compacts the elements that differ from the value to be removed ( number_in) in the beginning of the vector and returns the iterator to the first element after that range. Then erase removes these elements (whose value is unspecified). std::remove () shifts elements such that the elements to remove are overwritten.


1 Answers

You can use std::find_if to find the first non-zero value and then erase those elements:

auto first_non_zero = std::find_if(begin(v), end(v), [](int n){ return n != 0; });
v.erase(begin(v), first_non_zero);

(live demo)

like image 175
YSC Avatar answered Nov 15 '22 07:11

YSC