Domanda

Do I need to reserve memory when using a back:inserter?

d.reserve(s.size())
std::copy (s.begin(),s.end(),back_inserter(d));
È stato utile?

Soluzione

You do not need to reserve memory for the container when using std::back_inserter. However, if you know beforehand about the number of elements you are going to insert and want to prevent repeated allocations you may reserve memory.

Altri suggerimenti

You don't have to. However, if this code snippet is run very frequently, you may consider reserving memory to improve performance.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top