Map of list and forward_list in C++ STL with Examples
Last Updated :
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++
#include <bits/stdc++.h>
using namespace std;
void printMapContent1(map<list< int >,
int >& mapOfList)
{
cout << " Key Value"
<< "\n\n" ;
for ( auto p : mapOfList) {
list< int > ourList = p.first;
int val = p.second;
cout << "[ " ;
for ( auto it = ourList.begin();
it != ourList.end(); it++) {
cout << (*it) << ' ' ;
}
cout << ']' ;
cout << " " ;
cout << mapOfList[ourList] << '\n' ;
}
}
void printMapContent2(map< int ,
list< int > >& mapOfList)
{
cout << " Key Value"
<< "\n\n" ;
for ( auto p : mapOfList) {
int key = p.first;
list< int > ourList = p.second;
cout << " " ;
cout << key << " " ;
cout << "[ " ;
for ( auto it = ourList.begin();
it != ourList.end(); it++) {
cout << (*it) << ' ' ;
}
cout << ']' ;
cout << '\n' ;
}
}
int main()
{
list< int > ourList1;
ourList1.push_back(2);
ourList1.push_back(10);
ourList1.push_back(13);
list< int > ourList2;
ourList2.push_back(7);
ourList2.push_back(14);
ourList2.push_back(22);
map<list< int >, int > mapOfList1;
mapOfList1[ourList1] = 5;
mapOfList1[ourList2] = 10;
printMapContent1(mapOfList1);
map< int , list< int > > mapOfList2;
cout << "\n\n" ;
mapOfList2[3] = ourList1;
mapOfList2[7] = ourList2;
printMapContent2(mapOfList2);
return 0;
}
|
Output
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++
#include <bits/stdc++.h>
using namespace std;
void printMapContent1(map<forward_list< int >,
int >& mapOfList)
{
cout << " Key Value"
<< "\n\n" ;
for ( auto p : mapOfList) {
forward_list< int > ourList = p.first;
int val = p.second;
cout << "[ " ;
for ( auto it = ourList.begin();
it != ourList.end(); it++) {
cout << (*it) << ' ' ;
}
cout << ']' ;
cout << " " ;
cout << mapOfList[ourList] << '\n' ;
}
}
void printMapContent2(map< int ,
forward_list< int > >& mapOfList)
{
cout << " Key Value"
<< "\n\n" ;
for ( auto p : mapOfList) {
int key = p.first;
forward_list< int > ourList = p.second;
cout << " " ;
cout << key << " " ;
cout << "[ " ;
for ( auto it = ourList.begin();
it != ourList.end(); it++) {
cout << (*it) << ' ' ;
}
cout << ']' ;
cout << "\n" ;
}
}
int main()
{
forward_list< int > forwardList1;
forward_list< int > forwardList2;
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;
}
|
Output
Key Value
[ 5 3 13 ] 3
[ 8 9 13 ] 7
Key Value
3 [ 5 3 13 ]
7 [ 8 9 13 ]
Share your thoughts in the comments
Please Login to comment...