The list::end() is a built-in function in C++ STL which is used to get an iterator to past the last element. By past the last element it is meant that the iterator returned by the end() function return an iterator to an element which follows the last element in the list container. It can not be used to modify the element or the list container.
This function is basically used to set a range along with the list::begin() function.
Syntax:
list_name.end()
Parameters: This function does not accept any parameter, it simply returns an iterator to past the last element.
Return Value: This function returns an iterator to the element past the last element of the list.
Below program illustrates the list::end() function.
// CPP program to illustrate the // list::end() function #include <bits/stdc++.h> using namespace std; int main() { // Creating a list list<int> demoList; // Add elements to the List demoList.push_back(10); demoList.push_back(20); demoList.push_back(30); demoList.push_back(40); // using end() to get iterator // to past the last element list<int>::iterator it = demoList.end(); // This will not print the last element cout << "Returned iterator points to : " << *it << endl; // Using end() with begin() as a range to // print all of the list elements for (auto itr = demoList.begin(); itr != demoList.end(); itr++) { cout << *itr << " "; } return 0; } |
Returned iterator points to : 4 10 20 30 40
Note: This function works in constant time complexity.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- list::begin() and list::end() in C++ STL
- unordered_multimap begin() and end() function in C++ STL
- multiset begin() and end() function in C++ STL
- unordered_multiset end() function in C++ STL
- match_results begin() and end() function in C++ STL
- unordered_map end( ) function in C++ STL
- array::begin() and array::end() in C++ STL
- deque::begin() and deque::end in C++ STL
- forward_list::begin() and forward_list::end() in C++ STL
- map::begin() and end() in C++ STL
- set::begin() and set::end() in C++ STL
- vector::begin() and vector::end() in C++ STL
- multimap::begin() and multimap::end() in C++ STL
- unordered_set end() in C++ STL
- list::push_front() and list::push_back() in C++ STL
- list::pop_front() and list::pop_back() in C++ STL
- list::front() and list::back() in C++ STL
- list::empty() and list::size() in C++ STL
- list::remove() and list::remove_if() in C++ STL
- list::emplace_front() and list::emplace_back() 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.

