iterators. iterator.begin()

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?

1
2
3
4
5
6
7
8

vector<int>::iterator it;

for(it = vector.begin(); it != vector.end(); it++)
{

}


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.

std::vector::end actually 'points' to the address of the element past the end of the container.
https://upload.cppreference.com/mwiki/images/1/1b/range-begin-end.svg

https://en.cppreference.com/w/cpp/iterator/end

The advantage of using iterators for the standard library containers is they provide a similar interface to the container, container independence.

Not all C++ containers have specific element access with an operator[].
I meant for the for loop that I had shown in my original post, with the iterator

1
2
3
4
5
6
vector<int>::iterator it;

for(it = vector.begin(); it != vector.end(); it++)
{

}


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.
Your
1
2
3
4
5
6
7
vector<int> data;

vector<int>::iterator it;
for ( it = data.begin(); it != data.end(); it++ )
{

}

and
1
2
3
4
5
6
vector<int> data;

for ( vector<int>::iterator it = data.begin(); it != data.end(); it++ )
{

}

Differ only in the lifetime of it. In your version it lives after the loop too.

In your version you assign a value to variable that was declared earlier. it = data.begin();
Assigned, as in:
1
2
int answer;
answer = 42;

In the other version variable it is declared and initialized in the initialization part of the loop.

C++11 allows compiler to calculate the type for variable, as seen in:
for ( auto it = vector.begin(); ...

If you had a plain array, then the iterator-style loop were:
1
2
3
4
5
6
7
int data[N];

int* it;
for ( it = data; it != (data + N); it++ )
{

}

if i was able to check what was stored in the memory of it and the memory of vector.begin() would they both return the same memory address?
if i was able to check what was stored in the memory of it and the memory of vector.begin() would they both return the same memory address?

After
auto it = vec.begin();
Both it and vec.begin() point to the same element.
assert(it == vec.begin());
Last edited on
Topic archived. No new replies allowed.