Векторы в C++ — урок 12
Вектор в C++ — это замена стандартному динамическому массиву, память для которого выделяется вручную, с помощью оператора new .
Разработчики языка рекомендуют в использовать именно vector вместо ручного выделения памяти для массива. Это позволяет избежать утечек памяти и облегчает работу программисту.
Пример создания вектора
Управление элементами вектора
Создадим вектор, в котором будет содержаться произвольное количество фамилий студентов.
Результат работы программы:
Методы класса vector
Для добавления нового элемента в конец вектора используется метод push_back() . Количество элементов определяется методом size() . Для доступа к элементам вектора можно использовать квадратные скобки [] , также, как и для обычных массивов.
- pop_back() — удалить последний элемент
- clear() — удалить все элементы вектора
- empty() — проверить вектор на пустоту
Подробное описание всех методов std::vector (на английском) есть на C++ Reference.
список pop_back () в C ++ STL
List :: pop_back () — это встроенная функция в C ++ STL, которая используется для удаления элемента из задней части контейнера списка. То есть эта функция удаляет последний элемент контейнера списка. Таким образом, эта функция уменьшает размер контейнера на 1, так как удаляет элемент из конца списка.
Синтаксис :
Параметры : функция не принимает никаких параметров.
Возвращаемое значение : эта функция ничего не возвращает.
Ниже программа иллюстрирует функцию list :: pop_back () в C ++ STL:
// Программа CPP для иллюстрации
// list :: pop_back () функция
#include <bits/stdc++.h>
vector::push_back() and vector::pop_back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1.

Syntax :
Errors and Exceptions
1. Strong exception guarantee – if an exception is thrown, there are no changes in the container.
2. If the value passed as argument is not supported by the vector, it shows undefined behavior.
pop_back() function is used to pop or remove elements from a vector from the back. The value is removed from the vector from the end, and the container size is decreased by 1.
Syntax :
Errors and Exceptions
1. No-Throw-Guarantee – If the container is not empty, the function never throws exceptions.
2. If the vector is empty, it shows undefined behavior.
Does pop_back() removes values along with elements ?
When pop_back() function is called, element at the last is removed, values and elements are one of the same thing in this case. The destructor of the stored object is called, and length of the vector is removed by 1. If the container’s capacity is not reduced, then you can still access the previous memory location but in this case, there is no use of accessing an already popped element, as it will result in an undefined behavior.
Application push_back() and pop_back()
Given an empty vector, add integers to it using push_back function and then calculate its size.
Algorithm
1. Add elements to the vector using push_back function
2. Check if the size of the vector is 0, if not, increment the counter variable initialized as 0, and pop the back element.
3. Repeat this step until the size of the vector becomes 0.
4. Print the final value of the variable.