lundi 24 août 2020

C++11: most efficient way to append a POD to a vector<>

Say I have a little structure with an int, double, and std:string.

I have a vector of these structures.

The efficient C solution would be to allocate the array, then loop through and set the field's values. Each value is only calculated once and is stored where its final resting place is.

In C++11 I can use emplace_back() to avoid creating a temporary structure on the stack then copying it into the vector memory, and deleting the temporary structure. But I'm providing a brace-enclosed initializer list to emplace_back() and compiler complains there's no matching function for call to emplace_back with a brace-enclosed initializer list.

So must I write a three-argument public constructor in this case? And if I did so would it be as efficient as the C version?

Aucun commentaire:

Enregistrer un commentaire