My question is: First of all,I know the error is because the iterator exceeds after-end iterator. Generics algorithms which require three iterators always assume that destination sequences at least have the same length as source sequences.In other word,destination sequences must be able to contain all elements from source sequences.But, "The same length" here refer to what ?
1. The quantity of actual elements between source sequences and destination sequences is the same ?
2. The quantity of memories(not actual element is required) that source sequences and destination sequences have is the same ?
If it requires the same quantity of actual elements. Why doesn't the first code report errors ? I use std::allocator to allocate 3 memories. And these memories are uninitialized, there are no elements constructed in these memories. But std::uninitialized succeed in copying the elements from source sequences to destination sequences.
If it requires the same quantity of memories(not actual element is required). Then why the second code report an error ? At first, ivec_2 is empty. And I call reserve(3) to allocate 3 memories previously for it. I even call capacity() to make sure that it actually allocates 3 memories.And there are no actual elements in ivec_2.But this time std::uninitialized_copy doesn't work and report an error.
First of all,I know the error is because the iterator exceeds after-end iterator.
Doesn't this answer your question?
You're not supposed to do anything with memory that has been reserved but is currently not used to store any elements in the vector. This has nothing to do with std::uninitialized_copy.
ivec_2.reserve(3);
This doesn't change the size of the vector or the end-iterator position. It simply means that any future reallocation won't occur until (at least) this limit is breached.
You're not supposed to do anything with memory that has been reserved but is currently not used to store any elements
, when I use std::allocator to allocate memories,these memories are currently not used to store any elements either and they are uninitialized , but I can use std::uninitialized_copy to copy elements to these memories. I just feel strange.
Debug versions of the library may be (typically would be) using checked / safe iterators. So there would be a check to verify that we are not going out of bounds (beyond the logical end) of the vector.