在C++集2中对的排序向量(按第一和第二顺序排序)

我们在下面的集合1中讨论了对向量排序的一些情况。 在C++集1中对的排序向量(按第一和第二排序) 本文将讨论更多的案例 有时我们需要按相反的顺序对向量排序。在这些情况下,与其先对向量排序,然后使用“反向”函数,不如增加代码的时间复杂度。因此,为了避免这种情况,我们直接按降序对向量进行排序。 案例3:根据成对的第一个元素按降序排序向量元素。 这种类型的排序按降序排列向量中选定的成对行。这是通过使用“sort()”并将一维向量的迭代器作为其参数来实现的。

null

CPP

// C++ program to demonstrate sorting in vector of
// pair according to 1st element of pair in
// descending order
#include<bits/stdc++.h>
using namespace std;
int main()
{
// declaring vector of pairs
vector< pair < int , int > > vect;
// initializing 1st and 2nd element of
// pairs with array values
int arr[] = {5, 20, 10, 40 };
int arr1[] = {30, 60, 20, 50};
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]) );
// Printing the original vector(before sort())
cout << "The vector before applying sort is:" ;
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 modified sort() function to sort
sort(vect.rbegin(), vect.rend());
// Printing the sorted vector(after using sort())
cout << "The vector after applying sort is:" ;
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;
}


输出:

The vector before applying sort is:5 3020 6010 2040 50The vector after applying sort is:40 5020 6010 205 30

案例4:根据成对的第二个元素按降序排序向量元素。 也可以通过修改“sort()”函数并再次向用户定义的函数传递调用来处理这些实例。

CPP

// C++ program to demonstrate sorting/in vector of
// pair according to 2nd element of pair in
// descending order
#include<bits/stdc++.h>
using namespace std;
// Driver function to sort the vector elements by
// second element of pair in descending order
bool sortbysecdesc( const pair< int , int > &a,
const pair< int , int > &b)
{
return a.second>b.second;
}
int main()
{
// Declaring vector of pairs
vector< pair < int , int > > vect;
// Initializing 1st and 2nd element of
// pairs with array values
int arr[] = {5, 20, 10, 40 };
int arr1[] = {30, 60, 20, 50};
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]) );
// Printing the original vector(before sort())
cout << "The vector before sort operation is:" ;
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 modified sort() function to sort
sort(vect.begin(), vect.end(), sortbysecdesc);
// Printing the sorted vector(after using sort())
cout << "The vector after applying sort operation is:" ;
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;
}


输出:

The vector before sort operation is:5 3020 6010 2040 50The vector after applying sort operation is:20 6040 505 3010 20

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享