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?
vector::front()This function can be used to fetch the first element of a vector container.
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.
begin: Returns an iterator pointing to the first element in the sequence. cend: Returns a const_iterator pointing to the past-the-end element in the container. end: Returns an iterator pointing to the past-the-end element in the sequence.
In vector, each element only requires the space for itself only. In list, each element requires extra space for the node which holds the element, including pointers to the next and previous elements in the list.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With