The Wayback Machine - https://web.archive.org/web/20240930175201/https://www.geeksforgeeks.org/stack-of-pair-in-c-stl-with-examples/
Open In App

Stack of Pair in C++ STL with Examples

Last Updated : 25 Mar, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Stack in STL Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.

Pair in STL The pair container is a simple container defined in header consisting of two data elements or objects. The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second).

Stack of pair in STL: Stack of pair can be very efficient in designing complex data structures.

Syntax:

stack<pair<datatype, datatype>> stack_of_pair;

Below is an example to show the Stack of Pairs:




// CPP program to demonstrate
// the working of STL stack of pairs
  
#include <bits/stdc++.h>
using namespace std;
  
// Print the current pair
void printPair(pair<int, int> p)
{
  
    cout << "("
         << p.first << ", "
         << p.second << ") ";
}
  
// Print the Stack of Pairs
void Showstack(stack<pair<int, int> > s)
{
    while (!s.empty()) {
        printPair(s.top());
        s.pop();
    }
  
    cout << '\n';
}
  
// Driver code
int main()
{
    stack<pair<int, int> > s;
  
    s.push({ 10, 20 });
    s.push({ 15, 5 });
    s.push({ 1, 5 });
    s.push({ 5, 10 });
    s.push({ 7, 9 });
  
    cout << "Stack of Pairs: ";
    Showstack(s);
  
    cout << "\nSize of Stack of Pairs: "
         << s.size();
    cout << "\nTop of Stack of Pairs: ";
    printPair(s.top());
  
    cout << "\n\nRemoving the top pair\n";
    s.pop();
  
    cout << "Current Stack of Pairs: ";
    Showstack(s);
  
    return 0;
}


Output:

Stack of Pairs: (7, 9) (5, 10) (1, 5) (15, 5) (10, 20) 

Size of Stack of Pairs: 5
Top of Stack of Pairs: (7, 9) 

Removing the top pair
Current Stack of Pairs: (5, 10) (1, 5) (15, 5) (10, 20)

Below are the images to show the working of Stack of Pairs:



Similar Reads

stack empty() and stack size() in C++ STL
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only. stack::empty()empty() function is used to check if the stack container is empty or not. Syntax : stackname.empty()Parameters :No parameters are passed.Returns :True, if sta
3 min read
stack::push() and stack::pop() in C++ STL
The stack::push() and stack::pop() method in stack container is used to insert and delete the element from the top of stack. They are the member functions of std::stack container defined inside <stack> header file. In this article, we will learn how to use stack::push() and stack::pop() methods in C++. stack::push()The stack::push() function
2 min read
stack top() in C++ STL
Stacks are a type of container adaptors with LIFO(Last In First Out) type of work, where a new element is added at one end called the top of the stack, and an element is removed from the same end only. stack::top() top() function is used to reference the top(or the newest) element of the stack. Syntax : stackname.top() Parameters: No value is neede
2 min read
stack emplace() in C++ STL
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. stack::emplace() This function is used to insert a new element into the stack container, the new element is added on top of the stack. Syntax : stackname.emplace(value) Par
3 min read
How to implement a Stack using list in C++ STL
In this article, we will discuss how to implement a Stack using list in C++ STL. Stack is a linear data structure which follows. LIFO(Last In First Out) or FILO(First In Last Out). It mainly supports 4 major operations:1. Push: Push an element into the stack.2. Pop: Removes the element by following the LIFO order.3. Top: Returns the element present
3 min read
Stack-buffer based STL allocator
It is absolutely possible to develop a stack allocator that completely complies with C++11 and C++14. However, you must take into account some of the implications of stack allocation's implementation, semantics, and interactions with common containers. Here is a fully compliant stack allocator for C++11 and C++14:- Stack buffer: When information wr
4 min read
Stack in C++ STL
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. Stack uses an encapsulated object of either vector or deque (by default) or list (sequential container class) as its underlying container, providing a specific set of membe
5 min read
stack swap() in C++ STL
Stacks are a type of container adaptors with LIFO(Last In First Out) type of work, where a new element is added at one end and (top) an element is removed from that end only. stack::swap()This function is used to swap the contents of one stack with another stack of same type but the size may vary. Syntax : stackname1.swap(stackname2)Parameters: The
2 min read
Difference between pair in Multiset and Multimap in C++ STL
Pairs in C++: The pair container is a simple container defined in <utility> header consisting of two data elements or objects. The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second). Pair is used to combine together two values which may be different in type. Pair provides a way to
5 min read
Pair in C++ Standard Template Library (STL)
Pair is used to combine together two values that may be of different data types. Pair provides a way to store two heterogeneous objects as a single unit. It is basically used if we want to store tuples. The pair container is a simple container defined in <utility> header consisting of two data elements or objects. The first element is referen
9 min read
Sorting an array according to another array using pair in STL
We are given two arrays. We need to sort one array according to another. Examples: Input : 2 1 5 4 9 3 6 7 10 8 A B C D E F G H I JOutput : 1 2 3 4 5 6 7 8 9 10 B A F D C G H J E I Here we are sorting second array(a character array) according to the first array (an integer array).We have discussed different ways in below post. Sort an array accordi
6 min read
Find maximum in stack in O(1) without using additional stack
The task is to design a stack which can get the maximum value in the stack in O(1) time without using an additional stack. Examples: Input: push(2) findMax() push(6) findMax() pop() findMax() Output: 2 inserted in stack Maximum value in the stack: 2 6 inserted in stack Maximum value in the stack: 6 Element popped Maximum value in the stack: 2 Appro
11 min read
Stack Permutations (Check if an array is stack permutation of other)
A stack permutation is a permutation of objects in the given input queue which is done by transferring elements from the input queue to the output queue with the help of a stack and the built-in push and pop functions. The rules are: Only dequeue from the input queue.Use inbuilt push, and pop functions in the single stack.Stack and input queue must
15+ min read
Reversing a Stack with the help of another empty Stack
Given a Stack consisting of N elements, the task is to reverse the Stack using an extra stack. Examples: Input: stack = {1, 2, 3, 4, 5} Output: 1 2 3 4 5 Explanation: Input Stack: 5 4 3 2 1 Reversed Stack: 1 2 3 4 5 Input: stack = {1, 3, 5, 4, 2} Output: 1 3 5 4 2 Approach 1: Follow the steps below to solve the problem: Initialize an empty stack.Po
8 min read
Sort a stack using a temporary stack
Given a stack of integers, sort it in ascending order using another temporary stack. Examples: Input : [34, 3, 31, 98, 92, 23] Output : [3, 23, 31, 34, 92, 98] Input : [3, 5, 1, 4, 2, 8] Output : [1, 2, 3, 4, 5, 8] Recommended PracticeSort a stackTry It! Algorithm: Create a temporary stack say tmpStack.While input stack is NOT empty do this: Pop an
6 min read
How to Create a Stack of Stack in C++?
In C++, the stack is a container that follows the LIFO (Last In, First Out) order in which the elements are inserted and removed from it. In this article, we will learn how to create a stack of a stack in C++. Example:Input:Elements in stack1= 1, 2, 3, 4Elements in stack2= 5, 6, 7Output:Elements in the Stack of Stacks:7 6 5 4 3 2 1 Stack of Stacks
2 min read
Find maximum in stack in O(1) without using additional stack in Python
The task is to design a stack which can get the maximum value in the stack in O(1) time without using an additional stack in Python. Examples: Input: Consider the following SpecialStack 16 –> TOP29151918When getMax() is called it should return 29, which is the maximum element in the current stack. If we do pop two times on stack, the stack becom
3 min read
Pair formation such that maximum pair sum is minimized
Given an array of size 2 * N integers. Divide the array into N pairs, such that the maximum pair sum is minimized. In other words, the optimal division of array into N pairs should result into a maximum pair sum which is minimum of other maximum pair sum of all possibilities. Examples: Input : N = 2 , arr[] = { 5, 8, 3, 9 } Output : (3, 9) (5, 8) E
5 min read
Probability of a random pair being the maximum weighted pair
Given two arrays A and B, a random pair is picked having an element from array A and another from array B. Output the probability of the pair being maximum weighted. Examples: Input : A[] = 1 2 3 B[] = 1 3 3 Output : 0.222 Explanation : Possible pairs are : {1, 1}, {1, 3}, {1, 3}, {2, 1}, {2, 3}, {2, 3}, {3, 1}, {3, 3}, {3, 3} i.e. 9. The pair with
7 min read
Queries to check if any pair exists in an array having values at most equal to the given pair
Given a vector of pairs arr[] and Q queries in the form of pairs in an array Queries[], the task for each query is to check if there exists any pair with both the values smaller than those in the pair of the current query. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: arr[][] = {{3, 5}, {2, 7}, {2, 3}, {4, 9}}, Quer
12 min read
Count of quadruples with product of a pair equal to the product of the remaining pair
Given an array arr[] of size N, the task is to count the number of unique quadruples (a, b, c, d) from the array such that the product of any pair of elements of the quadruple is equal to the product of the remaining pair of elements. Examples: Input: arr[] = {2, 3, 4, 6}Output: 8Explanation:There are 8 quadruples in the array, i.e. (2, 6, 3, 4), (
7 min read
Pair the integers of Array so that each pair sum is consecutive and distinct
Given an array arr[]of size N. We have to pair the integers such that each integer should be in exactly one pair and each sum of pairs is consecutive and distinct. Formally, sum(arri + arrj) + 1 = sum(arri1 + arrj1). Print the pairs of integers satisfying all the conditions. If it is not possible to find the pairs return -1. Examples: Input: N = 6,
7 min read
Reduce Binary Array by replacing both 0s or both 1s pair with 0 and 10 or 01 pair with 1
Given a binary array arr[] of size N, the task is to find the last number remaining in the array after performing a set of operations. In each operation, select any two numbers and perform the following: If both numbers are the same, remove them from the array and insert a 0.If both numbers are different, remove both of them and insert a 1.Example:
9 min read
Number of indices pair such that element pair sum from first Array is greater than second Array
Given two integer arrays A[] and B[] of equal sizes, the task is to find the number of pairs of indices {i, j} in the arrays such that A[i] + A[j] > B[i] + B[j] and i < j.Examples: Input: A[] = {4, 8, 2, 6, 2}, B[] = {4, 5, 4, 1, 3} Output: 7 Explanation: There are a total of 7 pairs of indices {i, j} in the array following the condition. The
15+ min read
std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++
There can be some instances where you need to return multiple values (maybe of different data types ) while solving a problem. One method to do the same is by using pointers, structures or global variables, already discussed here There is another interesting method to do the same without using the above methods,  using tuples (for returning multipl
2 min read
Kth smallest pair and number of pairs less than a given pair (x, y)
Given N pairs, the task is to find the Kth smallest pair and the number of pairs less than the given pair (x, y). Examples: Input: pairs[][] = {{23, 20}, {23, 10}, {23, 30}, {12, 35}, {12, 22}}, K = 3, (x, y) = (23, 20) Output: {23, 10} 3 Input: pairs[][] = {{23, 20}, {23, 10}, {23, 30}, {12, 35}, {12, 22}}, K = 2, (x, y) = (12, 35) Output: {12, 35
10 min read
multiset lower_bound() in C++ STL with Examples
The multiset::lower_bound() is a built-in function in C++ STL which returns an iterator pointing to the first element in the container which is equivalent to k passed in the parameter. In case k is not present in the set container, the function returns an iterator pointing to the immediate next element which is just greater than k. If the key passe
3 min read
multiset upper_bound() in C++ STL with Examples
The multiset::upper_bound() is a built-in function in C++ STL that returns an iterator pointing to the immediate next element which is just greater than k. If the key passed in the parameter exceeds the maximum key in the container, then the iterator returned points an element which points to the position after the last element in the container. Sy
3 min read
multiset max_size() in C++ STL with Examples
The multiset::max_size() is a built-in function in C++ STL which returns the maximum number of elements a multiset container can hold. Syntax: multiset_name.max_size() Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of elements a multiset container can hold. Below programs illustrate th
1 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 elements the container can hold. Below programs illustrat
1 min read
Practice Tags :