Hey, folks! In this article, we will be focusing List in C++ in detail.
Standard Template Library is a generic library
that comprises of different c++ template classes
. It includes various classes and methods that can operate on different types of data records/values.
By this, we mean that, we need not define classes and methods over and over to perform the same operation on different data type values.
The basic components of Standard Template Library is:
- Containers
- Iterators
- Algorithms
Today, we will be focusing on lists that come under Containers. So, let us begin!
What is a List in C++?
A list
is part of a container is a component of the Standard Template Library that enables the programmer to create and store data values of different data types.
There are different types of Containers as follows:
- Sequence Containers
- Associative Containers
- Container Adapters
- Unordered Associative Containers
List is a Sequential Container
that enables us to store and work with different types of data values. Sequential Containers allow the programmer to access the data values of varied data types in a sequential manner only.
Thus, lists work upon the storage of data in non-contiguous memory locations and access the values in a sequential manner.
Lists
allows us to insert and delete the data values from any particular memory location. Moreover, we can access the elements of the lists in a bi-directional manner but in a sequential way.
With Lists, we can store data elements at varied locations in different chunks of memory. The lists can shrink and expand through both the ends at dynamic runtime as per the need.
Syntax of a List
Have a look at the below syntax!
1 |
list <data_type> list_name; |
We can set an iterator to the List in order to access the elements as shown below:
1 |
list <data_type> :: iterator iterator-name; |
Lists enable us to store values of different data types at runtime on different memory blocks and can be accessed sequentially in a bi-directional manner.
Examples with Important Functions of Lists in C++
Let us first iterate through a list and print all the data values present in it as shown below.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <list> #include <iterator> using namespace std; void view_list(list <int> li) { list <int> :: iterator x; for(x = li.begin(); x != li.end(); ++x) cout << 't' << *x; cout << 'n'; } int main() { list <int> X = {1,3,5,7,9}; view_list(X); return 0; } |
Output:
1 |
1 3 5 7 9 |
Some of the most commonly used functions in Lists–
list.begin()
: Returns an iterator element pointing to the first element of the list.list.end()
: Returns an iterator element pointing to the last element of the list.list.sort()
: In-built function to sort the list.list.reverse()
: In-built function to reverse the list.list.push_front(element)
: Adds an element to the beginning of the list.list.push_back(element)
: Adds an element to the end of the list.list.pop_front()
: Removes the first element and decrements the size of the list by 1.list.pop_back()
: Removes the last element and decrements the size of the list by 1.list.front()
: Returns the first value of the listlist.back()
: Returns the last value of the list.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> #include <list> #include <iterator> using namespace std; void view_list(list <int> li) { list <int> :: iterator x; for(x = li.begin(); x != li.end(); ++x) cout << 't' << *x; cout << 'n'; } int main() { list <int> X = {1,3,5,7,9}; cout<<"Original list:n"; view_list(X); X.push_front(100); cout<<"List after adding element using push_front() function:n"; view_list(X); X.push_back(200); cout<<"List after adding element using push_back() function:n"; view_list(X); cout<<"Reversing a List:n"; X.reverse(); view_list(X); cout<<"Sorting a List:n"; X.sort(); view_list(X); X.pop_front(); cout<<"List after removing element using pop_front() function:n"; view_list(X); X.pop_back(); cout<<"List after removing element using pop_back() function:n"; view_list(X); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Original list: 1 3 5 7 9 List after adding element using push_front() function: 100 1 3 5 7 9 List after adding element using push_back() function: 100 1 3 5 7 9 200 Reversing a List: 200 9 7 5 3 1 100 Sorting a List: 1 3 5 7 9 100 200 List after removing element using pop_front() function: 3 5 7 9 100 200 List after removing element using pop_back() function: 3 5 7 9 100 |
Conclusion
By this, we have come to the end of this topic. I hope this article turns out to be useful in understanding Lists in C++.
Feel free to comment below, in case you come across any question.
Till then, Stay tuned @ C++ programming with JournalDev and Keep Learning!!