Copy function arguments in C ++

I am reading this book called C ++ Accelerated. For the โ€œcopyโ€ shown below

// error - no element at ret.end() copy(bottom.begin(), bottom.end(), ret.end()); 

The book mentions that it is not right to use ret.end () as the third argument. But ret.end () returns an iterator for the last one element of the 'ret' container. What is the problem with this argument? They suggest using back_inserter (ret) instead. Why is this so?

+4
source share
1 answer

The problem with ret.end is that although it points to one end of the end of the container, nothing can be highlighted at this place or outside. Since writing to memory locations that were not allocated for your program is undefined behavior, you really should use back_inserter .

+8
source

All Articles