Map of list and forward_list in C++ STL with Examples - GeeksforGeeks

10 Jan, 2022
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values.
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. A list simply means a doubly-linked list and for a singly linked list forward List is used. 
Map of List in STL
A map of lists can be very useful in designing complex data structures.
Syntax:
map<datatype, list<datatype>> map_of_list 
This stores a list corresponding to a datatype

or

map<list<datatype>, datatype> map_of_list
This stores a datatype corresponding to a list
Below is the implementation of the Map of List in C++-
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;  void printMapContent1(map<list<int>,                          int>& mapOfList){    cout << "   Key         Value"         << "\n\n";    for (auto p : mapOfList) {        // Key is a list of integers        list<int> ourList = p.first;          // Value is an integer        int val = p.second;          // Printing list elements        cout << "[ ";        for (auto it = ourList.begin();             it != ourList.end(); it++) {            // Dereferencing value pointed by            // iterator            cout << (*it) << ' ';        }          cout << ']';          cout << "     ";          // Printing value        cout << mapOfList[ourList] << '\n';    }}  void printMapContent2(map<int,                          list<int> >& mapOfList){      cout << "   Key         Value"         << "\n\n";    for (auto p : mapOfList) {        // Key is an integer        int key = p.first;          // Value is a list of integers        list<int> ourList = p.second;          cout << "   ";        cout << key << "          ";          // Printing list elements        cout << "[ ";        for (auto it = ourList.begin();             it != ourList.end(); it++) {            // Dereferencing value pointed by            // iterator            cout << (*it) << ' ';        }          cout << ']';        cout << '\n';    }}  // Driver codeint main(){    // Declaring a list of integers    list<int> ourList1;      // Inserting elements at the    // back of list    ourList1.push_back(2);    ourList1.push_back(10);    ourList1.push_back(13);      // Declaring another list    list<int> ourList2;      // Inserting elements at the back    // of list    ourList2.push_back(7);    ourList2.push_back(14);    ourList2.push_back(22);      // Declaring a map where key is a list    // and value is integer itself    map<list<int>, int> mapOfList1;      mapOfList1[ourList1] = 5;    mapOfList1[ourList2] = 10;      // Printing the contents of the map    printMapContent1(mapOfList1);      // Declaring a map where key is integer    // and value is a list of integers    map<int, list<int> > mapOfList2;      cout << "\n\n";    mapOfList2[3] = ourList1;    mapOfList2[7] = ourList2;      printMapContent2(mapOfList2);      return 0;}
   Key         Value

[ 2 10 13 ]     5
[ 7 14 22 ]     10


   Key         Value

   3          [ 2 10 13 ]
   7          [ 7 14 22 ]
Forward List in STL 
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. forward_list can also be used with the map container.
Syntax: 
map<datatype, forward_list<datatype>> map_of_list
This stores a forward list corresponding to a datatype

or

map<forward_list<datatype>, datatype> map_of_list
This stores a datatype corresponding to a forward list
Below is the implementation of forward_list in C++- 
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;  void printMapContent1(map<forward_list<int>,                          int>& mapOfList){    cout << "   Key         Value"         << "\n\n";    for (auto p : mapOfList) {        // Key is a list of integers        forward_list<int> ourList = p.first;          // Value is an integer        int val = p.second;          // Printing list elements        cout << "[ ";        for (auto it = ourList.begin();             it != ourList.end(); it++) {            // Dereferencing value pointed by            // iterator            cout << (*it) << ' ';        }          cout << ']';        cout << "     ";          // Printing value        cout << mapOfList[ourList] << '\n';    }}  void printMapContent2(map<int,                          forward_list<int> >& mapOfList){      cout << "   Key         Value"         << "\n\n";    for (auto p : mapOfList) {        // Key is an integer        int key = p.first;          // Value is a list of integers        forward_list<int> ourList = p.second;          cout << "   ";        cout << key << "          ";          // Printing list elements        cout << "[ ";        for (auto it = ourList.begin();             it != ourList.end(); it++) {            // Dereferencing value pointed by            // iterator            cout << (*it) << ' ';        }          cout << ']';        cout << "\n";    }}  // Driver codeint main(){    // Declaring forward list    forward_list<int> forwardList1;      // Declaring another forward list    forward_list<int> forwardList2;      // Assigning values using assign()    forwardList1.assign({ 5, 3, 13 });    forwardList2.assign({ 8, 9, 13 });      map<forward_list<int>,        int>        mapOfList1;      mapOfList1[forwardList1] = 3;    mapOfList1[forwardList2] = 7;      printMapContent1(mapOfList1);    cout << "\n\n";      map<int,        forward_list<int> >        mapOfList2;      mapOfList2[3] = forwardList1;    mapOfList2[7] = forwardList2;      printMapContent2(mapOfList2);    return 0;}
   Key         Value

[ 5 3 13 ]     3
[ 8 9 13 ]     7


   Key         Value

   3          [ 5 3 13 ]
   7          [ 8 9 13 ]
 
surindertarika1234
simmytarika5
cpp-list
cpp-map
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Operator Overloading in C++
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
List in C++ Standard Template Library (STL)
Convert string to char array in C++
Pair in C++ Standard Template Library (STL)
Destructors in C++
new and delete operators in C++ for dynamic memory
Queue in C++ Standard Template Library (STL)