Vectors in C++ (vector )
One kind of dynamic array in C++ is vector (vector) .
Vector (vector ) — it is a data structure that is already a model of a dynamic array.
Ordinary arrays in C++ do not have any special functions and methods for working with them. Vectors in C++ are data structures that contain more additional functions for working with elements.
Creating a vector
#include <vector>
...
int main()
{
// declaration of integer vector v for 10 elements
vector <int> v(10);
// same with zero initial values (vector v1)
vector <int> v1(10, 0);
...
Vector padding
Method 1
We allocate memory for the n -th number of elements and fill them in by reading them from the keyboard.
intn;
cin>> n;
vector a(n);
for (int i = 0; i < n; i++)
cin>> a[i];
Method 2
The second method is needed when the number of elements is unknown. First, an empty vector is created, and then, using the push_back() method, a new element is added to the end of the vector.
intn;
cin>> n;
vectora;
for (int i = 0; i < n; i++) {
intb;
cin>> b;
a.push_back(b);
}
Vector size
int b = a.size();
|
Iterators
One way to iterate over elements in containers (data structures) is the iterator ( iterator code>), which come in several types.
Iterator - data structure that « indicates» on some container element, and (for some containers) can navigate to the previous/next element.
The vector uses the most powerful - random-access iterator(random-access Iterator ). A random access iterator can access an arbitrary element of the vector in addition to sequential movement.
The Benefits of Iterators
1) When deleting elements and iterating over elements using indices ([] ), we need to keep track of the number of remaining elements all the time so as not to go beyond the vector, and using an iterator, you can use end()< /code> indicating the end of the vector.
2) Using an iterator, you can easily dynamically remove and insert elements in a vector.
Iterator declaration
1) Declaring an iterator for an integer vector and pointing it to the first element in the vector.
vector <int> myvector = { 1, 2, 3, 4, 5 };
vector <int>::iterator it = myvector.begin();
2) Declaring an iterator for an integer vector and pointing it to the element after the last in the vector.
vector <int> myvector = { 1, 2, 3, 4, 5 };
vector <int>::iterator it = myvector.end(); // points to the element after the last one,
vector <int>::iterator it1 = myvector.end() - 1 ; // pointing to the last element.
Getting and displaying a value
Getting and displaying the element pointed to by the iterator.
cout << *it;
Move iterator position
Move the iterator position 3 positions forward.
advance(it, 3);
Creating a new iterator from an existing one
Create a new iterator based on an existing one, advancing 3 positions.
auto it1 = next(it, 3);
Displaying vector values using an iterator
vector<int>::iterator it;
for (it = myvector.begin(); it != myvector.end(); ++it) {
cout<<*it<<" ";
}
Vector traversal
To traverse the vector from the last element to the first, a reverse iterator reverse_iterator is used, it is obtained by:
1) rbegin() - returns a reverse iterator pointing to the last element of the vector, the application of the ++ operation leads to the transition to the previous element;
2) rend() - returns a reverse iterator pointing to the previous element of the vector, the application of the ++ operation leads to the transition to the next.
vector<int>::reverse_iterator it = myvector.rbegin(); // points to the last element
vector<int>::reverse_iterator it = myvector.rend(); // points to an element,
// which comes before the first one,
|