C++Builder How To Use C++ front() And back() Methods Of Vectors

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Use C++ front() And back() Methods Of Vectors
By Yilmaz Yoru October 20, 2021

What are the Vectors in C++? How can I use front() iterator method on vectors? How can I use back() iterator method on vectors? Can I get the last member of a vector by using back() method? How can I access to the first element of a vector? Can I print out the first and the last members of vectors by using front() and back() methods? Let’s answer these questions.

Для просмотра ссылки Войди или Зарегистрируйся and Для просмотра ссылки Войди или Зарегистрируйся in C++ are explained well in our previous posts. These were coming from the C, in this post, we will explain another great feature of C++, The Vectors.

Vectors are dynamic arrays included in <vector> library in modern C++ and they can resize themselves automatically when a member of a vector is inserted or deleted. Vectors are the same as dynamic arrays and these dynamic arrays of vectors are handled automatically by the container. Vectors are the way of Modern C++, their members are placed in the contiguous memory storage, thus it can be resized, and it can be accessed and traversed using iterators.

When we Insert data into vectors, this may take a time than other static arrays because of the need of extending the vector array. Vectors have low memory usage as in dynamic array implementations, because of having good data cache utilization and locality of reference. We can easily access an element of a vector by giving its index between ‘[‘ and ‘]’ as same as arrays, which means vector members can be referenced by indices.

Vectors allow random access; that is, an element of a vector may be referenced in the same manner as elements of arrays (by array indices). Linked lists and sets, on the other hand, do not support random access or pointer arithmetic. Vectors are very useful for storing data in lists whose number of elements (length in total) may not be known before setting up the list. Because the vector data structure allocates the necessary memory needed for specific data storage Erasing and clearing vector elements from a vector does not need to free any of the memory associated with that element. That makes vectors much more safe and modern in C++ than arrays.

A vector can be defined in this syntax,
C++:
std::vector<object type> variable_name;
Now let’s see how we can use iterator methods of vectors.

What are Iterator methods of C++ Vectors?​

Iterator (<iterator>) is an object that points an element in a range of elements (i.e. characters of a wide string). We can use Iterators to iterate through the elements of this range using a set of operators ( for example ++, –, * operators).

begin() method returns an iterator pointing to the first element in the vector
end() method returns an iterator pointing to the theoretical element that follows the last element in the vector
rbegin() method returns a reverse iterator pointing to the last element in the vector. It moves from last to first element
rend() method returns a reverse iterator pointing to the theoretical element preceding the first element in the vector
cbegin() method returns a constant iterator pointing to the first element in the vector.
cend() method returns a constant iterator pointing to the theoretical element that follows the last element in the vector.
crbegin() method returns a constant reverse iterator pointing to the last element in the vector. It moves from last to first element
crend() method returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector

How can I access the First Member of a C++ Vector with the front() method?​

What is the front() method?​

front() Method of std::vector is an element access method that returns a reference to the first element in the container. Calling front on an empty container is undefined.

Syntax of front() Method of std::vector:
C++:
const_reference front() const;
Simple front() example for a vector:


We can access to the value of the first vector member by using front() method as below,
C++:
std::vector<float> vec{ 1.5, 2.8, 3.6};
float front_value = vec.front();
Here, the front_value will be equal to the first member value which is 1.5 in this example.

How to Access to the Last Member of a C++ Vector with back() Method​

What is the back() Iterator method?​

back() Method of std::vector is an element access method that returns a reference to the last element in the container. Calling back on an empty container is undefined.

Syntax of back() Iterator Method of std::vector:
C++:
const_reference back() const;
Simple back() example for a vector:

We can reach to the value of the last vector member by using the back() method as below;
C++:
std::vector<float> vec{ 1.5, 2.8, 3.6};
float last_value = vec.back();
Here, the last_value will be equal to the last member value which is 3.6 in this example.

Can you show me an example of the front() and back() C++ Iterator methods?​

If you really want to reach to the first or last element, we should use front() and back() methods, do not use . Note that in the std::vector, the begin() and the end() methods are Iterators while the front() and the back() methods are reference elements of vectors. as below,
C++:
#include <iostream>
#include <vector>
 
int main()
{
 std::vector<float> vec{ 1.5, 2.8, 3.6};;  // Vector in floats
 
 float front_value = vec.front();
 float back_value = vec.back();
 
 std::cout << "Front Value:" << front_value << std::endl;
 std::cout << "Back Value:"   << back_value  << std::endl;
 
 getchar();
 return 0;
}