Hi, I am trying to learn about vectors and iterators, and I have just read that the .begin() and .end() functions that belong to the vector return an iterator, so i am wondering how does this actually work, for instance in a for loop (like the code below) that I have created an iterator for and set it equal to the vector.begin(), does that then set my iterator equal to the iterator that was returned by the .begin() ?
And for the vector.end() function, would that be called every loop and the iterator that i created checked against the returned end vector to determine if its the same?
Remember that iterators are very similar to pointers. The begin() returns an iterator to the beginning of the vector, end() returns a pointer to one past the end of the vector.
Like any for() loop there are three sections of the construct, the initialization section, the comparison section, and the iteration section.
Remember that iterators are very similar to pointers. The begin() returns an iterator to the beginning of the vector, end() returns a pointer to one past the end of the vector.
ok so say I call vector.begin(), a iterator returns back to me. So then is the initalisation part of the for loop basically, iterator = returned iterator ? or am I still not understanding?
In a traditional for loop for (int i = 0; i < 10; i++)
what is the initialization part of the loop? And what is the initialization value?
std::vector::begin returns an iterator (like a pointer) to the beginning of the vector, the address of the first element. std::vector::end return an iterator (like a pointer) to the end of the vector.
In this initalisation part of this. Since begin returns the an iterator to the beginning of the vector, or the address as you said. Does that then mean the iterator that i created (it) will also hold that same address, or point (I dont know if point is the correct word when talking about iterators) to that address that is holding the first element of the vector, since my iterator is set to = the iterator that is returned?
After it is created, then dereferencing it will return the first (0) element of the vector. .end() is one past the last element of the vector - so you can't deference an iterator that is .end(). When it is incremented, then dereferencing it will give the corresponding element. Note that auto is usually used with iterators. Also in general you can't rely on an iterator increasing from begin() to end() when incremented. So saying it < .end() may or not work depending upon the iterator. That's why != .end() is used. The point about iterators is that you don't need to know about the underlying container to use. If a class (container) correctly supports iterators, then general iterator code (such as the std::<algorithm> library) will work without changes.
1 2 3 4
for(auto it = vector.begin(); it != vector.end(); ++it)
{
}
You also have .cbegin() /.cend() for iterators to const data (data referenced by the iterator can't be changed), .rbegin()/ .rend() for reverse iterators so that .rbegin() is the last element and .crbegin() / .creend() for reverse iterators to const data.