📜  在 C++ 中对对的二维向量进行排序

📅  最后修改于: 2022-05-13 01:54:36.375000             🧑  作者: Mango

在 C++ 中对对的二维向量进行排序

2D 向量也称为向量的向量,是其中每个元素本身就是一个向量的向量。换句话说,它是一个借助向量实现的矩阵。

什么是二维向量对?

二维向量对是一个向量,其中每个元素本身就是一个对向量。换句话说,它是一个借助成对向量实现的矩阵。

示例:下面是实现二维对向量的 C++ 程序。

C++
// C++ program to demonstrate the
// working of vector of vectors
// of pairs
#include 
using namespace std;
  
// Function to print 2D vector elements
void print(vector > >& myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    vector > vect2 = 
    {{9, 10}, {11, 12}, {13, 14}, {15, 16}};
    vector > vect3 = 
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector > 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;
}


C++
// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include 
using namespace std;
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector > vect3 = 
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector > 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;
}


C++
// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(pair& pair1, 
                  pair& pair2)
{
    return pair1.second < pair2.second;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector > vect3 = 
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector > 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;
}


C++
// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1[1].first < vector2[1].first;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector > vect2 = 
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector > vect3 = 
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector > 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;
}


C++
// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1[1].second < vector2[1].second;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector > vect2 = 
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector > vect3 = 
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector > 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;
}


C++
// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(pair& pair1, 
                  pair& pair2)
{
    return pair1.first > pair2.first;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector > vect3 = 
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector > 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;
}


C++
// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(pair& pair1, 
           pair& pair2)
{
    return pair1.second > pair2.second;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector > vect3 = 
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector > 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;
}


C++
// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1[1].first > vector2[1].first;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector > vect2 = 
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector > vect3 = 
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector > 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;
}


C++
// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1[1].second > vector2[1].second;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector > vect2 = 
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector > vect3 = 
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector > 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;
}


C++
// 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 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1.size() < vector2.size();
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}, {16, 18}};
    vector > vect3 = 
    {{17, 18}, {19, 20}};
    vector > vect4 = 
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
    vector > 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;
}


C++
// 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 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1.size() > vector2.size();
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
  
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}, {16, 18}};
    vector > vect3 = 
    {{17, 18}, {19, 20}};
    vector > vect4 = 
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
    vector > 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;
}


输出

本文重点讨论用于对二维向量对进行排序的不同技术。

案例 1:对 2D 向量的特定行进行排序:

  • 基于对的第一个值:这种类型的排序按对的第一个值的升序排列二维向量的选定行。这是通过使用“sort()”并传递一维向量的迭代器作为其参数来实现的。

例子:

下面是上述方法的实现:

C++

// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include 
using namespace std;
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector > vect3 = 
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector > 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;
}
输出
  • 基于对的第二个值:这种类型的排序按对的第二个值的升序排列二维向量的选定行。这是通过使用“sort()”并传递一维向量的迭代器作为其参数来实现的。在这种情况下,需要将自定义比较器作为第三个参数传递,该参数实现基于对的第二个值进行排序的逻辑。

例子:

下面是上述方法的实现:

C++

// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(pair& pair1, 
                  pair& pair2)
{
    return pair1.second < pair2.second;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector > vect3 = 
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector > 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;
}
输出

案例 2:根据特定列对整个 2D 向量进行排序:

  • 基于对的第一个值:在这种类型的排序中,二维向量完全基于所选列进行排序。例如,如果选择的列是第二列,则第二列中对的第一个值最小的行成为第一行,第二列中对的第二个最小的第一个值成为第二行,依此类推。

例子:

下面是实现上述方法的 C++ 程序:

C++

// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1[1].first < vector2[1].first;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector > vect2 = 
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector > vect3 = 
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector > 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;
}
输出
  • 基于对的第二个值:在这种类型的排序中,二维向量完全基于所选列进行排序。例如,如果选择的列是第二列,则第二列的对中第二值最小的行成为第一行,第二列中对的第二小的第二值成为第二行,依此类推。

例如,根据第二列对的第二个值对整个二维向量进行排序将导致:

下面是实现上述方法的 C++ 程序:

C++

// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1[1].second < vector2[1].second;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector > vect2 = 
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector > vect3 = 
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector > 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;
}
输出

案例 3:按降序对二维向量对的特定行进行排序

  • 基于对的第一个值:这种类型的排序按对的第一个元素降序排列二维向量的选定行。这是通过使用“sort()”并传递一维向量的迭代器作为其参数来实现的。在这种情况下,需要将自定义比较器作为第三个参数传递,以实现基于对的第一个元素进行排序的逻辑。

例子:

下面是实现上述方法的 C++ 程序:

C++

// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(pair& pair1, 
                  pair& pair2)
{
    return pair1.first > pair2.first;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector > vect3 = 
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector > 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;
}
输出
  • 基于对的第二个值:这种类型的排序按对的第二个元素按降序排列二维向量的选定行。这是通过使用“sort()”并传递一维向量的迭代器作为其参数来实现的。在这种情况下,需要将自定义比较器作为第三个参数传递,该参数实现基于对的第二个元素按降序排序的逻辑。

例子:

下面是实现上述方法的 C++ 程序:

C++

// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(pair& pair1, 
           pair& pair2)
{
    return pair1.second > pair2.second;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector > vect3 = 
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector > 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;
}
输出

案例 4:根据特定列按降序对整个 2D 向量进行排序:

  • 基于pairs的第一个值:在这种类型的排序中,2D向量完全根据所选列的pairs的第一个值降序排序。例如,如果选择的列是第二列,则第二列中对的第一个元素的最大值的行成为第一行,第二列中对的第二个元素的第二大值成为第二行,等等。
    在这种情况下,需要将自定义比较器作为第三个参数传递,该参数实现基于对的第一个元素按降序排序的逻辑。

例子:

下面是实现上述方法的 C++ 程序:

C++

// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1[1].first > vector2[1].first;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector > vect2 = 
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector > vect3 = 
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector > 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;
}
输出
  • 基于pairs的第二个值:在这种类型的排序中,2D向量完全基于所选列的pairs的第二个元素以降序排列。例如,如果选择的列是第二列,则第二列中对的第二个元素的最大值的行成为第一行,第二列中对的第二个元素的第二大值成为第二行,等等。
    在这种情况下,需要将自定义比较器作为第三个参数传递,该参数实现基于对的第二个元素按降序排序的逻辑。

例子:

下面是实现上述方法的 C++ 程序:

C++

// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1[1].second > vector2[1].second;
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector > vect2 = 
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector > vect3 = 
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector > 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;
}
输出

案例 5:根据一行中的列数以升序对 2D 向量对进行排序:

在这种类型的排序中,成对的二维向量根据列的数量按升序排序。这是通过将第三个参数(自定义比较器函数)传递给“sort()”方法来实现的。

例子:

下面是实现上述方法的 C++ 程序:

C++

// 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 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1.size() < vector2.size();
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}, {16, 18}};
    vector > vect3 = 
    {{17, 18}, {19, 20}};
    vector > vect4 = 
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
    vector > 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;
}
输出

案例 6:根据行中的列数以降序对 2D 向量对进行排序:

在这种类型的排序中,二维向量根据列数按降序排序。这是通过将第三个参数(自定义比较器)传递给“sort()”方法来实现的。

例子:

下面是实现上述方法的 C++ 程序:

C++

// 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 
using namespace std;
  
// Custom comparator function
bool myComparator(vector >& vector1, 
                  vector >& vector2)
{
    return vector1.size() > vector2.size();
}
  
// Function to print 2D vector elements
void print(vector > >
           & myContainer)
{
  
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) 
    {
        // Each element of the 2D vector is
        // a vector itself
        vector > myVector = 
                                currentVector;
  
        // Iterating over the 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 > > myContainer;
  
    // Initializing vectors of pairs
    vector > vect1 = 
    {{2, 1}, {4, 3}, {8, 1}};
    vector > vect2 = 
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}, {16, 18}};
    vector > vect3 = 
    {{17, 18}, {19, 20}};
    vector > vect4 = 
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
    vector > 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;
}
输出