Skip to content
Related Articles

Related Articles

Improve Article

list::begin() and list::end() in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 29 Oct, 2020
Geek Week

Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists.

list::begin()

begin() function is used to return an iterator pointing to the first element of the list container. It is different from the front() function because the front function returns a reference to the first element of the container but begin() function returns a bidirectional iterator to the first element of the container.
Syntax : 

listname.begin()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the first element.

Examples:  

Input  : mylist{1, 2, 3, 4, 5};
         mylist.begin();
Output : returns an iterator to the element 1

Input  : mylist{8, 7};
         mylist.begin();
Output : returns an iterator to the element 8

Errors and Exceptions
1. It has a no exception throw guarantee. 
2. Shows error when a parameter is passed. 

CPP




// CPP program to illustrate
// Implementation of begin() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    // declaration of list container
    list<int> mylist{ 1, 2, 3, 4, 5 };
 
    // using begin() to print list
    for (auto it = mylist.begin(); it !=
                            mylist.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Output: 



1 2 3 4 5

Time Complexity: O(1) 

list::end()

end() function is used to return an iterator pointing to the last element of the list container. It is different from the back() function because the back() function returns a reference to the last element of the container but end() function returns a bidirectional iterator to the past last element of the container.
Syntax : 

listname.end()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the last element.

Examples:  

Input  : mylist{1, 2, 3, 4, 5};
         mylist.end();
Output : returns an iterator to the element 5

Input  : mylist{8, 7};
         mylist.end();
Output : returns an iterator to the element 7


Errors and Exceptions
1. It has a no exception throw guarantee. 
2. Shows error when a parameter is passed. 

CPP




// CPP program to illustrate
// Implementation of end() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    // declaration of list container
    list<int> mylist{ 1, 2, 3, 4, 5 };
 
    // using end() to print list
    for (auto it = mylist.begin(); it !=
                                mylist.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Output: 

1 2 3 4 5

Time Complexity: O(1) 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :