What do these terminologies mean in C++?
1. off the end value
2. half open range – [begin, off_the_end)
I came across them while reading about for loops.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A half-open range is one which includes the first element, but excludes the last one.
The range [1,5) is half-open, and consists of the values 1, 2, 3 and 4.
"off the end" or "past the end" refers to the element just after the end of a sequence, and is special in that iterators are allowed to point to it (but you may not look at the actual value, because it doesn’t exist)
For example, in the following code:
firstnow points to the first element of the array, whilelastpoints one past the end of the array. We are allowed to point one past the end of the array (but not two past), but we’re not allowed to try to access the element at that position:Our two pointers,
firstandlasttogether define a range, of all the elements between them.If it is a half open range, then it contains the element pointed to by
first, and all the elements in between, but not the element pointed to bylast(which is good, because it doesn’t actually point to a valid element)In C++, all the standard library algorithms operate on such half open ranges. For example, if I want to copy the entire array to some other location
dest, I do this:A simple for-loop typically follows a similar pattern:
This loop goes from 0 to 4, but it excludes the end value, so the range of indices covered is half-open, specifically
[0, 4)