Fast way to implement pop_front to a std::vector

前端 未结 4 732
遇见更好的自我
遇见更好的自我 2021-02-01 17:25

I\'m using some classes and several utility methods that use std:: vector.

Now I need to use each frame a pop_front - push_back method on one of those classes (but they

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 17:48

    if you just try to erase the first element then in the function use:

    if (my_vector.size()){ //check if there any elements in the vector array
        my_vector.erase(my_vector.begin()); //erase the firs element
    }
    

    if you want to emulate pop front for the entire vector array ( you want to keep pop out every element from start to end) , i suggest on:

    reverse(my_vector.begin(),my_vector.end());  // reverse the order of the vector array
    my_vector.pop_back();   // now just simple pop_back will give you the results
    

提交回复
热议问题