I'm reading this book called accelerated c++.For 'copy' shown below
// error - no element at ret.end()
copy(bottom.begin(), bottom.end(), ret.end());
It is mentioned in the book that it is not quiet right to use ret.end() as the third argument.But ret.end() returns the iterator for one past the last element of 'ret' container.What is the problem with this argument?They are suggesting to use 'back_inserter(ret)' instead.Why is that so?
The problem with ret.end
is that although it points to one past the end of the container, there may be nothing allocated at or beyond that location in memory. Since writing to memory locations that have not been allocated to your program is undefined behavior, you should indeed use back_inserter
.
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