C++ <vector> pop_back function
Among the many container operations,pop_backis a very practical function, used toRemove the element at the end of the vector。
pop_backis a member function of container classes, used to delete the last element of the container. The container'ssize()Will decrease by 1.
pop_backImplemented the container'sDynamic shrinking, allowing you to remove trailing elements that are no longer needed at any time.
Word Definitions: popis to pop,backmeans "back", combined it means popping the element at the back, which is very vivid.
Basic syntax and parameters
pop_backis a member function of container classes, so you need to have a container object first, and then use the dot operator.to call it.
Syntax format
void pop_back();
Parameter description
- Parameter: No parameters
pop_backaccepts no arguments.
Function description
- Return Value:
void(no return value). Its purpose is purely to "remove an element"; once the removal is done, the function ends. - Effect: of the container
size()(current number of elements) decreases by 1. The original last element is removed. - Note: If the container is empty, calling
pop_backis undefined behavior. Before calling, you should first check whether the container is empty (usingempty()function).
Example
Let us thoroughly master it through a series of examples from simple to complex.pop_backUsage.
Example 1: Basic usage - removing the trailing element
Example
#include <vector> // 1. Must include the vector header file
int main() {
// 2. Create a vector and add some elements
std::vector<int> numbers = {10, 20, 30, 40, 50};
std::cout << "Initially, the size of the vector is: " << numbers.size() << std::endl;
std::cout << "The elements are: ";
for(int n : numbers) {
std::cout << n << " ";
}
std::cout << std::endl;
// 3. Use pop_back to remove the last element
numbers.pop_back(); // Delete the last element (50)
std::cout << After deleting one element, the size is: << numbers.size() << std::endl;
numbers.pop_back(); // Delete the last element (40)
std::cout << After deleting another element, the size is: << numbers.size() << std::endl;
// 4. Traverse and output all elements
std::cout << The remaining elements are:;
for(int n : numbers) {
std::cout << n << " ";
}
std::cout << std::endl;
return 0;
}
Expected run result:
初始时,vector的大小是: 5 元素是: 10 20 30 40 50 删除一个元素后,大小是: 4 再删除一个元素后,大小是: 3 剩余的元素是: 10 20 30
Code analysis:
#include <vector>is to usevectorThe premise of the container.std::vector<int> numbers = {10, 20, 30, 40, 50};created a ... containing 5 elementsvector。- twice
pop_backcall, successively removed the trailing50and40。 - after each call, the container's
size()Will all decrease by 1.
Example 2: Safe usage of pop_back - checking whether the container is empty
When callingpop_back, it is best to check whether the container is empty to avoid undefined behavior.
Example
#include <vector>
#include <string>
int main() {
// Create a vector that stores strings
std::vector<std::string> tasks = {"Learn C++", "Do homework", "Read documentation"};
std::cout << Number of todos: << tasks.size() << std::endl;
// Safely delete the last element
while(!tasks.empty()) {
std::cout << Completed: << tasks.back() << std::endl;
tasks.pop_back(); // Delete the last element
std::cout << Remaining todos: << tasks.size() << std::endl;
}
// Try to delete again (the container is empty now)
if(tasks.empty()) {
std::cout << No more todos! << std::endl;
}
return 0;
}
Expected run result:
待办事项数量: 3 完成: 阅读文档 剩余待办事项: 2 完成: 写作业 剩余待办事项: 1 完成: 学习 C++ 剩余待办事项: 0 没有更多待办事项了!
Code analysis:
!tasks.empty()checks whether the container is not empty.tasks.back()returns the last element (without deleting it); it is used here to show what task has been completed.- Usage
whileLoop coordinationempty()check, you can safely clear the entire container.
Example 3: Implementing a simple stack using pop_back
push_backandpop_backUsed together, they can implement a simple stack structure.
Example
#include <vector>
class Stack {
private:
std::vector<int> data;
public:
// Push
void push(int value) {
data.push_back(value);
}
// Pop
void pop() {
if(!data.empty()) {
data.pop_back();
}
}
// Get the top element of the stack
int top() {
if(!data.empty()) {
return data.back();
}
return -1; // Indicates the stack is empty
}
// Check if the stack is empty
bool empty() {
return data.empty();
}
// Get the size of the stack
size_t size() {
return data.size();
}
};
int main() {
Stack s;
// Push operation
s.push(10);
s.push(20);
s.push(30);
std::cout << Stack size: << s.size() << std::endl;
std::cout << Top element of stack: << s.top() << std::endl;
// Pop operation
s.pop();
std::cout << After popping, the top element: << s.top() << std::endl;
s.pop();
std::cout << After popping again, the top element: << s.top() << std::endl;
s.pop();
std::cout << After popping again, is the stack empty: << (s.empty() ? Yes : No) << std::endl;
return 0;
}
This example shows how to usepop_backto implement the stack's pop operation.push_backCorresponds to push,pop_backCorresponds to pop.
other extensions