list back() function in C++ STL
The list::back() function in C++ STL returns a direct reference to the last element in the list container. This function is different from the list::end() function as the end() function returns only the iterator to the last element.
Syntax:
list_name.back()
Parameters: This function does not accepts any parameter.
Return Value: This function returns a direct reference to the last element in the list container demo_list.
Exception: There is as no such exception in this function but calling this function on an empty list container creates an undefined behavior in C++.
Below program illustrates the list::back() function.
// CPP program to illustrate the // list::assign() function #include <bits/stdc++.h> using namespace std; int main() { // Initialization of list list<int> demo_list; // Adding elements to the list demo_list.push_back(10); demo_list.push_back(20); demo_list.push_back(30); // prints the last element of demo_list cout << demo_list.back(); return 0; } |
30
Recommended Posts:
- list::front() and list::back() in C++ STL
- std::string::back() in C++ with Examples
- StringStream in C++ for Decimal to Hexadecimal and back
- list end() function in C++ STL
- list front() function in C++ STL
- list pop_back() function in C++ STL
- list remove() function in C++ STL
- list push_back() function in C++ STL
- list assign() function in C++ STL
- list push_front() function in C++ STL
- list emplace() function in C++ STL
- list splice() function in C++ STL
- list pop_front() function in C++ STL
- list reverse function in C++ STL
- list resize() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

