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 Vec = { 1 }...
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);
Must Read
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?