I need to insert onto the end of a vector, 2 elements each time.
I was wondering whether doing vector.insert( iter_first, iter_second) is faster than doing vector.push_back( first ) then vector.push_back( second )?
Maybe another alternative would be to create a struct like this:
struct T{int a; int b;};
T t;
t.a = first;
t.b = second;
X.push_back(t);
If the two elements are logically related, then you would use a struct or class. If they’re unrelated then certainly don’t group them for performance reasons.
This smells of premature optimization. Instead, write the code that’s clearest to maintain and let the compiler generate the code for you. That’s what it’s there for. Consider that code that’s easy to read is easy to maintain, and code that’s easy to maintain is easy to refactor and less likely to have subtle performance bugs due to misunderstandings.