YouTip LogoYouTip

Cpp Libs Array Back

[![Image 1: C++ Container Class ](#) C++ Container Class ](#) * * * In various array operations, `back` is a very practical function used to **get the last element**. `back` is a member function of the container class, used to **return the last element of the container**. It is equivalent to `at(size()-1)` or `operator[](size()-1)`, but with clearer semantics. `back` provides an intuitive way to access the last element of the container. **Word meaning**: `back` means "the back/behind", i.e., getting the last (rearmost) element. * * * ## Basic Syntax and Parameters `back` is a member function of the container class, calling it is very straightforward and requires no parameters. ### Syntax Format reference back(); const_reference back() const; ### Parameter Description * **Parameters**: No parameters * `back` does not accept any parameters. ### Function Description * **Return value**: Returns a **reference** to the last element of the container. If the container is a const container, returns a const reference. * **Effect**: Returns the last element of the container (the element at index `size()-1`). * **Note**: array cannot be empty, so calling `back` is safe. * * * ## Examples Let's thoroughly master the usage of `back` through a series of examples. ### Example 1: Basic Usage - Get the Last Element ## Example #include #include int main(){ // 1. Create an array and initialize it std::array numbers ={10, 20, 30, 40, 50}; std::cout<<"Array size is: "<< numbers.size()<< std::endl; // 2. Use back to get the last element std::cout<<"Last element (back): "<< numbers.back()<< std::endl; std::cout<<"Using at(size()-1): "<< numbers.at(numbers.size()-1)<< std::endl; std::cout<<"Using [size()-1]: "<< numbers[numbers.size()-1]<< std::endl; return 0; } **Expected Output:** Array size is: 5Last element (back): 50Using at(size()-1): 50Using [size()-1]: 50 **Code Analysis:** 1. `numbers.back()` returns the last element `50`. 2. It returns the same value as `numbers.at(numbers.size()-1)` and `numbers[numbers.size()-1]`, but with more explicit semantics. ### Example 2: Modify the Value of the Last Element `back` returns a reference, so it can be used to modify the element's value. ## Example #include #include #include int main(){ std::array tasks ={"Learn C++", "Do homework", "Read documentation"}; std::cout<<"Before modification, last task: "<< tasks.back()<< std::endl; // Use back() to get reference and modify element tasks.back()="Complete project"; std::cout<<"After modification, last task: "<< tasks.back()<< std::endl; return 0; } **Expected Output:** Before modification, last task: Read documentationAfter modification, last task: Complete project **Code Analysis:** * `tasks.back() = "Complete project";` modifies the last element's value through reference. * `back()` returns a modifiable lvalue reference. ### Example 3: Using back with sort to Get Max and Min Values After sorting, `back()` returns the maximum value. ## Example #include #include #include int main(){ std::array numbers ={30, 10, 50, 20, 40}; std::cout<<"Before sorting: "; for(int n : numbers) std::cout<< n <<" "; std::cout<< std::endl; // Sort std::sort(numbers.begin(), numbers.end()); std::cout<<"After sorting: "; for(int n : numbers) std::cout<< n <<" "; std::cout<< std::endl; std::cout<<"Minimum value (front): "<< numbers.front()<< std::endl; std::cout<<"Maximum value (back): "<< numbers.back()<< std::endl; return 0; } **Expected Output:** Before sorting: 30 10 50 20 40After sorting: 10 20 30 40 50Minimum value (front): 10Maximum value (back): 50 **Code Analysis:** * After sorting, `front()` returns the minimum value, `back()` returns the maximum value. * This is a simple way to get max/min values. * * C++ Container Class ](#)
← Cpp Libs Array FillCpp Libs Vector Begin End β†’