Angus Comber
Angus Comber

Reputation: 9708

Difference between std::vector::front() and begin()

Help on vector says of front()

Returns a reference to the first element in the vector container. Unlike member vector::begin, which returns an iterator to this same element, this > function returns a direct reference.

Help on vector says of begin()

Returns an iterator referring to the first element in the vector container. Notice that unlike member vector::front, which returns a reference to the first element, > this function returns a random access iterator.

And this code outputs:

char arr[] = { 'A', 'B', 'C' };
vector<char> vec(arr, arr+sizeof(arr));
cout << "address of vec.front() " << (void*)&vec.front() << endl;
cout << "address of vec.begin() " << (void*)&vec.begin() << endl;

address of vec.front() 00401F90 address of vec.begin() 0030F494

I don't understand what 'direct reference' means? In the case of begin() isn't a random access iterator just a pointer?

Can someone please point out the difference?

Upvotes: 6

Views: 8162

Answers (5)

Mohamed S.Shelf
Mohamed S.Shelf

Reputation: 325

To keep it clear in your mind, always try to remember the following two lines assuming that you have a vector from STL called v:

v.front() = * v.begin()
&&
v.back() = * v.end()

that means if you have a vector:

v = {1,2,3,4};

and you have an iterator called IT, and you want to access the first and the last element you can either do:

IT = v.begin();
std::cout<<*IT<<std::endl; // output : 1
IT = v.end();
std::cout<<*IT<<std::endl; // output : 4

or you can easily do this:

std::cout<<v.front<<std::endl; // output  : 1
std::cout<<v.back<<std::endl; // output  : 4

both will print the same output, the difference is just between if you want to use an iterator or not.

Upvotes: 0

Pete Becker
Pete Becker

Reputation: 76418

For a vector, begin() and end() return random access iterators. They might return a plain pointer; that's okay, because it meets the requirements to be a random access iterator. In particular, you can write *begin() to get a reference to the first object in the sequence (assuming there is one). front() gives you a reference to the first object in the sequence, without going through the intermediate iterator. Like this:

vector<int> v;
v.push_back(3);
int i = *v.begin(); // i == 3
int j = v.front();  // j == 3

Upvotes: 4

bn.
bn.

Reputation: 7949

According to Stroustrup in The C++ Programming Language, Section 16.3.3; think of front() as the first element and begin() as a pointer to the first element.

Upvotes: 16

j_random_hacker
j_random_hacker

Reputation: 51246

Assuming you have at least 1 element in the vector,

vec.front()

is the same as

*vec.begin()

Upvotes: 3

Luchian Grigore
Luchian Grigore

Reputation: 258618

In the case of begin() isn't a random access iterator just a pointer?

No, an iterator has some pointer semantics, but it's actually a class.

And even if it was, that should answer the question. It's like asking why the address of a pointer isn't the same as the address of the object it points to.

You'd get the same value if you dereference the iterator, which will give you the first element:

&(*vec.begin())

because

*vec.begin() == vec.front()

Upvotes: 8

Related Questions