📜  C++中元组向量的排序(升序)

📅  最后修改于: 2021-05-04 09:51:21             🧑  作者: Mango

什么是元组向量?
元组是可以容纳多个元素的对象,包含多个此类元组的向量称为元组向量。元素可以是不同的数据类型。元组的元素被初始化为参数,以便可以访问它们。

// C++ program to demonstrate vector of tuple
#include 
using namespace std;
int main()
{
    vector > v;
    v.push_back(make_tuple(10, 20, 30));
    v.push_back(make_tuple(15, 5, 25));
    v.push_back(make_tuple(3, 2, 1));
  
    // Printing vector tuples
    for (int i = 0; i < v.size(); i++) 
        cout << get<0>(v[i]) << " " 
             << get<1>(v[i]) << " " 
             << get<2>(v[i]) << "\n";
      
    return 0;
}
输出:
10 20 30
15 5 25
3 2 1

不同的元组向量排序方式
情况1:根据元组第一个元素以升序对向量元素进行排序。
这种类型的排序可以使用简单的“ sort() ”函数来实现。默认情况下,sort函数根据元组的第一个元素对向量元素进行排序。

// C++ program to demonstrate sorting in
// vector of tuple according to 1st element
// of tuple
#include 
using namespace std;
int main()
{
    vector > v;
    v.push_back(make_tuple(10, 20, 30));
    v.push_back(make_tuple(15, 5, 25));
    v.push_back(make_tuple(3, 2, 1));
  
    // Using sort() function to sort by 1st 
    // element of tuple
    sort(v.begin(), v.end());
    cout << "Sorted Vector of Tuple on basis"
           " of first element of tuple:\n";
    for (int i = 0; i < v.size(); i++) 
        cout << get<0>(v[i]) << " " 
             << get<1>(v[i]) << " "
             << get<2>(v[i]) << "\n";
      
    return 0;
}
输出:
Sorted Vector of Tuple on basis of first element of tuple:
3 2 1
10 20 30
15 5 25

情况2:根据元组第二个元素以升序对向量元素进行排序。
在某些情况下,我们需要根据元组的第二个元素对向量的元素进行排序。为此,我们修改了sort()函数,并传递了第三个参数,即对sort()函数用户定义的显式函数的调用。

// C++ program to demonstrate sorting in vector
// of tuple according to 2nd element of tuples
#include 
using namespace std;
  
// Comparison function to sort the vector elements
// by second element of tuples
bool sortbysec(const tuple& a, 
               const tuple& b)
{
    return (get<1>(a) < get<1>(b));
}
  
int main()
{
    vector > v;
    v.push_back(make_tuple(10, 20, 30));
    v.push_back(make_tuple(15, 5, 25));
    v.push_back(make_tuple(3, 2, 1));
  
    // Using sort() function to sort by 2nd element
    // of tuple
    sort(v.begin(), v.end(), sortbysec);
    cout << "Sorted Vector of Tuple on basis"
           " of Second element of tuple:\n";
  
    for (int i = 0; i < v.size(); i++) 
        cout << get<0>(v[i]) << " " 
             << get<1>(v[i]) << " " 
             << get<2>(v[i]) << "\n";
    return 0;
}
输出:
Sorted Vector of Tuple on basis of Second element of tuple:
3 2 1
15 5 25
10 20 30

情况3:根据元组第三个元素以升序对向量元素进行排序。
在某些情况下,我们需要根据元组的第三个元素对向量的元素进行排序。为此,我们修改了sort()函数,并传递了第三个参数,即对sort()函数用户定义的显式函数的调用。

// C++ program to demonstrate sorting in vector
// of tuple according to 3rd element of tuple
#include 
using namespace std;
// Driver function to sort the vector elements
// by third element of tuple
bool sortbyth(const tuple& a, 
              const tuple& b)
{
    return (get<2>(a) < get<2>(b));
}
  
int main()
{
    vector > v;
    v.push_back(make_tuple(10, 20, 30));
    v.push_back(make_tuple(15, 5, 25));
    v.push_back(make_tuple(3, 2, 1));
  
    // Using sort() function to sort by 3rd element
    // of tuple
    sort(v.begin(), v.end(), sortbyth);
    cout << "Sorted Vector of Tuple on basis"
            " of Third element of tuple:\n";
    for (int i = 0; i < v.size(); i++) 
        cout << get<0>(v[i]) << " " 
             << get<1>(v[i]) << " " 
             << get<2>(v[i]) << "\n";
      
    return 0;
}
输出:
Sorted Vector of Tuple on basis of Third element of tuple:
3 2 1
15 5 25
10 20 30
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”