Dave's Brain

Browse - Programming Tips - How do I prepend to a std::vector ?

Date: 2017aug24
Language: C++
Keywords: push_back, push_front, append

Q.  How do I prepend to a std::vector<> ?

A.  Its slow but this is how you do it:

std::vector<int> a; a.push_back(200); // Append at the end a.insert(a.begin(), 100); // Insert at the beginning
If you need to do this a lot use a different container like std::list or std::deque. They have push_front();

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.