Home » C++ Tutorial » C++ List with Examples

C++ List with Examples

As easy as it may be to learn C++, there are a few factors and components that do require a little extra attention. For instance, using the list in C++. The reason why this may be more confusing than most other components of C++ is that lists in themselves have many more aspects surrounding them that are required for the C++ program to function. We will be going through these concepts in brief to give you a general understanding of how C++ list functions and how you can use it in your C++ program.

What is C++ List?

Before we begin to learn about how we can use C++ list and what it can, we must understand that list in C++ functions as a part of STL (Standard Template Library). What is STL, you ask? The STL typically consists of four main components, which are as follows:

  • Container
  • Algorithm
  • Iterator
  • Functional

For instance, if you wish to store any piece of data, you use containers to perform this function. When it comes to an iterator, it is the component that helps you move and navigate through the data stream that you have stored. Algorithms are already implemented within STL, some of them are sorting and searching, among other such libraries. Functional refers to a collection of classes that are used like operators, often used with the algorithms.

Now that we know what STL is, let us learn what a list in STL of C++ can do.

C++ List:

Within STL in C++, lists are sequence container type objects that allow non-contiguous memory allocation. In other words, lists allow for efficient storage and removal of data elements anywhere in the containers, unlike vectors or deques that only let you insert data at the beginning, or the end. You can see how lists are laid out from the following diagram: 

Unlike vectors or deques, with lists, you have a pointer to the first element, as well as a pointer to the last element of your data sequence. All of the data elements within your sequence are contained in ‘nodes’. Each node contains the element itself and two pointers, one pointer that directs to the node before it and one that points to the node after it. In the example used in the above diagram, the elements that we have considered are integers (int) – 1, 2, and 3. These integers are contained within nodes.

Inserting and removing nodes is a simple process that can be done by adding or deleting the desired node and manipulating the pointers of the node before and after it. This is why C++ lists are much efficient data containers than deques or vectors in STL. 

Lists in C++ are double-linked in nature. Up until recently, the list in STL of C++ had one drawback. This being that you could not access any element directly without having to start from the beginning of the node sequence or reverse from the end. However, with the help of C++ list iterators, this problem is solved as well. We can use a random-access iterator to access any random node without having to follow the node chain.

C++ List Operators:

  • Assign = this lets you assign the contents of one list to another list.
  • Front = this lets you access the very beginning of the node sequence.
  • Back = this lets you access the very end of the node sequence.
  • Empty = this lets you check whether the particular list is empty or not.
  • Size = this lets you know about the size of the list.
  • Max_size = this helps you know what the maximum size of the list can be.
  • Clear = this lets you delete all the contents of the list.
  • Insert = this lets you insert nodes to your sequence.
  • Emplace = this lets you optimize your list.
  • Push_back = this lets you insert an element to the end of the sequence.
  • Pop_back = this lets you delete an element from the end of the sequence.
  • Push_front = this lets you add an element to the front of the sequence.
  • Pop_front = this lets you delete an element from the front of the sequence.
  • Reverse = this lets you reverse the list.
  • Sort = this lets you sort your data according to a pre-determined order. 
  • Merge = this lets you merge two linked-lists in C++.
  • Splice = this lets you move the contents of one list to any other list.
  • Unique = this lets you eliminate duplicate data elements from your sequence.
  • Remove = this lets you remove a node from the sequence.
  • Remove_if = this lets you remove a node if it satisfies a specific condition.
  • Resize = this lets you determine the maximum size of the list, thereby, resizing it.
#include <iostream>
#include <list>
using namespace std;
 
int main() {
  list<int> even = {0, 2, 4, 6, 8};
  list<int> odd = {1, 3, 5, 7, 9};
 
   cout << "Here are some even numbers:" << endl;
  for(auto& elm: even)
  cout << elm << ' ';
  cout << endl;
  cout << "Here are some odd numbers:" << endl;
  for(auto& elm: odd)
  cout << elm << ' ';
  cout << endl;
  return 0;
}

OUTPUT:

Here are some even numbers:
0, 2, 4, 6, 8
Here are some odd numbers:
1, 3, 5, 7, 9

As you can see, C++ lists have an advantage over other containers in STL, as they are fast to ass items, in the beginning, middle, and end, as long as you have a C++ list iterator. Lists in C++ also traverse forward, as well as backward. The only disadvantage being that they may be slow at accessing items from the middle of the list.

Now that you know how to use lists within C++, it will get that much easier for you to add, store, delete, and/ or retrieve data into your system memory without any hassle.

Do You Know?
1. For Loop in C++
2. Vectors in C++
3. Difference Between C and C++
4. C++ Swap
5. C++ List

Pin It on Pinterest