Understanding .begin and .end in vectors

c++ vector
vector in
vector functions
vector.begin() + 1
c++ vector length
how to take input in vector
how to return a vector in c++
how to implement vector in c++

I'm learning vectors from this post and they start off with iterators. They define .begin and .end as follows:

begin() – Returns an iterator pointing to the first element in the vector

end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector

And then they give the following code snippet, I added the 3rd for loop to express my question.

#include<iostream>
#include<vector>

int main() {
    std::vector <int> g1; //creating a vector
    for (int i = 1; i <= 3; i++){
        g1.push_back(i);
    }
    std::cout << "Output of beginning and end values: ";
    for (auto i = g1.begin(); i != g1.end(); i++) {
        std::cout << *i << " "; 
    }
    std::cout << "\nOutput of beginning and end addresses: ";
    for (auto i = g1.begin(); i != g1.end(); i++) {
        std::cout << &i << " ";
    }
     //"Output of beginning and end values: 1 2 3"
     //"Output of beginning and end addresses: 0105FB0C 0105FB0C 0105FB0C"
    return 0;
}

My confusion is that the address of i stays the same, but the value that i has changed. Doesn't i* mean i is just dereferenced? So something has to be changing the value of i if it's address is not changing so that it would be able to have a different value. I think I may be confusing iterators with pointers. I know that auto is basically type inference but that's about it.

So my question is, how does the value of i change, if it's address is the same for every element in the vector?

&i is the address of the local variable i. This will not change. *i dereferences the iterator, and returns the value at that element of the vector. &*i will return a pointer to the element in the vector.

So you loop should use

std::cout << &*i << " ";

to see the addresses change.

Understanding .begin and .end in vectors, &i is the address of the local variable i . This will not change. *i dereferences the iterator, and returns the value at that element of the vector. They define .begin and .end as follows: begin () – Returns an iterator pointing to the first element in the vector. end () – Returns an iterator pointing to the theoretical element that follows the last element in the vector. And then they give the following code snippet, I added the 3rd for loop to express my question.

To make it more clear consider a similar code but instead of a vector there is used an array.

#include <iostream>
#include <iterator>

int main() 
{
    int a[] = { 1, 2, 3 };

    for ( auto p = std::begin( a ); p != std::end( a ); p++ )
    {
        std::cout << "The address of p is " << &p
                  << ", its value is " << p
                  << ", and the pointed value is " << *p
                  << '\n';
    }

    return 0;
}

The program output might look like

The address of p is 0x7ffcaf6a6830, its value is 0x7ffcaf6a683c, and the pointed value is 1
The address of p is 0x7ffcaf6a6830, its value is 0x7ffcaf6a6840, and the pointed value is 2
The address of p is 0x7ffcaf6a6830, its value is 0x7ffcaf6a6844, and the pointed value is 3

This part of the outputted strings

The address of p is 0x7ffcaf6a6830

is not being changed because it is the address of the local variable (pointer) p itself.

This part of the strings

its value is 0x7ffcaf6a683c

is being changed because within the loop the value of the pointer is changed

for ( auto p = std::begin( a ); p != std::end( a ); p++ )
                                                        ^^^^

In each iteration the pointer points to the next element of the array.

This part of the strings

and the pointed value is 1

is also being changed because there is outputted the pointed value due to dereferencing the pointer.

The same things take place with the iterator of a vector.

vector::begin() and vector::end() in C++ STL, It contains well written, well thought and well explained computer science and programming vector::begin() and vector::end() in C++ STL. vector::begin() function is a bidirectional iterator used to return an iterator pointing to the first element of the container. vector::end() function is a bidirectional iterator used to return an iterator pointing to the last element of the container. Algorithm Begin Initialize the vector v. Declare the vector v1 and iterator it to the vector.

I think I may be confusing iterators with pointers

Well, pointers are iterators, so this is understandable. But not all iterators are pointers. In other words, non-pointers can also be iterators.

I know that auto is basically type inference but that's about it.

In this case, the type is deduced to be std::vector<int>::iterator because that is the type that std::vector<int>::begin returns.

So my question is, how does the value of i change, if it's address is the same for every element in the vector?

The value of i is changed by the i++ in the loop. The address where an object is stored never changes through the lifetime of the object. Regardless of whether the type of that object is pointer, some other iterator or not an iterator at all.

The Basics of C++ Vector Explained With Examples, vector::end() returns an iterator to point at past-the-end element of a C++ vector. vector::cbegin() is similar to vector::begin() , but without the ability� In the above example, we can see the use of begin () and end () function. First, we define a vector vec1, we push back values in it from 1 to 10 using a for a loop. Then we print the values of our vectors using for loop, we use begin ( ) and end ( ) function to specify the start and endpoint of our for a loop.

What are Vectors in C++ ? All You Need to Know, Vectors in C++ are the dynamic arrays that are used to store data. Unlike arrays cout << "Understanding begin() and end() function: " << endl;. One of the most important and fundamental concepts in linear algebra is the vector.Luckily, vectors are all around us, but they are, in general, not visible. The common ways to introduce a vector is either to begin with the strict mathematical definition, or to discuss examples of vectors, such as velocities, forces, acceleration, etc.

C++ Vectors, vector<int> values (5); // Declares a vector of 5 integers vector<double> To accomplish this, we use the begin and end methods of vector. These methods give� Returns an iterator referring to the past-the-end element in the vector container. The past-the-end element is the theoretical element that would follow the last element in the vector. It does not point to any element, and thus shall not be dereferenced.

Understanding how Vectors work in C++ (Part-2): What happens , That is, we will start from calling a vector constructor and then see how passing 2 iterators ( __l.begin(), __l.end() ) and 1 forward iterator tag. When you add two vectors, it is as if you took the vectors and placed them end to end and created a new vector running from the starting point to the end point. If the vectors have the same direction, then this just means adding the magnitudes, but if they have different directions, it can become more complex.

Comments
  • i is an iterator, which acts "like" a pointer. It contains a handle of some sort (could be a pointer, could be an index) which can be used to (a) move through the collection and (b) location the current element of the collection. If you looked at g1[i] and &i in the first loop, would you still have the same questions? If not, why?
  • Imagine if i was just a int*. It may help understand what is happening. Note that unlike other containers, T* is a valid iterator type for std::vector<T> so the comparison is pretty accurate.
  • "So something has to be changing the value of i if it's address is not changing so that it would be able to have a different value" - that would be the i++ statement in the loop. It advances the iterator to refer to the next element in the vector
  • Thank you! I see the addresses changing but they're 4 bytes apart. i.e. 00F6E3B8 00F6E3BC 00F6E3C0. Is that because an ìnt occupies 4 bytes by default?
  • @S.Ramjit Yes, an int is 4 bytes on your system. Other systems may have a different size.