Array of list in C++ with Examples - GeeksforGeeks

24 Jan, 2022
What is an array?
An array in any programming language is a data structure that is used to store elements or data items of similar data types at contiguous memory locations and elements can be accessed randomly using indices of an array. Arrays are efficient when we want to store a large number of elements that too of similar data types.
What is a list?
In C++, a list is a sequence container that allows non-contiguous memory allocation. If we compare a vector with a list, then a list has slow traversal as compared to a vector but once a position has been found, insertion and deletion are quick. Generally, a list in C++ is a doubly-linked list.
Functions used with List:
front(): Returns the value of the first element in the list.
back(): Returns the value of the last element in the list.
push_front(x): Adds a new element ‘x’ at the beginning of the list.
push_back(x): Adds a new element ‘x’ at the end of the list.
pop_front(): Removes the first element of the list, and reduces the size of the list by 1.
pop_back(): Removes the last element of the list, and reduces the size of the list by 1.
Array of lists
C++ allows us a facility to create an array of lists. An array of lists is an array in which each element is a list on its own.
Syntax:
list<dataType> myContainer[N];
Here,N: The size of the array of the lists.dataType: A dataType. It signifies that each list can store elements of this data type only.
Example 1: Below is the C++ program to implement an array of lists.
C++
// C++ program to demonstrate the// working of array of lists in C++#include <bits/stdc++.h>using namespace std;  // Function to print list elements// specified at the index, "index"void print(list<int>& mylist,           int index){    cout << "The list elements stored at the index " <<              index << ": \n";      // Each element of the list is a pair on     // its own    for (auto element : mylist)     {        // Each element of the list is a pair         // on its own        cout << element << '\n';    }    cout << '\n';}  // Function to iterate over all the arrayvoid print(list<int>* myContainer, int n){    cout << "myContainer elements:\n\n";      // Iterating over myContainer elements    // Each element is a list on its own    for (int i = 0; i < n; i++)     {        print(myContainer[i], i);    }}  // Driver codeint main(){    // Declaring an array of lists    // In list each element is of type int    list<int> myContainer[3];      // listing values to the list stored    // at the index 0    // 15 <-> 5 <-> 10 <-> 20    myContainer[0].push_front(5);    myContainer[0].push_back(10);    myContainer[0].push_front(15);    myContainer[0].push_back(20);      // listing values to the list stored    // at the index 1    // 40 <-> 30 <-> 35 <-> 45    myContainer[1].push_front(30);    myContainer[1].push_back(35);    myContainer[1].push_front(40);    myContainer[1].push_back(45);      // listing values to the list stored    // at the index 2    // 60 <-> 50 <-> 55 <-> 65    myContainer[2].push_front(50);    myContainer[2].push_back(55);    myContainer[2].push_front(60);    myContainer[2].push_back(65);      // Calling print function to iterate    // over myContainer elements    print(myContainer, 3);      return 0;}
myContainer elements:
The list elements stored at the index 0:1551020
The list elements stored at the index 1:40303545
The list elements stored at the index 2:60505565
Example 2: Below is the C++ program to implement an array of lists.
C++
// C++ program to demonstrate the// working of array of lists in C++#include <bits/stdc++.h>using namespace std;  // Function to print list elements// specified at the index, "index"void print(list<string>& mylist,           int index){    cout << "The list elements stored at the index " <<              index << ": \n";      // Each element of the list is a pair     // on its own    for (auto element : mylist)     {        // Each element of the list is a pair         // on its own        cout << element << '\n';    }    cout << '\n';}  // Function to iterate over all the arrayvoid print(list<string>* myContainer, int n){    cout << "myContainer elements:\n\n";      // Iterating over myContainer elements    // Each element is a list on its own    for (int i = 0; i < n; i++)     {        print(myContainer[i], i);    }}  // Driver codeint main(){    // Declaring an array of lists    // In list each element is of type string    list<string> myContainer[3];      // listing values to the list stored    // at the index 0    // "GeeksforGeeks" <-> "C++" <->     // "Python" <-> "C"    myContainer[0].push_front("C++");    myContainer[0].push_back("Python");    myContainer[0].push_front("GeeksforGeeks");    myContainer[0].push_back("C");      // listing values to the list stored    // at the index 1    // "Nainwal" <-> "Java" <-> "C#" <-> "GFG"    myContainer[1].push_front("Java");    myContainer[1].push_back("C#");    myContainer[1].push_front("Nainwal");    myContainer[1].push_back("GFG");      // listing values to the list stored    // at the index 2    // "HTML" <-> "Swift" <-> "R" <-> "CSS"    myContainer[2].push_front("Swift");    myContainer[2].push_back("R");    myContainer[2].push_front("HTML");    myContainer[2].push_back("CSS");      // Calling print function to iterate    // over myContainer elements    print(myContainer, 3);      return 0;}
myContainer elements:
The list elements stored at the index 0:GeeksforGeeksC++PythonC
The list elements stored at the index 1:NainwalJavaC#GFG
The list elements stored at the index 2:HTMLSwiftRCSS
simmytarika5
Arrays
cpp-array
cpp-list
STL
Arrays
C++
Arrays
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Window Sliding Technique
Program to find sum of elements in a given array
Move all negative numbers to beginning and positive to end with constant extra space
Reversal algorithm for array rotation
Find duplicates in O(n) time and O(1) extra space | Set 1
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
Multidimensional Arrays in C / C++