Search code examples
c++deque

deque.resize() from the *front*?


How do you resize a std::deque from the front instead of the back?

(Yes, of course I can push_front a dummy value thousands of times, but is there a better/more efficient way?)


Solution

  • There is an overload of insert that inserts N elements:

    std::deque<int> c;
    
    std::size_t new_size = get_new_desired_size();
    
    c.insert(c.begin(), new_size - c.size(), int());
    

    (This example requires that new_size >= c.size())