List in C++ Standard Template Library With Examples

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!

We can set an iterator to the List in order to access the elements as shown below:

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:

Output:

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 list
  • list.back(): Returns the last value of the list.

Example:

Output:


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!!


References

By admin

Leave a Reply

%d bloggers like this: