How to convert an Array to List in C++?

In this article, we will discuss different ways to convert an array to a list in C++.

Table Of Contents

Problem Description

We have been given an array containing n number of elements, and we have to convert this array to a List in C++ .

What is a List in C++ ?

A list in C++ is a sequential container, which is implemented as a doubly linked list. The list iteration is possible in both the directions.

There are different ways to a convert a given array to list. Let’s discuss them one by one,

Convert an Array to List using for loop (range based)

In this approach we will use loop which will iterate over each element of an array and add each element encountered to the list, using the function list::push_back()

Example

#include <iostream>
#include <list>

int main()
{
    int arr[] = { 4, 10, 25, 6, 20, 40 };

    // intialize the list
    std::list<int> listOfNumbers;

    // push each element to the list
    for (const int& i: arr)
    {
        listOfNumbers.push_back(i);
    }

    // print the elements of the list 
    for (const int& i: listOfNumbers) 
    {
        std::cout << i << " ";
    }

    std::cout<<std::endl;

    return 0;
}

Output

4 10 25 6 20 40

We copied all the elements of an array to a list in C++.

Convert an array to a list using std::insert()

In this approach we will first initialize an empty List and then we will use the std::insert() function of list, which takes three parameters. The first parameter is the iterator that points to the beginning of destination list. The second and third parameters are also iterators that defines the range of the array elements that needs to be inserted in the list. As the second and third parameters, we will pass the iterators pointing to beginning and end of array.

So, it will insert all the elements of array to the list. Let’s see complete example,

Example

#include <iostream>
#include <list>

int main()
{
    int array[] = { 4, 10, 25, 6, 20, 40 };

     // intialize the list 
    std::list<int> listOfNumbers;  

    listOfNumbers.insert(listOfNumbers.begin(),
                         std::begin(array),
                         std::end(array) );

     // print the elements of the list
    for(auto i : listOfNumbers)
    {
        std::cout << i << " ";  
    }
    std::cout<< std::endl;
    return 0;
}

Output

4 10 25 6 20 40

We copied all the elements of an array to a list in C++.

Convert an array to a list using the range constructor

In this approach we can use the range constructor of std::list. It takes two parameters, and both the parametres are input iterators which signifies the range. We will pass two pointers as range in the list constructor. First pointer will point to the start of array and the second points to the end of array. This will construct a new list containing all the elements of given array. Let’s see complete example,

Example

#include <iostream>
#include <list>

int main()
{
    int array[] = { 4, 10, 25, 6, 20, 40 };


    int * start = array;
    int * end = array + (sizeof(array) / sizeof(int));

    // intialize the list with elements
    std::list<int> listOfNumbers(start, end);  

    // print the elements of the list
    for (int i: listOfNumbers) 
    {
        std::cout<< i << " ";  
    }

    std::cout<<std::endl;

    return 0;
}

Output

4 10 25 6 20 40

In this example, we converted an array to a list in C++.

Convert an array to a list using std::copy()

In this approach, we will use the std::copy() function, which copies the given range of elements to an another range. This function takes three parameters. The first two parameters specifies the range of array elements, but for the third parameter we will use the std::back_inserter to the starting position in destination list. It will call the push_back() function of list behind the scenes.

So, the std::copy() function will copy all the elements from the given range (array in our case) to the destination range(list in our case). Let’s see complete example,

Example

#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>

int main()
{
    int arr[] = { 4, 10, 25, 6, 20, 40  };

     // intialize the list
    std::list<int> listOfNumbers;

     // copy the elemnts of array to the list
    std::copy(  std::begin(arr),
                std::end(arr),
                std::back_inserter(listOfNumbers) );

     // print the elements of the list
    for (int i : listOfNumbers) 
    {
        std::cout << i << " ";
    }
    std::cout << std::endl;

    return 0;
}

Output

4 10 25 6 20 40

In this example, we converted an array to a list in C++.

Convert an array to a list using std::assign

In this approach we will use the list’s function std::assign() which takes two parameters. Both the parameters signifies the range of the array. So, to copy all elements of array to list, we will pass the pointers to pointing to start and end of the array. This function will replace the existing elements of the list with the elements of the array. Let’s see complete example,

Example

#include <iostream>
#include <list>

int main()
{
    int arr[] = { 4, 10, 25, 6, 20, 40  };

     // intialize the list
    std::list<int> listOfNumbers;

     // determine the size of array
    int n = sizeof(arr)/sizeof(int);

     // replaces the elemnts
    listOfNumbers.assign(arr, arr + n);

     // print the elements of the list
    for (int i: listOfNumbers)
    {
        std::cout << i << " ";
    }
    std::cout<< std::endl;

    return 0;
}

Output

4 10 25 6 20 40

In this example, we converted an array to a list in C++ using the assign() function of list.

Summary

In this article we learnt how to convert a given array to a list in C++, by using various approaches such as using various STL functions like copy(), assign(), insert() and by using simple for loop as well as range constructor. Happy Learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top