
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
list begin( ) and list end( ) in C++ STL
Given is the task to show the functionality list begin( ) and list end( ) function in C++ in STL.
What is List in STL
List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.
What is begin( )
The list begin( ) is used to return an iterator pointing to the first element of the list.
Syntax
list_name.begin( )
What is end( )?
The list end( ) is used to return an iterator pointing to the last element of the list.
Syntax
list_name.end( )
Example
Output − List − 10 11 12 13 14
Output − List − 66 67 68 69 70
Approach can be followed
First we intialize list
Then we define begin( ) and end( ).
By using the above approach we can print the list using begin( ) and end( ) function.
Example
/ / C++ code to demonstrate the working of begin( ) and end( ) function in STL #include <iostream.h> #include<list.h> Using namespace std; int main ( ){ List<int> list = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; / / print the list cout<< “ Elements in List: “; for( auto x = list.begin( ); x != list.end( ); ++x) cout<> *x << “ “; return 0; }
Output
IF WE RUN THE ABOVE CODE THEN IT WILL GENERATE THE FOLLOWING OUTPUT
Elements of List: 11 12 13 14 15 16 17 18 19 20
Example
/ / C++ code to demonstrate the working of list begin( ) and end( ) function in STL #include<iostream.h> #include<list.h> Using namespace std; int main ( ){ List list = { ‘D’, ‘E’, ‘S’, ‘I’, ‘G’, ‘N’ }; / / print the list cout << “ Elements in List: “; for( auto x = list.begin( ); x != list.end( ); ++x) cout<< *x << “ “; return 0; }
Output
IF WE RUN THE ABOVE CODE THEN IT WILL GENERATE THE FOLLOWING OUTPUT
Elements in List: D E S I G N
- Related Articles
- map::begin() and end() in C++ STL
- set::begin() and set::end() in C++ STL
- vector::begin() and vector::end() in C++ STL
- forward_list::begin() and forward_list::end() in C++ STL
- deque::begin() and deque::end in C++ STL
- match_results begin() and end() function in C++ STL
- multiset begin() and end() function in C++ STL
- multimap::begin() and multimap::end() in C++ STL
- list end() function in C++ STL
- list::emplace_front() and list::emplace_back() in C++ STL
- list::front() and list::back() in C++ STL
- list::pop_front() and list::pop_back() in C++ STL
- list::push_front() and list::push_back() in C++ STL
- BEGIN and END Blocks in Perl
- list_remove( ) and list remove_if( )in C++ STL
