Updated April 13, 2023
Definition of C++ arraylist
Arraylist is a collection that is used to store different types of data. It is a flexible list that can be resized dynamically unlike the arrays in C++. Members/ data of arraylist can be accessed using integer indexes. Two different types of data can be stored in the arraylist. Multidimensional arrays cannot be stored in it. Arraylist in C++ can easily be accessed using the indexing through integers. But now the arraylist is being replaced with List in C++ over the years.
The list is a sequential container which holds the data of the same type. List in C++ is implemented as a doubly-linked list so allows the bidirectional access of its data.
Syntax:
Below given is the basic syntax of using the list on C++ programs:
- One needs to import the <list> header file to use the list in the program.
template <class T, class Alloc = allocator<T>> class list;
where,
- T: It is the type of element to be stored in the list. It can be a user-defined type of element.
- Alloc: It is an allocator object and defines the memory allocation.
How does List Work in C++?
Few important points defining the working and features of the list in C++ are given below:
- List in C++ is implemented as a doubly-linked list which allows the operations like insertion, deletion, and access from both directions.
- The list allows the non- contiguous storage of elements which means Elements of the list can be stored at different locations in the memory.
- Elements in the list are connected with each other using the link of the doubly linked list of its proceeding and the previous elements.
- List in C++ has a slow traversal as a comparison to the Vectors. But once the element is found, operations like insertion and deletion become very easy.
- As compared to other sequence containers, the performance of List is not as good as the direct access of the elements is not possible. One needs to traverse the whole list till the required element is found in order to perform any operation.
- For the small number of elements, List is not considered to be a good option as it occupies more space in the memory as compared to other containers keeping the track of its preceding and previous element through links.
- List in C++ provides us the facility to increase or decrease the size of it at runtime. A zero size list is also possible practically.
Some of the commonly used list functions are given below:
Method Name | Description |
list::begin() | This function returns the iterator which is pointing to the starting/ first element of the list |
list::end() | This function returns the iterator pointing to the end/ last element of the list |
size | This function returns the total size of the list |
front | This function helps in accessing the first element of the list |
back | This function access the last element of the list |
empty | This function checks whether the list container is empty or not. |
push_front | This function pushes the element at the front/starting of the element |
push_back | This function pushes the element at the end of the list |
pop_front | This function removes/ pops out the element front of the list or the first element of the list. It also reduces the list of the list by 1. |
pop_back | This function pops out the last element of the list. It also reduces the list of the list by 1. |
reverse | This function reverses the elements of the list container. (Reverse is done on the basis of order of elements) |
sort | This function sorts the elements of the list. Sorting is done in increasing order by default. |
erase | It removes the element from the list. It can also erase all the elements of the list. |
merge | It merges the 2 lists into a single list. |
swap | It swaps the elements of one list with the elements of another list. For swapping list type and size needs to be the same. |
Examples of C++ arraylist
Some of the examples of list used in the C++ programs are given below:
Example #1
Code:
#include <algorithm>
#include <iostream>
#include <list>
using namespace std;
intmain() {
//creating the list 'mlist' having integer values in it
list<int>mlist = { 10, 20, 30, 40, 50 };
//pushing the elements at the starting and last
mlist.push_back(60);
mlist.push_front(0);
//printing of the list elements after pushing
list <int> :: iterator it;
cout<< "Elements after insertion are as follows : " <<endl;
for(it = mlist.begin(); it != mlist.end(); it++)
cout<< *it <<'\n';
//popping the elements from the last
mlist.pop_back();
//printing the list elements after pop
cout<< "Elements after deletion are as follows : " <<endl;
for(it = mlist.begin(); it != mlist.end(); it++)
cout<< *it <<'\n';
cout<< "Elements after insertion at particular position are as follows: " <<endl;
//searching the element '10' in the list and then inserting the element before it
auto i = find(mlist.begin(), mlist.end(), 10);
if (i != mlist.end()) {
mlist.insert(i, 90);
}
//printing the elements after the new insertion
for(it = mlist.begin(); it != mlist.end(); it++)
cout<< *it <<'\n';
}
Output:
Example #2
Code:
#include <iostream>
#include <list>
using namespace std;
intmain()
{
//creating 2 lists of integer type
list<int> mlist1 = {100, 30, 12, 45, 76, 43, 4};
list<int> mlist2 = {56, 54, 43, 23, 45};
//Sorting the list ‘mlist1’ in increasing order
mlist1.sort();
cout<< "List 1 after sorting is: " <<endl;
for (inti : mlist1) {
cout<<i<< '\n';
}
//Sorting the list ‘mlist’ in increasing order
mlist2.sort();
cout<< "List 2 after sorting is: " <<endl;
for (inti : mlist2) {
cout<<i<< '\n';
}
// merging the mlist1 in mlist2
mlist2.merge(mlist1);
cout<< "List 2 after merging is: " <<endl;
for (inti : mlist2) {
cout<<i<< '\n';
}
cout<< "Size of list 2 after merging is: ";
cout<< mlist2.size() << '\n';
}
Output:
Conclusion
The above description clearly explains what an arraylist is and how it is used in the program to store various types of data. As the arraylist was not type-safe and has many disadvantages so generic list is considered to be efficient as compared to the arraylist. As the generic list is more reliable and type-safe it is now used in programming. The list also has some of its own advantages and disadvantages both so one needs to understand it properly before using it in a program.
Recommended Articles
This is a guide to C++ arraylist. Here we also discuss the definition and how does the list work in c++ along with different examples and its code implementation. You may also have a look at the following articles to learn more –