Forward List and List of Tuples in C++ with Examples - GeeksforGeeks

24 Dec, 2021
What is Forward List?
Forward list in STL is used to implement a singly linked list. It was introduced from C++11 onwards, 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 associated with forward list:
push_front(): This function is used to insert the element at the first position in a forward list. The value from this function is copied to the space before the first element in the container. The size of the forward list increases by 1.
pop_front(): This function is used to delete the first element of the list.
empty(): Returns whether the list is empty(1) or not(0).
What is List?
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 associated with a list:
front(): Returns the value of the first element in the list.
back(): Returns the value of the last element in the 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.
empty(): Returns whether the list is empty(1) or not(0).
What is Tuple?
A tuple in C++ is an object that has the ability to group a number of elements. The elements can be of the same type as well as different data types. The order in which tuple elements are initialized can be accessed in the same order.
Functions associated with a tuple:
1. make_tuple(): It is used to assign tuples with values. The values passed should be in order with the values declared in the tuple.2. get(): It is used to access the tuple values and modify them, it accepts the index and tuple name as arguments to access a particular tuple element.
Forward list of tuples
In C++, A forward list of tuples is a forward list in which each element is a tuple itself. Although, a tuple can contain more or fewer elements, for simplicity we have used tuples having three elements only. 
Syntax:
 forward_list<tuple<dataType1, dataType2, dataType3> myForwardList;
Here, 
dataType1, dataType2, and dataType3 are similar or dissimilar data types.
Example 1: Below is the C++ program to demonstrate the working of forward list of tuples. 
C++
// C++ program to demonstrate // the working of forward list// of tuples#include <bits/stdc++.h>using namespace std;  // Function to print forward // list elementsvoid print(forward_list<tuple<int, int,            int>> &forwardListOftuples){  for (auto currentTuple : forwardListOftuples)   {    // Each element of the forward list is    // a tuple itself    tuple<int, int, int> tuple = currentTuple;      cout << "[ ";      // Printing tuple elements    cout << get<0>(currentTuple) << ' ' <<             get<1>(currentTuple) << ' ' <<             get<2>(currentTuple);    cout << ']';    cout << '\n';  }}  // Driver codeint main(){  // Declaring a forward list of tuples  // having integer values only  forward_list<tuple<int, int, int> >     forwardListOftuples;    // Declaring a tuple  tuple<int, int, int> tuple1;    // Initializing the  // tuple  tuple1 = make_tuple(11, 22, 33);    // Push the tuple at the back  // in the forward list  forwardListOftuples.push_front(tuple1);    // Declaring another tuple  tuple<int, int, int> tuple2;    // Initializing the  // tuple  tuple2 = make_tuple(33, 44, 55);    // Push the tuple at the front  // in the forward list  forwardListOftuples.push_front(tuple2);    // Declaring another tuple  tuple<int, int, int> tuple3;    // Initializing the tuple  tuple3 = make_tuple(55, 66, 77);    // Push the tuple at the front  // in the forward list  forwardListOftuples.push_front(tuple3);    // Declaring another tuple  tuple<int, int, int> tuple4;    // Initializing the tuple  tuple4 = make_tuple(77, 88, 99);    // Push the tuple at the front  // in the forward list  forwardListOftuples.push_front(tuple4);    // Calling print function  print(forwardListOftuples);  return 0;}
Output:
[ 77 88 99][ 55 66 77][ 33 44 55][ 11 22 33]
Example 2: Below is the C++ program to demonstrate the working of forward list of tuples.
C++
// C++ program to demonstrate // the working of forward list// of tuples#include <bits/stdc++.h>using namespace std;  // Function to print forward // list elementsvoid print(forward_list<tuple<string, string,            bool>> &forwardListOftuples){  for (auto currentTuple : forwardListOftuples)   {    // Each element of the forward list is    // a tuple itself    tuple<string, string, bool> tuple =                           currentTuple;      cout << "[ ";      // Printing tuple elements    cout << get<0>(currentTuple) << ' ' <<             get<1>(currentTuple) << ' ' <<             get<2>(currentTuple);    cout << ']';    cout << '\n';  }}  // Driver codeint main(){  // Declaring a forward list of tuples  // having first two values of string   // type and third value as bool type  forward_list<tuple<string, string, bool>>     forwardListOftuples;    // Declaring a tuple  tuple<string, string, bool> tuple1;    // Initializing the  // tuple  tuple1 = make_tuple("GeeksforGeeks",                       "Computer Science", 0);    // Push the tuple at the back  // in the forward list  forwardListOftuples.push_front(tuple1);    // Declaring another tuple  tuple<string, string, bool> tuple2;    // Initializing the  // tuple  tuple2 = make_tuple("Java", "C++", 1);    // Push the tuple at the front  // in the forward list  forwardListOftuples.push_front(tuple2);    // Declaring another tuple  tuple<string, string, bool> tuple3;    // Initializing the tuple  tuple3 = make_tuple("GFG", "C", 1);    // Push the tuple at the front  // in the forward list  forwardListOftuples.push_front(tuple3);    // Declaring another tuple  tuple<string, string, bool> tuple4;    // Initializing the tuple  tuple4 = make_tuple("Swift", "Python", 0);    // Push the tuple at the front  // in the forward list  forwardListOftuples.push_front(tuple4);    // Calling print function  print(forwardListOftuples);  return 0;}
Output:
[ Swift Python 0][ GFG C 1][ Java C++ 1][ GeeksforGeeks Computer Science 0]
List of tuples
In C++, a list of tuples is a list in which each element is a tuple itself. Although, a tuple can contain more or fewer elements, for simplicity we have used tuples having three elements only. 
Syntax:
list<tuple<dataType1, dataType2, dataType3> myList;
Here,
dataType1, dataType2, and dataType3 are similar or dissimilar data types.
Example 1: Below is the C++ program to demonstrate the working of list of tuples.
C++
// C++ program to demonstrate // the working of list of tuples#include <bits/stdc++.h>using namespace std;  // Function to print// list elementsvoid print(list<tuple<int, int,            int>> &listOftuples){  for (auto currentTuple : listOftuples)   {    // Each element of the List is    // a tuple itself    tuple<int, int, int> tuple = currentTuple;      cout << "[ ";      // Printing tuple elements    cout << get<0>(currentTuple) << ' ' <<             get<1>(currentTuple) << ' ' <<             get<2>(currentTuple);    cout << ']';    cout << '\n';  }}  // Driver codeint main(){  // Declaring a List of tuples  // having all values of integer type  list<tuple<int, int, int>>   listOftuples;    // Declaring a tuple  tuple<int, int, int> tuple1;    // Initializing the  // tuple  tuple1 = make_tuple(11, 22, 33);    // Push the tuple at the back  // in the List  listOftuples.push_front(tuple1);    // Declaring another tuple  tuple<int, int, int> tuple2;    // Initializing the  // tuple  tuple2 = make_tuple(33, 44, 55);    // Push the tuple at the front  // in the List  listOftuples.push_front(tuple2);    // Declaring another tuple  tuple<int, int, int> tuple3;    // Initializing the tuple  tuple3 = make_tuple(55, 66, 77);    // Push the tuple at the front  // in the List  listOftuples.push_front(tuple3);    // Declaring another tuple  tuple<int, int, int> tuple4;    // Initializing the tuple  tuple4 = make_tuple(77, 88, 99);    // Push the tuple at the front  // in the List  listOftuples.push_front(tuple4);    // Calling print function  print(listOftuples);  return 0;}
Output:
[ 77 88 99][ 55 66 77][ 33 44 55][ 11 22 33]
Example 2: Below is the C++ program to demonstrate the working of list of tuples.
C++
// C++ program to demonstrate // the working of list of tuples#include <bits/stdc++.h>using namespace std;  // Function to print// list elementsvoid print(list<tuple<string, string,            bool>> &listOftuples){  for (auto currentTuple : listOftuples)   {    // Each element of the List is    // a tuple itself    tuple<string, string, bool> tuple =                           currentTuple;      cout << "[ ";      // Printing tuple elements    cout << get<0>(currentTuple) << ' ' <<             get<1>(currentTuple) << ' ' <<             get<2>(currentTuple);    cout << ']';    cout << '\n';  }}  // Driver codeint main(){  // Declaring a List of tuples  // having first two values as string type  // and third value is of bool type  list<tuple<string, string, bool>>   listOftuples;    // Declaring a tuple  tuple<string, string, bool> tuple1;    // Initializing the  // tuple  tuple1 = make_tuple("GeeksforGeeks",                       "Computer Science", 0);    // Push the tuple at the back  // in the List  listOftuples.push_front(tuple1);    // Declaring another tuple  tuple<string, string, bool> tuple2;    // Initializing the  // tuple  tuple2 = make_tuple("Java", "C++", 1);    // Push the tuple at the front  // in the List  listOftuples.push_front(tuple2);    // Declaring another tuple  tuple<string, string, bool> tuple3;    // Initializing the tuple  tuple3 = make_tuple("GFG", "C", 1);    // Push the tuple at the front  // in the List  listOftuples.push_front(tuple3);    // Declaring another tuple  tuple<string, string, bool> tuple4;    // Initializing the tuple  tuple4 = make_tuple("Swift", "Python", 0);    // Push the tuple at the front  // in the List  listOftuples.push_front(tuple4);    // Calling print function  print(listOftuples);  return 0;}
Output:
[ Swift Python 0][ GFG C 1][ Java C++ 1][ GeeksforGeeks Computer Science 0]
simranarora5sos
CPP-forward-list
cpp-list
cpp-tuple
STL
C++
STL
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++
Friend class and function in C++
Polymorphism in C++
List in C++ Standard Template Library (STL)
Pair in C++ Standard Template Library (STL)
Convert string to char array in C++
new and delete operators in C++ for dynamic memory
Destructors in C++
Queue in C++ Standard Template Library (STL)