Set of List and Forward List in C++ with examples
Last Updated :
10 Nov, 2021
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;
}
Output[ 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;
}
Output[ 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.
Similar Reads
Forward List and List of Pairs in C++ with Examples
Forward List 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
8 min read
Map of list and forward_list in C++ STL with Examples
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,
5 min read
Forward List and List of Unordered Maps in C++ with Examples
Forward List 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
10 min read
forward_list::cend() in C++ STL with Example
forward_list::cend() is a function in C++ STL which returns a constant iterator pointing to the past-the-last element of the forward_list. The iterator returned by the function does not point to any element in the container, but to the position followed by the last element of the forward list contai
2 min read
forward_list max_size() in C++ STL with Examples
The forward_list::max_size() is a built-in function in C++ STL which returns the maximum number of elements a forward_list container can hold Syntax: forward_list_name.max_size() Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of element
1 min read
Array of list in C++ with Examples
What is an array? An array in any programming language is a data structure that is used to store elements or data items of similar data types at contiguous memory locations and elements can be accessed randomly using indices of an array. Arrays are efficient when we want to store a large number of e
5 min read
Multiset of Vectors in C++ with Examples
What is Multiset? A multiset in C++ is an associative container that can hold a number of elements in a specific order. Unlike a set, a multiset can hold multiple copies of the same element. Functions associated with a multiset: begin(): Returns an iterator to the first element in the multiset.end()
4 min read
Multiset of Pairs in C++ with Examples
What is Multiset? A multiset is an associative container that can hold a number of elements in a specific order. Unlike a set, a multiset can contain multiple occurrences of the same element. Some of the functions associated with a multiset: begin(): Returns an iterator to the first element in the m
4 min read
Multiset of Tuples in C++ with Examples
What is a tuple? A tuple in C++ is an object which binds a group of elements together. The elements can be similar as well as different data types. The elements of tuples are initialized as in the order in which they will be accessed. Syntax: tuple<data_type1, data_type2, dataType3, ....> myTu
5 min read
forward_list::front() and forward_list::empty() in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of
3 min read