C++

How to Use C++ Array List

In C++ arraylist different kinds of data can be kept. It is dynamic and can have its size changed dynamically. Integer indexes are a way to access an arraylist members. In this guide, we will discuss the working of arraylist in C++.

What is arraylist in C++?

A collection used to store several types of data is called an arraylist. Unlike C++’s arrays, it is a versatile list that can have its size changed dynamically. Integer indexes can be used to access an arraylist’s members and data.

In the arraylist, two different kinds of information can be kept. In C++, indexing through integers makes it simple to navigate across an arraylist. However, List has gradually taken the place of the arraylist in C++. Since Lists in C++ are implemented as doubly linked lists, data can be accessed in both directions.

Syntax

To use List in C++ first import the <list> header file into the program. The basic syntax for using the List in C++ programs is shown below:

list<Type> list_name = {value1, value2, ...};

In the above syntax Type is the data type.

Example 1: C++ arraylist using List – push_back() Function

Following is an example of an arraylist in C++:

#include <iostream>

#include <list>

using namespace std;

int main() {

  list<float> numList;

  numList.push_back(10.1);

  numList.push_back(20.2);

  numList.push_back(30.3);

  for (auto element: numList) {

      cout << element << " ";

  }

  cout << endl;

  return 0;

}

In the above example, first, we created a list of floats. After creating the list, we added float integers with the help of the push_back function, and then we printed the list which shows the following output:

Example 2: C++ arraylist using List – push_front() Function

The example of List using push_front() function is mentioned below:

#include <iostream>

#include <list>

using namespace std;

int main() {

  list<float> numList;

  numList.push_front(10.1);

  numList.push_front(20.2);

  numList.push_front(30.3);

  for (auto j: numList) {

    cout << j << " ";

  }

  return 0;

}

The output will be the opposite of the push_back() function:

Example 3: C++ arraylist using List – remove() Function

To delete an element from a C++ List use remove() function:

#include <iostream>

#include <list>

using namespace std;

int main() {

  list<float> numList = {10.1, 20.2, 30.3};

  numList.remove(10.1);

  for (auto j: numList) {

    cout << j << " ";

  }

  return 0;

}

Element 10.1 has been removed:

Example 4: C++ arraylist using List – size() Function

To get the size of the List use:

#include <iostream>

#include <list>

using namespace std;

int main() {

  list<float> numList = {10.1, 20.2, 30.3, 40.2, 22.1};

  int size = numList.size();

    cout << "Size of the List is: " << size;

  return 0;

}

The output is given below:

How does the arraylist work in C++?

A few key points defining the operation and features of the list in C++ are as follows:

  • In C++, a list is created as a doubly linked list, allowing insertion, deletion, and access from both directions.
  • The doubly linked list of the list’s previous and next elements is used to link the list’s elements to one another.
  • The list is not regarded as an excellent alternative for a small number of components because it takes up more memory than other containers while maintaining linkages to its preceding and previous elements.
  • The ability to increase or reduce the size of a list in C++ at runtime is provided. Practically, a zero-length list is also achievable.

Commonly Used arraylist Functions

The following table includes some of the most popular list functions:

Name of the function Working
list::begin() This function returns an iterator that points to the list’s first entry.
list::end() This function returns an iterator that points to the list’s last entry.
push_front() The element at the starting point of the element is pushed by this function.
push_back() The element on the final spot of the list is pushed by this function.
size() The total number of elements in a list is returned by this function.
sort() This function arranges the list’s elements in ascending order.
remove() By using this function, an element is removed from the list.

Conclusion

In C++ the arraylist has been replaced with the List. There are various functions that can be implemented to manipulate the arrays. In this guide, we learned how to add, remove elements, and check the size of the List. Moreover, various functions that List supports are also listed.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.