按升序中的第一个元素和降序中的第二个元素对对的向量进行排序
对是存储两个相互映射的值的容器,包含多个此类对的向量称为对向量。
在解决问题时,有很多情况需要根据对的第一个和第二个元素对向量的元素进行排序。在这种情况下,我们必须将一个附加参数传递给 sort()函数i,即调用 sort()函数中用户定义的显式函数。
本文重点讨论pairs的排序向量,根据pairs的第一个元素升序排列,如果第一个元素相等则根据第二个元素降序排列。
下面是演示对向量排序的 C++ 程序。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to sort the vector elements
// ascending for first element
// and if first element equal
// then descending for second element
bool sortbyCond(const pair& a,
const pair& b)
{
if (a.first != b.first)
return (a.first < b.first);
else
a.second > b.second;
}
// Driver code
int main()
{
// Declaring vector of pairs
vector > vect;
// Initialising 1st and 2nd element
// of pairs with array values
int arr[] = { 10, 10, 5, 5, 15, 15 };
int arr1[] = { 40, 60, 20, 50, 12, 24 };
int n = sizeof(arr) / sizeof(arr[0]);
// Entering values in vector of pairs
for (int i = 0; i < n; i++)
vect.push_back(make_pair(arr[i],
arr1[i]));
// The original vector(before sort())
cout << "The vector before sort operation is:\n";
for (int i = 0; i < n; i++) {
// "first" and "second" are used to
// access 1st and 2nd element of pair
// respectively
cout << vect[i].first << " "
<< vect[i].second << endl;
}
// Using sort() function to sort by
// 1st element of pair and if first
// element equal then by descending
// order of second element
sort(vect.begin(), vect.end(), sortbyCond);
// Printing the sorted vector(after
// using sort())
cout << "The vector after sort operation is:\n";
for (int i = 0; i < n; i++) {
// "first" and "second" are used to
// access 1st and 2nd element of pair
// respectively
cout << vect[i].first << " "
<< vect[i].second << endl;
}
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
static class pair implements Comparable
{
int first,second;
pair(int s, int e)
{
first = s;
second = e;
}
// Function to sort the vector elements
// ascending for first element
// and if first element equal
// then descending for second element
public int compareTo(pair b)
{
if (this.first != b.first)
return (this.first < b.first)?-1:1;
else
return this.second > b.second?-1:1;
}
}
// Driver code
public static void main(String[] args)
{
// Declaring vector of pairs
List vect = new ArrayList ();
// Initialising 1st and 2nd element
// of pairs with array values
int arr[] = { 10, 10, 5, 5, 15, 15 };
int arr1[] = { 40, 60, 20, 50, 12, 24 };
int n = arr.length;
// Entering values in vector of pairs
for (int i = 0; i < n; i++)
vect.add(new pair(arr[i],
arr1[i]));
// The original vector(before sort())
System.out.print("The vector before sort operation is:\n");
for (int i = 0; i < n; i++)
{
// "first" and "second" are used to
// access 1st and 2nd element of pair
// respectively
System.out.print(vect.get(i).first+ " "
+ vect.get(i).second +"\n");
}
// Using sort() function to sort by
// 1st element of pair and if first
// element equal then by descending
// order of second element
Collections.sort(vect);
// Printing the sorted vector(after
// using sort())
System.out.print("The vector after sort operation is:\n");
for (int i = 0; i < n; i++)
{
// "first" and "second" are used to
// access 1st and 2nd element of pair
// respectively
System.out.print(vect.get(i).first+ " "
+ vect.get(i).second +"\n");
}
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to implement
# the above approach
# Function to sort the vector elements
# ascending for first element
# and if first element equal
# then descending for second element
from functools import cmp_to_key
def sortbyCond(a, b):
if (a[0] != b[0]):
return (a[0] - b[0])
else:
return b[1] - a[1]
# Driver code
# Declaring vector of pairs
vect = []
# Initialising 1st and 2nd element
# of pairs with array values
arr = [ 10, 10, 5, 5, 15, 15 ]
arr1 = [ 40, 60, 20, 50, 12, 24 ]
n = len(arr)
# Entering values in vector of pairs
for i in range(n):
vect.append([arr[i],arr1[i]])
# The original vector(before sort())
print("The vector before sort operation is: ")
for i in range(n):
# "first" and "second" are used to
# access 1st and 2nd element of pair
# respectively
print(f"{vect[i][0]} {vect[i][1]}")
# Using sort() function to sort by
# 1st element of pair and if first
# element equal then by descending
# order of second element
vect.sort(key = cmp_to_key(sortbyCond))
# Printing the sorted vector(after
# using sort())
print("The vector after sort operation is: ")
for i in range(n):
# "first" and "second" are used to
# access 1st and 2nd element of pair
# respectively
print(f"{vect[i][0]} {vect[i][1]}")
# This code is contributed by shinjanpatra
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
public class GFG{
class pair : IComparable
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int CompareTo(pair b)
{
if (this.first != b.first)
return (this.first < b.first)?-1:1;
else
return this.second > b.second?-1:1;
}
}
// Driver code
public static void Main(String[] args)
{
// Declaring vector of pairs
List vect = new List ();
// Initialising 1st and 2nd element
// of pairs with array values
int []arr = { 10, 10, 5, 5, 15, 15 };
int []arr1 = { 40, 60, 20, 50, 12, 24 };
int n = arr.Length;
// Entering values in vector of pairs
for (int i = 0; i < n; i++)
vect.Add(new pair(arr[i],
arr1[i]));
// The original vector(before sort())
Console.Write("The vector before sort operation is:\n");
for (int i = 0; i < n; i++)
{
// "first" and "second" are used to
// access 1st and 2nd element of pair
// respectively
Console.Write(vect[i].first+ " "
+ vect[i].second +"\n");
}
// Using sort() function to sort by
// 1st element of pair and if first
// element equal then by descending
// order of second element
vect.Sort();
// Printing the sorted vector(after
// using sort())
Console.Write("The vector after sort operation is:\n");
for (int i = 0; i < n; i++)
{
// "first" and "second" are used to
// access 1st and 2nd element of pair
// respectively
Console.Write(vect[i].first+ " "
+ vect[i].second +"\n");
}
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
The vector before sort operation is:
10 40
10 60
5 20
5 50
15 12
15 24
The vector after sort operation is:
5 50
5 20
10 60
10 40
15 24
15 12