Say I have the following:
std::vector<int> myints;
and then I have a function that returns an int vector:
std::vector<int> GiveNumbers()
{
std::vector<int> numbers;
for(int i = 0; i < 50; ++i)
{
numbers.push_back(i);
}
return numbers;
}
could I then do:
myints = GiveNumbers();
would doing this safely make it so that myints has the numbers 0 to 49 in it and nothing else? Would doing this clear what could have been in myints previously? If not whats the proper way to do this?
Thanks
Yes. This is safe. You will be copying the results from your GiveNumbers()
function into myints
. It may not be the most efficient way to do it, but it is safe and correct. For small vectors, the efficiency differences will not be that great.
Yes, it will assign it and it will clear what was in the receiving vector previously.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With