Insert or Push_Back to End of a Std: :Vector?

Is there any difference in performance between the two methods below to insert new elements to the end of a std::vector:

Method 1

std::vector<int> vec = { 1 };
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);

Method 2

std::vector<int> vec = { 1 };
int arr[] = { 2,3,4,5 };
vec.insert(std::end(vec), std::begin(arr), std::end(arr));

Personally, I like method 2 because it is nice and concise and inserts all the new elements from an array in one go. But

  • is there any difference in performance?
  • After all, they do the same thing. Don't they?
Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.