I’m writing a little piece of code where I’ll have to insert values into a C++ STL vector at a certain place depending on values in the vector elements. I’m using the insert() function to accomplish this. I realize that when I want to add a new element to the end of the vector, I could simply use push_back(). But to keep my code looking nice, I’d like to exclusively use insert(), which takes as input the iterator pointing to the element after the desired insertion point and the value to be inserted. If the value of the iterator passed in as an argument is v.end(), where v is my vector, will this work the same as push_back()?
Thanks a lot!
a.push_back(x)is defined to have identical semantics to(void)a.insert(a.end(),x)for sequence containers that support it.See table 68 in ISO/IEC 14882:2003 23.1.1/12 [lib.sequence.reqmts].
Regarding the running time of
vector.push_back(x)vs.vector.insert(vector.end(), x)consider the emphasized part: