| by Arround The Web | No comments

Std List C++

In C++, std::list is like a container that stores the elements on the non-contagious memory location. The “std::list” is implemented as a doubly linked list. We can insert or remove any element of the list that is stored in any location, which makes the lists bidirectional and sequential. Bidirectional because we can access the list elements from the front and back of the list (from any location). Sequential because we can access the elements anywhere in the sequence. Lists behave as doubly linked lists. Doubly linked lists can store their elements anywhere in the memory and they can be accessed from any location on the runtime. The list belongs to a standard template library like arrays and vectors but lists do not allow a fast access comparable to other data structures. Let’s discuss “std::list” in this article with the practical implementation of lists with different operations.

Integrate Library

For using lists in code, the programmer needs to import the required library. The library for using the list is “list”. We can import this by adding the following-given statement: 

#include<list>

Without importing this library, we won’t be able to use the list and its methods in the code.

Why Do We Use a List?

You must wonder why we use a list when we have vectors and arrays. You will see why we choose a list rather than another type of data structure after reading the arguments which are listed in the following:

  • The list offers us better insert, update, and delete functions as compared to other data structures.
  • It provides good performance with algorithms which perform operations like insertion, deletion, or update.
  • Lists provide us with many methods for insertion and deletion like push() and pop(), etc.

Syntax

Template < class_type , class Allocation = allocator<T>  > class list;

Or

std::list<int> List_1;

The “std” attribute represents the standard template library. The scope resolution operator tells the compiler that we are going to use a class of “std”. Then, we define the type of the list inside the angle brackets. Outside the angle brackets, we state the name of the list.

Parameters

  • Type: The type of the data that we store in the list like in the second statement, we store the integer type data in the list. It is compulsory to define the type of data that is stored in the list.
  • Allocator: The object of a type that hides the details of the list in it. This is optional.

Methods

There are several functions through which we can make the CURD operations of the list possible.

  • insert(): It inserts the elements in the list.
  • push_back(): It inserts the elements in the list from the back.
  • push_front(): It inserts the elements in the list from the front.
  • pop_back(): It removes an element from the back of the list.
  • size(): It finds the size of the list.
  • back(): It shows the last list element.

Use Std::List

Let’s talk about the implementation of lists in the program using the standard list template for lists.

Code:

#include <iostream>
#include <list>
int main() {
    std::list List_1 = { 8.72, 6.7, 9.4, 5.3 };
        std::cout<<"The values are: \n";
    for (float value : List_1) {
        std::cout << value << '\n';
    }
}

The first step shows the integration of two libraries. The first library <iostream> contains all the input and output methods that we use to get or print the value. Then, the second library is <list>. We import this to access the lists methods. The main() function is called. Here, we define the float type list and initialize the list in the same way as we initialize the arrays. The “std::list” tells the compiler that we are defining the list. In angle brackets, “float” tells the type of the list. Next, represent a message on the screen using the “std::cout” command. After that, run the “for” loop to print the list on the console. Inside the parenthesis of “for” defines a float type variable that accesses the values from the list. We cannot print the whole list directly that’s why we use a loop. Inside the body of “for”, employ the “std::cout” to show the values from the list. By doing this, we initialize and print the lists.

Output

The values are:
8.72
6.7
9.4
5.3

Get the List Size and Insert Elements

Let’s observe how we can obtain the size of a list and insert the items in a list after initializing the list.

Code:

#include <iostream>
#include <list>
int main ()
{
  std::list List_0{1,3,4,6,7};
  std::list::iterator iter_0;
  iter_0 = List_0.begin();
  List_0.insert (iter_0,5);                      
  List_0.insert (iter_0,2,80);                    
  --iter_0;    
  std::cout << "List_0 contains:";
  for (iter_0=List_0.begin(); iter_0!=List_0.end(); ++iter_0)
    std::cout << "\t" << *iter_0;
    std::cout<<"\nThe size of the list is :"<<List_0.size();
  return 0;
}

The libraries to access the input/output methods and the list methods are imported first. Then, invoke the main() method to define and initialize a list of an integer type. Here, we define another list iterator of integer type “iter_0” that iterates in the list. Now, use the begin() method as it returns the iterator of the first item of the list. Then, we want to insert a value in the first list which is “List_0”. So, we call the insert() function. It contains two arguments – first is the list_name which temporarily stores the new element. And the second argument is the “value”. In the begin() method, we have not given the location or index where we want to insert this new value, so it is automatically stored in the first index and pushes the value that is already stored in that index. Insert another value and pass three parameters to the insert() method. First, it iterates. Second, it tells the number of values that we want to insert. And third is the value that is inserted. Here, “80” is inserted in the second and third index in the list. After insertion, we perform a decrement in the list. Print the “List_0 contains:” text on the console and use the “for” loop to show the list items. In the “for” loop, initialize the iterator to begin from the list and keep looping in the list until we reach the end of the list and increment the iterator. In the body of “for”, display the list of items that we get by another list iterator. The “*iter_0” accesses the location of list items and “cout” prints the value on the console. Outside the “for” loop, using the size()function, we acquire the size of the list.

Output:

List_0 contains:    5       80    80       1       3       4       6       7
The size of the list is : 8

Conclusion

In this article, we explained in detail about the “std::list”. After a brief introduction, we talked about why we need lists when there are other containers, the syntax, methods, and practical examples of lists. Lists are easy to use because of their built-in methods. We can insert the elements anywhere in the list and the same goes for extraction. It works as a doubly linked list but provides more facilities than a double-linked list.

Share Button

Source: linuxhint.com

Leave a Reply