List of vectors in C++ STL with examples

• Difficulty Level : Medium
• Last Updated : 10 Nov, 2021

Lists

Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, the list has slow traversal, but once a position has been found, insertion and deletion are quick. Normally, when we say a List, we talk about a doubly linked list. For implementing a singly linked list, we use a forward list.

Functions used with 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.

Forward Lists

Forward list in STL implements singly linked list. Introduced from C++11, forward lists are more useful than other containers in insertion, removal, and moving operations (like sort) and allow time constant insertion and removal of elements. It differs from the list by the fact that the forward list keeps track of the location of only the next element while the list keeps track of both the next and previous elements, thus increasing the storage space required to store each element. The drawback of a forward list is that it cannot be iterated backward and its individual elements cannot be accessed directly.
Forward List is preferred over the list when only forward traversal is required (same as singly linked list is preferred over doubly linked list) as we can save space. Some example cases are, chaining in hashing, adjacency list representation of the graph, etc.

Functions used with Forward List:

• push_front(x):– Adds a new element ‘x’ at the beginning of the list.

Vectors

Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

Functions used with Vectors:

• size(): Returns the number of elements in the vector.
• push_back(): This function is used to push elements into a vector from the back.

A list of vectors can be quite useful while designing complex data structures. Each node can hold a vector.

Below is the implementation of the list of vectors:

C++

 `// C++ program to implement``// the above approach``#include ``using` `namespace` `std;`` ` `// Function to print list``// contents``void` `print(list >& listOfVectors)``{``    ``for` `(``auto` `vect : listOfVectors) {``        ``// Each element of the list is``        ``// a vector itself``        ``vector<``int``> currentVector = vect;`` ` `        ``cout << ``"[ "``;`` ` `        ``// Printing vector contents``        ``for` `(``auto` `element : currentVector)``            ``cout << element << ``' '``;`` ` `        ``cout << ``']'``;``        ``cout << ``'\n'``;``    ``}``}`` ` `// Driver code``int` `main()``{``    ``// Declaring a list of vectors``    ``list > listOfVectors;`` ` `    ``// Declaring a vector``    ``vector<``int``> vector1;`` ` `    ``// Adding elements into the``    ``// vector``    ``vector1.push_back(10);``    ``vector1.push_back(14);``    ``vector1.push_back(17);`` ` `    ``// Push the vector at the back``    ``// in the list``    ``listOfVectors.push_back(vector1);`` ` `    ``// Declaring another vector``    ``vector<``int``> vector2;`` ` `    ``// Adding elements in the vector``    ``vector2.push_back(2);``    ``vector2.push_back(6);``    ``vector2.push_back(11);`` ` `    ``// Push the vector at the back``    ``// in the list``    ``listOfVectors.push_back(vector2);`` ` `    ``// Declaring another vector``    ``vector<``int``> vector3;`` ` `    ``// Adding elements in the``    ``// vector``    ``vector3.push_back(11);``    ``vector3.push_back(16);``    ``vector3.push_back(29);`` ` `    ``// Push the vector at the front``    ``// in the list``    ``listOfVectors.push_front(vector3);`` ` `    ``// Calling print function``    ``print(listOfVectors);``    ``return` `0;``}`

Output

```[ 10 14 17 ]
[ 2 6 11 ]
[ 11 16 29 ]```

Below is the implementation of  forward list of vectors:

C++

 `// C++ program to implement``// the above approach``#include ``using` `namespace` `std;`` ` `// Function to print forward``// list contents``void` `print(forward_list >& forwardListOfVectors)``{``    ``for` `(``auto` `vect : forwardListOfVectors) {``        ``// Each element of the forward list``        ``// is a vector itself``        ``vector<``int``> currentVector = vect;`` ` `        ``cout << ``"[ "``;`` ` `        ``// Printing vector contents``        ``for` `(``auto` `element : currentVector)``            ``cout << element << ``' '``;`` ` `        ``cout << ``']'``;``        ``cout << ``'\n'``;``    ``}``}`` ` `// Driver code``int` `main()``{``    ``// Declaring a list of vectors``    ``forward_list > forwardListOfVectors;`` ` `    ``// Declaring a vector``    ``vector<``int``> vector1;`` ` `    ``// Adding elements into the vector``    ``vector1.push_back(10);``    ``vector1.push_back(14);``    ``vector1.push_back(17);`` ` `    ``// Push the vector in the forward``    ``// list``    ``forwardListOfVectors.push_front(vector1);`` ` `    ``// Declaring another vector``    ``vector<``int``> vector2;`` ` `    ``// Adding elements in the vector``    ``vector2.push_back(2);``    ``vector2.push_back(6);``    ``vector2.push_back(11);`` ` `    ``// Push the vector in the forward``    ``// list``    ``forwardListOfVectors.push_front(vector2);`` ` `    ``// Declaring another vector``    ``vector<``int``> vector3;`` ` `    ``// Adding elements in the vector``    ``vector3.push_back(11);``    ``vector3.push_back(16);``    ``vector3.push_back(29);`` ` `    ``// Push the vector in the forward``    ``// list``    ``forwardListOfVectors.push_front(vector3);`` ` `    ``// Calling print function``    ``print(forwardListOfVectors);`` ` `    ``return` `0;``}`

Output

```[ 11 16 29 ]
[ 2 6 11 ]
[ 10 14 17 ]```

My Personal Notes arrow_drop_up