Skip to content
Related Articles

Related Articles

Forward List and List of Pairs in C++ with Examples

View Discussion
Improve Article
Save Article
  • Last Updated : 14 Feb, 2022
View Discussion
Improve Article
Save Article

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 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 used 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.

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 used with the 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.

Pair

A pair container is a simple container that is defined in <utility> header consisting of two data elements or objects. In a pair first object is referenced with “first” and the second object is referenced as “second” and order is fixed as {first, second}.

Syntax 1: 

forward_list<pair<data_type1, data_type2>> forwardList;

Here,
data_type1 and data_type2 are the data types.
Declared forward list can store pairs as an element that consist of those data types only. 

Syntax 2: 

list<pair<data_type1, data_type2>> List;

Here, 
data_type1 and data_type2 are the data types.
Declared list can store pairs as an element that consists of those data types only. 

Forward List of Pairs

Below is the implementation of a forward list of pairs:  

Example 1:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print forward
// list elements
void print(forward_list<pair<int, int> >&
           forwardListOfPairs)
{
 
  cout << "Forward List : " << '\n';
  for (auto currentPair : forwardListOfPairs)
  {
    // Each element of the forwardList is
    // a pair itself
    pair<int, int> currentpair = currentPair;
 
    cout << "[ ";
 
    // Printing pair contents
    cout << currentPair.first << ' ' <<
            currentPair.second;
    cout << ']';
    cout << '\n';
  }
}
 
// Driver code
int main()
{
  // Declaring a forward list of pairs
  forward_list<pair<int, int> >
  forwardListOfPairs;
 
  // Declaring a pair
  pair<int, int> pair1;
 
  // Initializing the
  // pair
  pair1 = make_pair(11, 22);
 
  // Push the pair at the back
  // in the forward list
  forwardListOfPairs.push_front(pair1);
 
  // Declaring another pair
  pair<int, int> pair2;
 
  // Initializing the
  // pair
  pair2 = make_pair(33, 44);
 
  // Push the pair at the front
  // in the forward list
  forwardListOfPairs.push_front(pair2);
 
  // Declaring another pair
  pair<int, int> pair3;
 
  // Initializing the pair
  pair3 = make_pair(55, 66);
 
  // Push the pair at the front
  // in the forward list
  forwardListOfPairs.push_front(pair3);
 
  // Declaring another pair
  pair<int, int> pair4;
 
  // Initializing the pair
  pair4 = make_pair(77, 88);
 
  // Push the pair at the front
  // in the forward list
  forwardListOfPairs.push_front(pair4);
 
  // Calling print function
  print(forwardListOfPairs);
  return 0;
}


 
 

Output

Forward List : 
[ 77 88]
[ 55 66]
[ 33 44]
[ 11 22]

 

Example 2:

 

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print list
// contents
void print(forward_list<pair<int, string> >&
           forwardListOfPairs)
{
  cout << "Forward List : " << '\n';
   
  for (auto currentPair : forwardListOfPairs)
  {
    // Each element of the forward list is
    // a pair itself
    pair<int, string> currentpair = currentPair;
 
    cout << "[ ";
 
    // Printing pair elements
    cout << "First: " << currentPair.first <<
            " and " << "Second: " <<
            currentPair.second;
    cout << ']';
    cout << '\n';
  }
}
 
// Driver code
int main()
{
  // Declaring a forward list of pairs
  forward_list<pair<int, string> >
  forwardListOfPairs;
 
  // Declaring a pair
  pair<int, string> pair1;
 
  // Initializing the
  // pair
  pair1 = make_pair(1, "Geeks");
 
  // Push the pair at the back
  // in the forwardList
  forwardListOfPairs.push_front(pair1);
 
  // Declaring another pair
  pair<int, string> pair2;
 
  // Initializing the
  // pair
  pair2 = make_pair(2, "for");
 
  // Push the pair at the front
  // in the forward list
  forwardListOfPairs.push_front(pair2);
 
  // Declaring another pair
  pair<int, string> pair3;
 
  // Initializing the pair
  pair3 = make_pair(3, "Geeks");
 
  // Push the pair at the front
  // in the forwardList
  forwardListOfPairs.push_front(pair3);
 
  // Declaring another pair
  pair<int, string> pair4;
 
  // Initializing the pair
  pair4 = make_pair(4, "C++");
 
  // Push the pair at the front
  // in the forwardList
  forwardListOfPairs.push_front(pair4);
 
  // Calling print function
  print(forwardListOfPairs);
  return 0;
}


 
 

Output

Forward List : 
[ First: 4 and Second: C++]
[ First: 3 and Second: Geeks]
[ First: 2 and Second: for]
[ First: 1 and Second: Geeks]

List of Pairs

 

Below is the implementation of a list of pairs:

 

Example 1:

 

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print list
// contents
void print(list<pair<int, int> >&
           listOfPairs)
{
 
  cout << "List : " << '\n';
   
  for (auto currentPair : listOfPairs)
  {
    // Each element of the list is
    // a pair itself
    pair<int, int> currentpair = currentPair;
 
    cout << "[ ";
 
    // Printing pair elements
    cout << "First: " << currentPair.first <<
            " and " << "Second: " <<
            currentPair.second;
    cout << ']';
    cout << '\n';
  }
}
 
// Driver code
int main()
{
  // Declaring a list of pairs
  list<pair<int, int> > listOfPairs;
 
  // Declaring a pair
  pair<int, int> pair1;
 
  // Initializing the
  // pair
  pair1 = make_pair(11, 22);
 
  // Push the pair at the back
  // in the list
  listOfPairs.push_back(pair1);
 
  // Declaring another pair
  pair<int, int> pair2;
 
  // Initializing the
  // pair
  pair2 = make_pair(33, 44);
 
  // Push the pair at the back
  // in the list
  listOfPairs.push_back(pair2);
 
  // Declaring another pair
  pair<int, int> pair3;
 
  // Initializing the pair
  pair3 = make_pair(55, 66);
 
  // Push the pair at the front
  // in the list
  listOfPairs.push_front(pair3);
 
  // Declaring another pair
  pair<int, int> pair4;
 
  // Initializing the pair
  pair4 = make_pair(77, 88);
 
  // Push the pair at the back
  // in the list
  listOfPairs.push_back(pair4);
 
  // Calling print function
  print(listOfPairs);
  return 0;
}


 
 

Output

List : 
[ First: 55 and Second: 66]
[ First: 11 and Second: 22]
[ First: 33 and Second: 44]
[ First: 77 and Second: 88]

 

Example 2:

 

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print list
// contents
void print(list<pair<int, string> >&
           listOfPairs)
{
 
  cout << "List : " << '\n';
  for (auto currentPair : listOfPairs)
  {
    // Each element of the forwardList is
    // a pair itself
    pair<int, string> currentpair = currentPair;
 
    cout << "[ ";
 
    // Printing pair contents
    cout << "First: " << currentPair.first <<
            " and " << "Second: " <<
            currentPair.second;
    cout << ']';
    cout << '\n';
  }
}
 
// Driver code
int main()
{
  // Declaring a list of pairs
  list<pair<int, string> > listOfPairs;
 
  // Declaring a pair
  pair<int, string> pair1;
 
  // Initializing the
  // pair
  pair1 = make_pair(1, "Geeks");
 
  // Push the pair at the back
  // in the list
  listOfPairs.push_front(pair1);
 
  // Declaring another pair
  pair<int, string> pair2;
 
  // Initializing the
  // pair
  pair2 = make_pair(2, "for");
 
  // Push the pair at the front
  // in the list
  listOfPairs.push_front(pair2);
 
  // Declaring another pair
  pair<int, string> pair3;
 
  // Initializing the pair
  pair3 = make_pair(3, "Geeks");
 
  // Push the pair at the front
  // in the list
  listOfPairs.push_front(pair3);
 
  // Declaring another pair
  pair<int, string> pair4;
 
  // Initializing the pair
  pair4 = make_pair(4, "C++");
 
  // Push the pair at the front
  // in the list
  listOfPairs.push_front(pair4);
 
  // Calling print function
  print(listOfPairs);
  return 0;
}


 
 

Output

List : 
[ First: 4 and Second: C++]
[ First: 3 and Second: Geeks]
[ First: 2 and Second: for]
[ First: 1 and Second: Geeks]

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!