Consider the following code -
{
int n = 3;
n = 5;
std::vector<int> v = { 1, 2, 3 .... 3242};
v = std::vector<int>{10000, 10001, 10002... 50000};
}
for primitive data types like int
, it can simply overwrite the previous memory location with the new value and get away with it. However what happens to types like std::vector
, where sizes of previous value might not be same as the new assignment.
In other words, what happens to the part { 1, 2, 3 .... 3000}
when v is reassigned to new {10000, 10001, 10002... 50000}
value. Does it simply throw away the previous value and reassign the internal pointers to new locations? Or does it overwrites the previous locations with new data as much as it can, and either reallocates new memory in case of large assignment or clears out existing memory in case of shorter assignment thus preserving the capacity()
of initial vector?
Would this be preferable anywhere over clearing out the contents (.clear()
) instead because I saw this type of code somewhere?
Aucun commentaire:
Enregistrer un commentaire