## Class vector. Methods that provide access to array elements. Methods at(), front(), back(), data(), begin(), end(), cbegin(), cend(), rbegin(), rend(), crbegin(), crend()

Before exploring this topic, it is recommended that you familiarize yourself with the following topic:

### Contents

Search other resources:

##### 1. Method at(). Get the element of a vector by its position

The at() method is used to access a specific element of an array based on a given index. The method has 2 overloaded implementations

```const T& at(const size_t _Pos) const
T& at(const size_t _Pos)```

here T – the type of the array elements.

The first implementation is used to read an element from an array. The second implementation is used to change an element of an array.

Example.

```...

// Method at() - get the element of the vector by its position
vector<double> A(5); // Create an array of 5 elements of type double

// Fill with arbitrary values
A[0] = 2.8;
A[1] = 3.3;
A[2] = 4.5;
A[3] = 7.3;
A[4] = 8.8;

// Read the item at index 2
double x = A.at(2); // x = 4.5

// Change the value of the item at index 2
A.at(2) = 3.7;

...```

### ⇑

##### 2. Method front(). Returns a reference to the first element of the vector

Using the front() method, you can get a reference to the first element of the array. The syntax for declaring two overloaded method implementations is as follows

```const T& front() const
T& front()```

The first implementation of the method allows you to read the value of the first element of the array. The second implementation allows you to write values to the first element of the array.

Example.

```...

// The front() method - get the value of the first element of the array.
// 1. Declare an array of 5 floats
vector<float> A(5);

// 2. Fill the array with data
A[0] = 2.88f;
A[1] = 3.8f;
A[2] = 4.4f;
A[3] = 4.9f;
A[4] = 5.5f;

// 3. Read and display the value of the first element of the vector
float x = A.front();
cout << x << endl; // 2.88

...```

### ⇑

##### 3. Method back(). Returns a reference to the last element of the vector

The back() method is used to access the last element of the vector. This method has 2 overloaded implementations, the declaration syntax for which is as follows

```const T& back() const
T& back()```

The first implementation is used when you need to read the value from the last element of the vector. The second implementation is used when you need to write a value to the last element of a vector.

Example.

```...

// The back() method is to get the value of the last element of the array.
// 1. Declare an array of 5 elements of type float
vector<float> A(5);

// 2. Fill the array with data
A[0] = 2.88f;
A[1] = 3.8f;
A[2] = 4.4f;
A[3] = 4.9f;
A[4] = 5.5f;

// 3. Read and display the value of the last element of the array
float x = A.back();
cout << x << endl; // 5.5

// 4. Change the last element of an array
A.back() = 7.9f; // A[4] = 7.9
cout << A[4] << endl; // 7.9

...```

### ⇑

##### 4. Method data(). Get a pointer to a vector

The data() method allows you to get a pointer to a dynamic array. With this pointer, you can access the elements of the vector as if you were an ordinary array.

The syntax for declaring a method is as follows:

`T* vector<T>::data()`

The syntax for declaring a method is as follows:

Example.

```...

// Method data() - get the pointer to the vector.
// 1. Declare a vector of 10 elements of type short.
vector<short> A(10);

// 2. Fill the array with data
for (int i = 0; i < A.size(); i++)
A.at(i) = i;

// 3. Get a pointer to a vector
short* p = A.data();

// 4. Use the p pointer to display the elements of the vector on the screen
for (int i=0; i<A.size(); i++)
cout << *(p + i) << " ";

// 5. Print the element of the vector with index 2
cout << endl;
cout << p[2] << endl; // 2

...```

### ⇑

##### 5. Method begin(). Return an iterator pointing to the first element of the vector

The begin() method returns an iterator pointing to the first element of the dynamic array. The method has the following general form:

`vector<T>::iterator vector<T>::begin()`

here T is the type of the elements of the array.

Example.

```...

// Method begin() - returns an iterator set to the first element of a vector
// 1. Declare a vector of 5 elements of type char
vector<char> A(5);

// 2. Fill array with data
A[0] = 'a';
A[1] = 'b';
A[2] = 'c';
A[3] = 'd';
A[4] = 'e';

// 3. Declare an iterator on an array of char elements
vector<char>::iterator pA;

// 4. Set the iterator to the beginning of the array and print the first element
pA = A.begin(); // *pA = 'a'
cout << *pA << endl;

...```

### ⇑

##### 6. Method end(). Return an iterator pointing to the last element of the array

The end() method sets the iterator to the end of the array. This means that the iterator is set to the element following the last element of the array.

Figure 1. Setting iterators with begin() and end() methods. Array size is defined by method size()

The syntax for declaring a method is as follows

`vector<T>::iterator vector<T>::end()`

Example.

```...

// The end() method - returns the iterator at the end of the array.
// 1. Declare a vector of 5 elements of type char.
vector<char> A(5);

// 2. Fill array with data
A[0] = 'a';
A[1] = 'b';
A[2] = 'c';
A[3] = 'd';
A[4] = 'e';

// 3. Declare an iterator on an array of char elements
vector<char>::iterator pA;

// 4. Set the iterator to the end of the array
pA = A.end(); // pA points to the position after the last element of the array

// 5. Move to the last element of the array
pA--; // *pA = 'e'
cout << *pA << endl;

...```

### ⇑

##### 7. Methods cbegin(), cend(). Set the constant iterator to the beginning and end of the array

When working with iterators, in addition to ordinary iterators, constant iterators are distinguished. In the STL library, a standard iterator is declared using the type iterator

`vector<T>::iterator It;`

A constant iterator is declared using type constant_iterator

`vector<T>::constant_iterator ConstIt;`

here

• T – the type of the array elements;
• constant_iterator – a type that defines a constant iterator;
• ConstIt – the name of the constant iterator.

Unlike a normal (standard) iterator, it is not possible to change the value of an array element with a constant iterator. That is, assigning some value to a constant iterator

`*ConstIt = value;`

will cause a compilation error.

The cbegin() and cend() methods are designed to get a constant iterator that points to the beginning and end of the array, respectively. The general form for declaring methods is as follows.

```vector<T>::const_iterator cbegin() const
vector<T>::const_iterator cend() const```

here T is the type of the array elements.

The cend() method returns an iterator pointing to the element following the last element in the array.

Example.

```...

// The constant iterator const_iterator
// 1. Create an array of integers based on the initialization list
initializer_list<int> L = { 1, 2, 3, 4, 5 };
vector<int> A1(L); // A1 = { 1, 2, 3, 4, 5 }

// 2. Declare a constant iterator it pointing to the start of vector A1
vector<int>::const_iterator it = A1.cbegin();

// 3. Using a constant iterator, print the elements of the vector A1
cout << "A1 = ";
while (it != A1.cend())
cout << *it++ << " ";
cout << endl;

// 4. An attempt to change a value by a constant iterator will cause a compilation error
it = A1.cbegin();
//*it = 8; // compilation error

...```

### ⇑

##### 8. Methods rbegin(), rend(). Access to the array elements using a reverse iterator

A reverse iterator differs from a regular iterator in that the order of elements is considered from end to beginning. From this point of view, all possible changes are made in processing methods and operations on iterators. For example, the it++ iterator increment operation moves to the previous element of the iterator, and not to the next one, as in a regular iterator.

A reverse iterator is declared using the reverse_iterator keyword

`vector<T>::reverse_iterator itReverse;`

here

• T – type of vector elements;
• itReverse is the name of the iterator.

The rbegin(), rend() methods work with reverse iterators. They allow you to get iterators pointing to the beginning (rbegin) and end (rend) of the array, respectively. Methods have the following overloaded implementations

```reverse_iterator<vector<T>::iterator> rbegin()
reverse_iterator<vector<T>::const_iterator> rbegin() const
reverse_iterator<vector<T>::iterator> rend()
reverse_iterator<vector<T>::const_iterator> rend() const```

One of the implementations of the rbegin() and rend() methods allows you to work like a normal read/write iterator. The second implementation of these methods acts as a read-only constant iterator.

Example.

```#include <iostream>
#include <vector>
using namespace std;

void main()
{
// Reverse iterator const_iterator
// 1. Create an array of real numbers based on the initialization list
initializer_list<double> L = { 1.5, 2.2, 3.4, 4.1, 5.8 };
vector<double> A1(L); // A1 = { 1.5, 2.2, 3.4, 4.1, 5.8 };

// 2. Declare a reverse iterator itReverse to type double
vector<double>::reverse_iterator itReverse;

// 3. Set the reverse iterator to the beginning of vector A1
itReverse = A1.rbegin();

// 4. Print the first element of the array
cout << "begin: " << *itReverse << endl;

// 5. With the help of a reverse iterator, display the elements of the entire vector A1.
// Important: items are displayed in reverse order
cout << "A1 = ";
while (itReverse != A1.rend())
{
cout << *itReverse << " ";
itReverse++; // the increment of the iterator is shifted from end to beginning
}
cout << endl;

// 6. Change the value of the element with index 3
itReverse = A1.rbegin() + 3; // elements are indexed from the end
*itReverse = 8.5; // Ок!
cout << "A1[3] = " << A1[3] << endl;
}```

Program result

```begin: 5.8
A1 = 5.8 4.1 3.4 2.2 1.5
A1[3] = 4.1```

### ⇑

##### 9. Methods crbegin(), crend(). Set a constant reverse iterator to the beginning and end of the array

In addition to the constant_iterator, the STL library introduces a constant reverse iterator that considers an array from end to beginning. Such an iterator is declared as follows

`vector<T>::const_reverse_iterator itConstReverse;`

here

• T – the type of array items;
• itConstReverse – the name of the constant reverse iterator.

You cannot change the value of array elements using a constant reverse iterator.

The crbegin(), crend() methods are designed to work with the constant_reverse_iterator type and have the following declarations

Example.

```#include <iostream>
#include <vector>
using namespace std;

void main()
{
// Constant reverse iterator const_reverse_iterator
// 1. Create an array of characters of the initialization list
initializer_list<char> L = { 'a', 'b', 'c', 'd' };
vector<char> A1(L); // A1 = { 'a', 'b', 'c', 'd' };

// 2. Declare a constant reverse iterator itConstReverse to type char
vector<char>::const_reverse_iterator itConstReverse;

// 3. Set a constant reverse iterator to the beginning of vector A1
itConstReverse = A1.crbegin();

// 4. Print the first element of the array
cout << "begin: " << *itConstReverse << endl;

// 5. Output the items of the whole vector A1 using a constant reverse iterator.
// Important: items are displayed in reverse order
cout << "A1 = ";
while (itConstReverse != A1.crend())
{
cout << *itConstReverse << " ";
itConstReverse++;
}
cout << endl;

// 6. Changing the value of the element with index 3 will fail
itConstReverse = A1.crbegin() + 3;
//*itConstReverse = 8.5; // compilation error
}```

Program result

```begin: d
A1 = d c b a```