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

什么是成对向量? A. 一对 存储两个相互映射的值的容器,以及 矢量 包含多个这样的对的向量称为对向量。

null

// C++ program to demonstrate vector of pairs
#include<bits/stdc++.h>
using namespace std;
int main()
{
//declaring vector of pairs
vector< pair < int , int > > vect;
// initialising 1st and 2nd element of
// pairs with array values
int arr[] = {10, 20, 5, 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 vector
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;
}


输出:

10 30
20 60
5 20
40 50

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

// C++ program to demonstrate sorting in
// vector of pair according to 1st element
// of pair
#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[] = {10, 20, 5, 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 simple sort() function to sort
sort(vect.begin(), vect.end());
// Printing the sorted vector(after using sort())
cout << "The vector after 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 applying sort operation is:
10 30
20 60
5 20
40 50
The vector after applying sort operation is:
5 20
10 30
20 60
40 50

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

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

C++集2中对的排序向量 (按第一和第二降序排序) 本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

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