Fast way to implement pop_front to a std::vector

前端 未结 4 734
遇见更好的自我
遇见更好的自我 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:51

    I also have a way... Not so good tho..

    This looks like @0xPwn's solution, but he didn't reverse the vector the second time. You will probably understand this code, so i will not explain it.

    #include 
    #include 
    template 
    void pop_front(std::vector& vec){
        std::reverse(vec.begin(),vec.end()); // first becomes last, reverses the vector
        vec.pop_back(); // pop last
        std::reverse(vec.begin(),vec.end()); // reverses it again, so the elements are in the same order as before
    
    }
    

提交回复
热议问题