Convert a vector to a list in C++

In this article, we will discuss how to convert a vector to a list in C++.

Table Of Contents

Method 1: Using Range based constructor of list

Pass the start, and end iterators of a vector, to the list constructor, while creating the list object. It will create a list object populated with the vector contents. Let’s see an example,

#include <iostream>
#include <vector>
#include <list>

template <typename T>
void displayList(std::list<T> listObj)
{
    // Iterate over all elements of list
    for(auto& elem : listObj)
    {
        std::cout<<elem << ", ";
    }
    std::cout<<std::endl;
}

int main()
{
    // Vector of integers
    std::vector<int> vectorOfNumbers {11, 12, 13, 11, 12, 11, 13, 14, 14, 15, 16, 15};

    // Create a list from vector elements
    // Using range constructor
    std::list<int> listOfNumbers(vectorOfNumbers.begin(), vectorOfNumbers.end());

    // Print all elements of list
    displayList(listOfNumbers);

    return 0;
}

Output

11, 12, 13, 11, 12, 11, 13, 14, 14, 15, 16, 15,

It converted the vector to a list object.

Advertisements

Method 2: using list::push_back()

Iterate over all the characters of a vector using a range based for loop. During iteration, add each character to the list using the member function push_back(). It accepts an element as an argument, and adds that to the end of list. Let’s see an example,

#include <iostream>
#include <vector>
#include <list>

template <typename T>
void displayList(std::list<T> listObj)
{
    // Iterate over all elements of list
    for(auto& elem : listObj)
    {
        std::cout<<elem << ", ";
    }
    std::cout<<std::endl;
}

int main()
{
    // Vector of integers
    std::vector<int> vectorOfNumbers {11, 12, 13, 11, 12, 11, 13, 14, 14, 15, 16, 15};

    // Create an empty list
    std::list<int> listOfNumbers;

    // Iterate over all elements of vector
    // and append each element to a list
    for (const auto& elem: vectorOfNumbers)
    {
        listOfNumbers.push_back(elem);
    }

    // Print all elements of list
    displayList(listOfNumbers);

    return 0;
}

Output

11, 12, 13, 11, 12, 11, 13, 14, 14, 15, 16, 15,

It converted the vector to a list object.

Method 3: Using copy() function

Pass two iterators pointing to the first, and one past the last element of vector to the copy() function, as a range. Also, pass a insert_iterator of list object to the copy() function. It will copy all the elements of vector, to the list.

A insert iterator is an output iterator, that allows the algorithms that usually overwrite elements (like copy()) to insert new elements automatically at a specific index position in the container. Let’s see an example.

#include <algorithm>

int example3()
{
    // Vector of integers
    std::vector<int> vectorOfNumbers {11, 12, 13, 11, 12, 11, 13, 14, 14, 15, 16, 15};

    // Create an empty list
    std::list<int> listOfNumbers;

    /*

    */
    // Copy all elements of vector from range [start, end]
    // to the end of list using an insert iterator. 
    // The inserter iterator will insert the elements at the end of list.
    std::copy(
        vectorOfNumbers.begin(),
        vectorOfNumbers.end(),
        std::inserter(listOfNumbers, listOfNumbers.end()));

    // Print all elements of list
    displayList(listOfNumbers);

    return 0;
}

Output

11, 12, 13, 11, 12, 11, 13, 14, 14, 15, 16, 15,

It converted the vector to a list object.

Method 4: Using STL Algorithm for_each()

Create an emply list. Then, apply a lambda function to every element of vector using the for_each() function of STL. In this lambda function, insert each element to the list. Let’s see an example,

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

template <typename T>
void displayList(std::list<T> listObj)
{
    // Iterate over all elements of list
    for(auto& elem : listObj)
    {
        std::cout<<elem << ", ";
    }
    std::cout<<std::endl;
}

int main()
{
    // Vector of integers
    std::vector<int> vectorOfNumbers {11, 12, 13, 11, 12, 11, 13, 14, 14, 15, 16, 15};

    // Create an empty list
    std::list<int> listOfNumbers;

    // Iterate over all elements of vector
    // and insert each element to a list
    std::for_each(
        vectorOfNumbers.begin(),
        vectorOfNumbers.end(),
        [&](auto& elem){ listOfNumbers.push_back(elem); });

    // Print all elements of list
    displayList(listOfNumbers);

    return 0;
}

Output

11, 12, 13, 11, 12, 11, 13, 14, 14, 15, 16, 15,

It converted the vector to a list object.

Method 5: Using list::insert()

Insert all elements of vector (range) to the list, by passing the start, and end iterators of vector to the list::insert() function. Let’s see an example,

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

template <typename T>
void displayList(std::list<T> listObj)
{
    // Iterate over all elements of list
    for(auto& elem : listObj)
    {
        std::cout<<elem << ", ";
    }
    std::cout<<std::endl;
}

int main()
{
    // Vector of integers
    std::vector<int> vectorOfNumbers {11, 12, 13, 11, 12, 11, 13, 14, 14, 15, 16, 15};

    // Create an empty list
    std::list<int> listOfNumbers;

    listOfNumbers.insert(
            listOfNumbers.begin(),
            vectorOfNumbers.begin(),
            vectorOfNumbers.end());

    // Print all elements of list
    displayList(listOfNumbers);

    return 0;
}

Output

11, 12, 13, 11, 12, 11, 13, 14, 14, 15, 16, 15,

It converted the vector to a list object.

Summary

We learned different ways to convert a vector to a list in C++.

Advertisements

Thanks for reading.

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