Set of List and Forward List in C++ with examples
Sets
Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.
Functions used with Sets:
- begin(): Returns an iterator to the first element in the set.
- end(): Returns an iterator to the theoretical element that follows the last element in the set.
- size(): Returns the number of elements in the set.
- max_size(): Returns the maximum number of elements that the set can hold.
- empty(): Returns whether the set is empty.
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 Lists:
- push_front(): This function is used to push elements into a list from the front.
- push_back(): This function is used to push elements into a list from the back.
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.
Functions used with Forward Lists:
- push_front(): This function is used to push elements into a Forward list from the front.
Sets of List in STL
A set of lists can be very useful in designing complex data structures.
Syntax:
set<list<data_type>> set_of_list: This stores lists. set<forward_list<data_type>> set_of_forward_list: This stores forward lists.
Below is the C++ program to demonstrate the implementation of set of lists:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to print set contents void print(set<list< int > >& setOfList) { for ( auto x : setOfList) { // Each element of the set is // a list itself list< int > li = x; // Printing list elements cout << "[ " ; for ( auto element : li) cout << element << ' ' ; cout << ']' ; cout << '\n' ; } } // Driver code int main() { set<list< int > > setOfList; // Declaring a list list< int > list1; // Pushing elements in the list // Pushing at the back list1.push_back(10); list1.push_back(12); // Pushing at the front list1.push_front(21); // Pushing at the back list1.push_back(16); // Inserting a list into the set setOfList.insert(list1); // Declaring another list list< int > list2; // Pushing elements in the list // Pushing at the back list2.push_back(6); list2.push_back(9); list2.push_back(11); // Pushing at the front list2.push_front(2); setOfList.insert(list2); // Declaring another list list< int > list3; // Pushing elements in the list // Pushing at the back list3.push_back(2); list3.push_back(6); list3.push_back(9); list3.push_back(1); setOfList.insert(list3); print(setOfList); return 0; } |
[ 2 6 9 1 ] [ 2 6 9 11 ] [ 21 10 12 16 ]
Below is the C++ program to demonstrate the implementation of set of forward lists:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to print set contents void print(set<forward_list< int > >& setOfForwardList) { for ( auto x : setOfForwardList) { // Each element is a forward list // itself forward_list< int > li = x; cout << "[ " ; for ( auto element : li) cout << element << ' ' ; cout << ']' ; cout << '\n' ; } } // Driver code int main() { // Declaring a setOfForwardList set<forward_list< int > > setOfForwardList; // Declaring a forward list forward_list< int > forwardList1; // Pushing elements in the forward // list forwardList1.push_front(10); forwardList1.push_front(12); forwardList1.push_front(21); forwardList1.push_front(16); // Inserting forward list into // the set setOfForwardList.insert(forwardList1); // Declaring another forward list forward_list< int > forwardList2; // Pushing elements in the forward // list forwardList2.push_front(6); forwardList2.push_front(9); forwardList2.push_front(11); forwardList2.push_front(2); // Inserting forward list into // the set setOfForwardList.insert(forwardList2); // Print set contents print(setOfForwardList); return 0; } |
[ 2 11 9 6 ] [ 16 21 12 10 ]
By default, lists are arranged in non – descending order in the set and follow the logic that in the set, if the first value of two lists is equal then the second value of lists is compared, and so on.
Please Login to comment...