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();