Open In App

List of Vectors in C++ STL

Last Updated : 03 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the list of vector refers to the list container in which each element is a vector. In this article, we will learn about the list of vectors in C++.

Let’s take a look at an example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    list<vector<int>> l = {{1, 3}, {2, 8}};
  
    for (auto v : l) {
        for (auto i : v)
            cout << i << " ";
        cout << endl;
    }
    return 0;
}

Output
1 3 
2 8 

This article covers the syntax, basic operations, usage, and common queries of list of vectors in C++:


Syntax of List of Vector

The list of vectors can be defined by passing the vector of given type as template parameter

list<vector<T>> l;

where l is the name of list and T is the datatype of vector which we have to store in list.

Declaration and Initialization

Declaration and initialization are the process of creating a list of vectors and assigning it some initial value. In C++, it can be done in multiple ways as shown below:

1. Default Initialization

An empty list of vectors can be created using the below declaration. This can be filled later on in the program.

list<vector<T>> v;

2. Initialization with Size and Default Value

A list in with given number of vectors each of given size can be initialized to a default value using the below syntax. If a specific size can also be declared and initialized to the given value as default value.

list<vector<T>> v(size, val);

3. Initialization Using Initializer List

List of vectors can also be initialized using initializer list in a similar way as 2d vector.

list<vector<T>> v(m, vector<T>(n, val) = {{ v1, v2, v3….},
{ v1, v2, v3….}};

Let's take a look at an example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {

    // Creating the list of vector
    list<vector<char>> l;

    // Inserting vectors into list
    l.push_back(vector<char>({'s', 'm', 'a'}));
    l.push_back(vector<char>({'d', 'f', 'g'}));
    l.push_back(vector<char>({'t', 'q', 'm'}));

    // Printing list of vectors
    for (auto v : l) {
        for (auto i : v)
            cout << i << " ";
        cout << endl;
    }
    return 0;
}

Output
s m a 
d f g 
t q m 

Basic Operations

The basic operations on list of vectors have to be done by keeping in mind that each element is a vector.

Operation

Description

Insert Elements

Use list push_back(), list push_front(), insert() for inserting the elements at end, start and any specified position in list respectively.

Delete Elements

Use erase() to remove the vector from the list.

Traversal

Use range based for loop or traditional loop to access the every elements of list of vector.

The below example demonstrates how to perform these basic operations on a list of vectors:

C++
#include <bits/stdc++.h>
using namespace std;

// Function to Print list of vectors
void print(list<vector<int>> &l) {
    for (auto v : l) {
        for (auto i : v)
            cout << i << " ";
        cout << endl;
    }
    cout << endl;
}

int main() {

    // Creating the list of vector
    list<vector<int>> l;

    vector<int> v1 = {1, 3, 7};
    vector<int> v2 = {2, 5, 4};
    vector<int> v3 = {9, 6, 7};
    vector<int> v4 = {11, 10, 8};

    // Inserting vectors to back of list
    l.push_back(v1);
    l.push_back(v2);

    // Insert vector to front of list
    l.push_front(v3);

    // Insert vector at 1st index
    auto it = l.begin();
    it++;
    l.insert(it, v4);
  
    print(l);

    // Remove the first vector from list
    l.erase(l.begin());
  
    print(l);
    return 0;
}

Output
9 6 7 
11 10 8 
1 3 7 
2 5 4 

11 10 8 
1 3 7 
2 5 4 

Next Article
Practice Tags :

Similar Reads