Open In App

Sorting 2D Vector of Pairs in C++

A 2D vector also known as vector of vectors is a vector in which each element is a vector on its own. In other words, It is a matrix implemented with the help of vectors.

What is a 2D vector of pairs?



A 2D vector of pairs is a vector in which each element is a vector of pairs on its own. In other words, It is a matrix implemented with the help of vectors of pairs.

Example: Below is the C++ program to implement a 2D vector of pairs.






// C++ program to demonstrate the
// working of vector of vectors
// of pairs
#include <bits/stdc++.h>
using namespace std;
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >& myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
         {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    vector<pair<int, int> > vect2 =
    {{9, 10}, {11, 12}, {13, 14}, {15, 16}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    print(myContainer);
 
    return 0;
}

Output[ {1, 2} {3, 4} {5, 6} {7, 8} ] [ {9, 10} {11, 12} {13, 14} {15, 16} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ]

This article focuses on discussing different techniques used to sort a 2D vector of pairs.

Case 1: To sort a particular row of a 2D vector:

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                     

Output:
(1, 9)   (4, 1)   (7, 2)  
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)

Below is the implementation of the above approach:




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include <bits/stdc++.h>
using namespace std;
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements
    // before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting the first row of 2D vector
    // of pairs on the basis of first
    // element of pairs
    sort(myContainer[0].begin(),
         myContainer[0].end());
 
    cout << "\n\n After sorting the first row " <<
            "on the basis of first element of pairs, ";
    print(myContainer);
 
    return 0;
}

OutputBefore sorting, myContainer elements: [ {2, 1} {1, 9} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] After sorting the first row on the basis of first element of pairs, myContainer elements: [ {1, 9} {2, 1} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ]

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                     

Output:
(4, 1)   (7, 2)   (1, 9)  
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)

 Below is the implementation of the above approach:




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(pair<int, int>& pair1,
                  pair<int, int>& pair2)
{
    return pair1.second < pair2.second;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting first row of 2D vector of pairs
    // on the basis of second element of pairs
    // By passing a custom comparator as a
    // third argument
    sort(myContainer[0].begin(),
         myContainer[0].end(), myComparator);
 
    cout << "\n\n After sorting the first row on " <<
            "the basis of second element of pairs, ";
    print(myContainer);
 
    return 0;
}

OutputBefore sorting, myContainer elements: [ {2, 1} {1, 9} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] After sorting the first row on the basis of second element of pairs, myContainer elements: [ {2, 1} {8, 1} {4, 3} {1, 9} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ]

Case 2: To sort the entire 2D vector on basis of a particular column:

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                     

Output:
(4, 1)   (1, 9)   (7, 2)  
(1, 6)   (3, 2)   (1, 4)    
(3, 2)   (4, 5)   (8, 1)  

Below is the C++ program to implement the above approach:




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1[1].first < vector2[1].first;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector<pair<int, int> > vect2 =
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector<pair<int, int> > vect3 =
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector<pair<int, int> > vect4 =
    {{5, 2}, {1, 52}, {9, 3}, {3, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements
    // before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting 2D vector of pairs on the basis
    // of first element of pairs of the second
    // column by passing a custom comparator
    // as a third argument
    sort(myContainer.begin(), myContainer.end(),
         myComparator);
 
    cout << "\n\n After sorting the 2D vector on the " <<
            "basis of first element of pairs of the second column, ";
    print(myContainer);
 
    return 0;
}

OutputBefore sorting, myContainer elements: [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ] After sorting the 2D vector on the basis of first element of pairs of the second column, myContainer elements: [ {5, 2} {1, 52} {9, 3} {3, 32} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {3, 5} {10, 3} {2, 1} {3, 8} ]

For example, sorting the entire 2D vector on the basis of the second value of pairs of the second column would result into,

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                      

Output:  
(1, 6)   (3, 2)   (1, 4) 
(3, 2)   (4, 5)   (8, 1)  
(4, 1)   (1, 9)   (7, 2) 

Below is the C++ program to implement the above approach:




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1[1].second < vector2[1].second;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector<pair<int, int> > vect2 =
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector<pair<int, int> > vect3 =
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector<pair<int, int> > vect4 =
    {{5, 2}, {1, 52}, {9, 3}, {3, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting 2D vector of pairs on the basis of
    // first element of pairs of the second column
    // By passing a custom comparator as a third argument
    sort(myContainer.begin(), myContainer.end(),
         myComparator);
 
    cout << "\n\n After sorting the 2D vector on " <<
            "the basis of first element of pairs of " <<
            "the second column, ";
    print(myContainer);
 
    return 0;
}

OutputBefore sorting, myContainer elements: [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ] After sorting the 2D vector on the basis of first element of pairs of the second column, myContainer elements: [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ]

Case 3: To sort a particular row of a 2D vector of pairs in descending order

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                     

Output:
(7, 2)   (4, 1)   (1, 9)  
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)

Below is the C++ program to implement the above approach:




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(pair<int, int>& pair1,
                  pair<int, int>& pair2)
{
    return pair1.first > pair2.first;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements
    // before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting the first row of 2D vector
    // of pairs in descending order
    // of the first element of pairs
    sort(myContainer[0].begin(),
         myContainer[0].end(), myComparator);
 
    cout << "\n\n After sorting the first row " <<
            "in descending order of the first " <<
            "element of pairs, ";
    print(myContainer);
 
    return 0;
}

OutputBefore sorting, myContainer elements: [ {2, 1} {1, 9} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] After sorting the first row in descending order of the first element of pairs, myContainer elements: [ {8, 1} {4, 3} {2, 1} {1, 9} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ]

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)              
(1, 6)   (3, 2)   (1, 4)                     

Output:
(1, 9)   (7, 2)   (4, 1)  
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)

Below is the C++ program to implement the above approach:




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(pair<int, int>& pair1,
           pair<int, int>& pair2)
{
    return pair1.second > pair2.second;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting first row of 2D vector of pairs
    // in descending order of second element of pairs
    // By passing a custom comparator as a third argument
    sort(myContainer[0].begin(),
         myContainer[0].end(), myComparator);
 
    cout << "\n\n After sorting the first row " <<
            "in descending order of second element " <<
            "of pairs, ";
    print(myContainer);
 
    return 0;
}

OutputBefore sorting, myContainer elements: [ {2, 1} {1, 9} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] After sorting the first row in descending order of second element of pairs, myContainer elements: [ {1, 9} {4, 3} {2, 1} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ]

Case 4: To sort the entire 2D vector on basis of a particular column in descending order:

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                      

Output:
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)  
(4, 1)   (1, 9)   (7, 2)

Below is the C++ program to implement the above approach:




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1[1].first > vector2[1].first;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector<pair<int, int> > vect2 =
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector<pair<int, int> > vect3 =
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector<pair<int, int> > vect4 =
    {{5, 2}, {1, 52}, {9, 3}, {3, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting 2D vector of pairs in descending
    // order of the first element of pairs of
    // the second column by passing a custom
    // comparator as a third argument
    sort(myContainer.begin(),
         myContainer.end(), myComparator);
 
    cout << "\n\n After sorting the 2D vector in " <<
            "descending order of the first element " <<
            "of pairs of the second column, ";
    print(myContainer);
 
    return 0;
}

OutputBefore sorting, myContainer elements: [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ] After sorting the 2D vector in descending order of the first element of pairs of the second column, myContainer elements: [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ]

Example:

Input:
(4, 1)   (1, 9)   (7, 2)                      
(3, 2)   (4, 2)   (8, 1)      
(1, 6)   (3, 5)   (1, 4)                     

Output:
(4, 1)   (1, 9)   (7, 2)        
(1, 6)   (3, 5)   (1, 4)   
(3, 2)   (4, 2)   (8, 1) 

Below is the C++ program to implement the above approach:




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1[1].second > vector2[1].second;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector<pair<int, int> > vect2 =
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector<pair<int, int> > vect3 =
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector<pair<int, int> > vect4 =
    {{5, 2}, {1, 52}, {9, 3}, {3, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting 2D vector of pairs in descending
    // order of second element of pairs of the
    // second column by passing a custom comparator
    // as a third argument
    sort(myContainer.begin(),
         myContainer.end(), myComparator);
 
    cout << "\n\n After sorting the 2D vector " <<
            "in descending order of second element " <<
            "of pairs of the second column, ";
    print(myContainer);
 
    return 0;
}

OutputBefore sorting, myContainer elements: [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ] After sorting the 2D vector in descending order of second element of pairs of the second column, myContainer elements: [ {5, 2} {1, 52} {9, 3} {3, 32} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ]

Case 5: Sorting a 2D Vector of pairs on the basis of the number of columns in a row in ascending order:

In this type of sorting, a 2D vector of pairs is sorted on basis of a number of the column in ascending order. This is achieved by passing a third argument, a custom comparator function to the “sort()” method.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(8, 11)
(1, 6)   (3, 2)                     

Output:
(8, 11) 
(1, 6)   (3, 2)
(4, 1)   (1, 9)  (7, 2)

Below is the C++ program to implement the above approach:




// C++ program to demonstrate the
// working of sorting a 2D vector or
// vector of vectors of pairs in
// descending order of number of
// columns in a row
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1.size() < vector2.size();
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}, {16, 18}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
    vector<pair<int, int> > vect5 =
    {{7, 2}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
    myContainer.push_back(vect5);
 
    // Print the vector elements
    // before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting the 2D vector of pairs
    // in ascending order
    // of number of columns in a row
    sort(myContainer.begin(),
         myContainer.end(), myComparator);
 
    cout << "\n\n After sorting the 2D vector " <<
            "in ascending order of the number of " <<
            "columns in a row, ";
    print(myContainer);
 
    return 0;
}

OutputBefore sorting, myContainer elements: [ {2, 1} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} {16, 18} ] [ {17, 18} {19, 20} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] [ {7, 2} ] After sorting the 2D vector in ascending order of the number of columns in a row, myContainer elements: [ {7, 2} ] [ {17, 18} {19, 20} ] [ {2, 1} {4, 3} {8, 1} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] [ {19, 10} {11, 2} {12, 14} {14, 6} {16, 18} ]

Case 6: Sorting a 2D Vector of pairs on basis of the number of columns in a row in descending order:

In this type of sorting, a 2D vector is sorted on basis of the number of columns in descending order. This is achieved by passing a third argument, a custom comparator to the “sort()” method.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(8, 11)
(1, 6)   (3, 2)                               

Output:
(4, 1)   (1, 9)  (7, 2)
(1, 6)   (3, 2)
(8, 11)  

Below is the C++ program to implement the above approach:




// C++ program to demonstrate the
// working of sorting a 2D vector or
// vector of vectors of pairs in
// descending order of number of
// columns in a row
 
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1.size() > vector2.size();
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
 
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}, {16, 18}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
    vector<pair<int, int> > vect5 = {{7, 2}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
    myContainer.push_back(vect5);
 
    // Print the vector elements
    // before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting the first row of 2D
    // vector of pairs in descending
    // order of the first element of pairs
    sort(myContainer.begin(),
         myContainer.end(), myComparator);
 
    cout << "\n\n After sorting the 2D vector " <<
            "in descending order of the number " <<
            "of columns in a row, ";
    print(myContainer);
 
    return 0;
}

OutputBefore sorting, myContainer elements: [ {2, 1} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} {16, 18} ] [ {17, 18} {19, 20} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] [ {7, 2} ] After sorting the 2D vector in descending order of the number of columns in a row, myContainer elements: [ {19, 10} {11, 2} {12, 14} {14, 6} {16, 18} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] [ {2, 1} {4, 3} {8, 1} ] [ {17, 18} {19, 20} ] [ {7, 2} ]

Time complexity: O(N * M * log(M)), where N is the number of rows in the 2D vector and M is the maximum number of columns in any row. 

Auxiliary space: O(N*M).                              


Article Tags :