List in C++
A list is a sequence that supports traversal in both forward and backwards, and it is known to be a double-linked sequenced list. We can insert delete items at any position, at the start, end, and in the middle as well.
Time consumption, known through the complexity, is the same for insertion and deletion at any point in the list. The list is used to store the elements in it and their address with it. These addresses are pointed through the pointers. These pointers help easily access the value in the next or previous positions for traversal in the backward and forward direction. The memory in the list is located dynamically every time we add a new item to the list.
Why should we use a list?
The list data structure shows a better performance while inserting data, deleting, or moving elements from one point to another. It is also good in using algorithms that perform operations effectively.
T: represents the data type of items. We can use any data type. Alloc: It describes the object of the allocator. An allocator class is used. It depends on the value and uses a simple way of allocating memory.
Working of C++ list
List works in such a way that first, we need to add a list library to operate with all the functions it provides effectively in our program. Inside the program, a list is declared as we have described in the syntax. The method is quite simple. The data type of elements is defined with the name of the list. Using an assignment operator, we insert the integer values in the list. All the elements are displayed by using a FOR loop as we need to display each element present in every index.
Vector in C++
Vector is a dynamic array that can automatically resize itself when any element is added or deleted from it. The elements that are inserted in vectors are placed in contiguous storage so that they are easily accessed with the help of iterators. Elements are inserted from the back of the vector.
The insertion of data at the end takes a differential time. Whereas the items are removed from the vectors by using a constant time.
Why should we use vectors?
We prefer to use a vector container in the C++ program when we don’t have to mention the size of data before starting the program. By using vectors, we don’t require to set any maximum size of the container.
The syntax is started by using a vector keyword. A data type is a type of items/elements to be inserted in the vectors. ‘name’ shows the name of a vector or the data element. The ‘elements’ represent the number of items that are inserted. This is an optional parameter.
Working of C++ vectors
In the main program, as described by the syntax, we declare the vector container by providing the data type of the items and the name of a vector. After entering the vector’s values, we will display all the elements by using a FOR loop. An iterator will help to iterate throughout the loop. In this way, a vector works in the C++ programming language.
Differences between Lists and vectors in C++
Deletion and insertion
Both insertion and the deletion of items in the list are very efficient as compared to the vectors. This is because while insertion of data at the start, end, or in the middle of the list requires one or two pointers to be swapped.
On the other hand, in vectors, the insertion and deletion procedure will make all the elements to be shifted by one. Moreover, if the memory is not sufficient, more memory is allocated, and whole data is transferred there.
So both insertion and deletion in lists are more effective and efficient than vectors.
In lists, it is hard for random access to occur as it is said to be that inside the lists, a doubly-linked list is present. So if you want to access the 6th item, you first need to iterate the first 5 elements in the list.
And in the case of vectors, all elements are stored at contiguous memory locations to perform random access in vectors.
Use of pointers
We need to use pointers in the list to store the address. So, according to the expert programmers, it is very logical while dealing with the pointers in lists. Working with lists is considered difficult compared to vectors because vectors use normal operations like arrays.
Here is a tabular representation of some major differences between lists and vectors.
|Vector in C++||List in C++|
|The memory used is contiguous.||It uses a non-contiguous memory.|
|It has a default size.||There is no default size in the case of lists.|
|In vectors, space is allocated to the data present in it only.||Extra space is required in lists for the data and for the nodes to store addresses in it.|
|Inserting elements at the end uses constant time by at any point in the vector; it is 0.||The deletion process in the list is very cheap from any point.|
|Random access is possible easily.||It is impossible to apply for random access on the list.|
Implementation of list
In this example, we have used operations like displaying the data in the list, reverse, and sorting functions. Moreover, begin() and end() functions are also used.
The display function is declared separately. This uses the begin() and the end() to traverse throughout the list and display all items using the object. We created two lists here. Both lists are entered items through the front and the back as well. Data will be entered from both directions.
After that, we will call the display function to see all its contents. And also use the built-in functions like reverse and sort.
Implementation of vector
This example includes vector creation. A single vector is created, but we enter 5 values by using a “For” loop.
After entering data, we will display the size of the vector created by all the elements inside it. After that, we insert a new value by using a “For” loop again. But this time, we have used begin and end() functions. We can see that the value is entered at the end. And the size of the vector is also updated according to the data itself.
‘List vs vector C++ compared’ describes the differences between the list and the vector. Initially, we have described the list and vectors in detail and worked on them. So that it may assist the user in discriminating between these containers. Two examples are implemented in the Ubuntu Linux operating system that verifies the difference in declaring, inserting, and deleting the items from the containers.