YouTip LogoYouTip

Cpp Libs List Push_Back

[![Image 1: C++ Container Class ](#) C++ Container Class ](#) * * * Among many containers, `list` (doubly linked list) is a data structure with efficient insertion and deletion, and `push_back` is the key function for adding elements at the end of the list. `push_back` is a member function used to add a new element at the **end** of a **list**. `push_back` implements **dynamic growth** of the list. You don't need to specify the final size of the container in advance; you can add elements anytime, anywhere as needed during program execution. **Word meaning**: `push` means to push in, `back` means the rear, together it means pushing to the back, very vivid. * * * ## Basic Syntax and Parameters `push_back` is a member function of the container class, so you need to have a container object first, then call it through the dot operator `.`. ### Syntax Format void push_back(const T& value);void push_back(T&& value); // C++11 and later (supports rvalue) ### Parameter Description * **Parameter**: `value` * Type: Consistent with the element type specified when defining the container, or a value that can be converted to that type. * Description: The value you want to add to the end of the container. 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 element"; the function ends after adding is complete. * **Effect**: The container's `size()` (current number of elements) will increase by 1. The new element becomes the last element of the list. * * * ## Examples Let's thoroughly master the usage of `push_back` through a series of examples from simple to complex. ### Example 1: Basic Usage - Managing Integers ## Example #include #include // 1. Must include list header file int main(){ // 2. Create an empty list for storing integers std::list numbers; std::cout<<"Initially, list size 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, list size is: "<< numbers.size()<< std::endl; // 4. Traverse and output all elements std::cout<<"Elements in list are: "; for(int n : numbers){ std::cout<< n <<" "; } std::cout<< std::endl; return 0; } **Expected Output:** Initially, list size is: 0After adding, list size is: 3Elements in list are: 10 20 30 **Code Analysis:** 1. `#include ` is the prerequisite for using the `list` container. 2. `std::list numbers;` creates an empty `list` named `numbers`, which will store integers of type `int`. 3. Three `push_back` calls sequentially add `10`, `20`, `30` to the end of `numbers`. The adding order is the final order of elements in the `list`. 4. `numbers.size()` returns the current number of elements. ### Example 2: Managing Strings `list` can not only store numbers, but also store strings, custom types, and any other data types. ## Example #include #include #include int main(){ // Create a list storing std::string std::list 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:** Today's to-do items (3 items):- Learn C++ list- Do homework- Read technical documentation **Code Analysis:** * `std::list` declares a container with element type `std::string`. * The parameter of `push_back` is a string literal. ### Example 3: Combined with push_front `push_back` and `push_front` can be used together to build a list. ## Example #include #include int main(){ std::list data; // First add at the front data.push_front(30); data.push_front(20); data.push_front(10); // Then add at the back data.push_back(40); data.push_back(50); std::cout<<"Elements: "; for(int n : data){ std::cout<< n <<" "; } std::cout< 20 -> 30 -> 40 -> 50 * * C++ Container Class ](#)
← Cpp Libs List InsertCpp Libs Array Swap β†’