C++ <list> push_back function
Among the many containers,list(Doubly linked list) is a data structure with efficient insertion and deletion, andpush_backis the key function for adding elements at the end of the linked list.
push_backis a member function used tolinked list (list)ofendAdd a new element.
push_backimplements a linked listdynamic growth, you don't need to pre-specify the final size of the container, and can add elements anytime and anywhere as needed during program runtime.
Word Definitions: pushis pushing,backmeans "the back", so together it means "push to the back", which is very vivid.
Basic syntax and parameters
push_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 push_back(const T& value); void push_back(T&& value); // C++11 之后(支持右值)
Parameter description
- Parameter:
value- Type: consistent with the element type specified when the container was defined, or a value that can be converted to that type.
- Description: the value you want to add to the end of the container. It can be a variable, a literal (such as
10,"hello"), or the result of an expression.
Function description
- Return Value:
void(no return value). Its purpose is purely "adding an element"; once the addition is complete, the function ends. - Effect: of the container
size()(current number of elements) will increase by 1. The new element becomes the last element of the linked list.
Example
Let's thoroughly master it through a series of examples, from simple to complex:push_backUsage.
Example 1: Basic usage - managing integers
Example
#include <list> // 1. Must include the list header file
int main() {
// 2. Create an empty list to store integers
std::list<int> numbers;
std::cout << "Initially, the size of the list is: " << numbers.size() << std::endl;
// 3. Use push_back to add elements
numbers.push_back(10); // Add integer 10 at the end
numbers.push_back(20); // Add 20 after 10
numbers.push_back(30); // Add 30 after 20
std::cout << "After adding, the size of the list is: " << numbers.size() << std::endl;
// 4. Traverse and output all elements
std::cout << "The elements in the list are: ";
for(int n : numbers) {
std::cout << n << " ";
}
std::cout << std::endl;
return 0;
}
Expected output:
初始时,list的大小是: 0 添加后,list的大小是: 3 list中的元素是: 10 20 30
Code analysis:
#include <list>is to uselistThe premise of a container.std::list<int> numbers;Created one namednumbersof the emptylist, which will later storeintintegers of type.- three times
push_backcall, sequentially10,20,30add tonumbersend. The order of addition is the elements inlistthe final order in numbers.size()Returns the current number of elements.
Example 2: Managing strings
listIt can store not only numbers, but also strings, custom types, and any other data type.
Example
#include <list>
#include <string>
int main() {
// Create a list that stores std::string
std::list<std::string> tasks;
// Use push_back to add to-do items
tasks.push_back("Learn C++ list");
tasks.push_back("Do homework");
tasks.push_back("Read technical documentation");
std::cout << "Today's to-do items (" << tasks.size() << " items):" << std::endl;
for(const auto& task : tasks) {
std::cout << "- " << task << std::endl;
}
return 0;
}
Expected output:
今日待办事项 (3 项): - 学习 C++ list - 写作业 - 阅读技术文档
Code analysis:
std::list<std::string>declares the element type asstd::stringcontainer.push_backThe parameter is a string literal.
Example 3: Using with push_front
push_backandpush_frontcan be used together to build a linked list.
Example
#include <list>
int main() {
std::list<int> data;
// First add at the head
data.push_front(30);
data.push_front(20);
data.push_front(10);
// Then add at the tail
data.push_back(40);
data.push_back(50);
std::cout << "Elements: ";
for(int n : data) {
std::cout << n << " ";
}
std::cout << std::endl;
return 0;
}
Expected output:
元素: 10 20 30 40 50
Code analysis:
- First use
push_frontAdd 10, 20, 30 at the head - Then use
push_backAdd 40, 50 at the tail - The final order is 10 -> 20 -> 30 -> 40 -> 50
other extensions