C++ 如何按降序和第二个元素对向量进行排序?
我正在进行一项编程挑战,在提出以下问题之前,我已经看过了这些主题: 情况是这样的: -我有我的成对向量:C++ 如何按降序和第二个元素对向量进行排序?,c++,sorting,std-pair,C++,Sorting,Std Pair,我正在进行一项编程挑战,在提出以下问题之前,我已经看过了这些主题: 情况是这样的: -我有我的成对向量:vectorrank -我已经实现了一个谓词来比较和排序向量对第二个元素和降序: struct predicate { bool operator()(const std::pair<int, int> &left, const std::pair<int, int> &right) { return left.s
vectorrank代码>
-我已经实现了一个谓词来比较和排序向量对第二个元素和降序:
struct predicate
{
bool operator()(const std::pair<int, int> &left, const std::pair<int, int> &right)
{
return left.second < right.second;
}
}
sort(rank.rbegin(), rank.rend(), predicate());
struct谓词
{
布尔运算符()(常数std::pair&left,常数std::pair&right)
{
返回left.second
编程挑战将为第二个元素提供重复的值,在这种情况下,我必须在第一个元素插入成对向量时对其进行排序,例如:
K V
1 3
2 4
4 5
33 3
K V
1 3
2 4
4 5
33 3
排序结果必须是:
4 5
2 4
1 3
33 3
4 5
2 4
1 3
33 3
当我使用我设计的测试用例测试我的解决方案时,问题就出现了:
K V
1 2
16 3
11 2
20 3
18 2
39 39
23 22
12 19
123 4
145 6
3 5
26 4
9574 4
7 1
135 5
193 99
18237 3
22 4
1293 3
3471 33
K V
1 2
16 3
11 2
20 3
18 2
39 39
23 22
12 19
123 4
145 6
3 5
26 4
9574 4
7 1
135 5
193 99
18237 3
22 4
1293 3
3471 33
在对向量进行排序后,假设输出应如下所示:
193 99
39 39
3471 33
23 22
12 19
145 6
3 5
135 5
123 4
26 4
9574 4
22 4
16 3
20 3
18237 3
1293 3
1 2
11 2
18 2
7 1
193 99
39 39
3471 33
23 22
12 19
145 6
3 5
135 5
123 4
26 4
9574 4
22 4
16 3
20 3
18237 3
1293 3
1 2
11 2
18 2
7 1
但与此相反,我得到了一些元素也按第一个值排序:
193 99
39 39
3471 33
23 22
12 19
145 6
3 5
135 5
123 4
26 4
9574 4
22 4
20 3 ->Changed element
16 3 ->Changed element
18237 3
1293 3
18 2 ->Changed element
11 2
1 2 ->Changed element
7 1
193 99
39 39
3471 33
23 22
12 19
145 6
3 5
135 5
123 4
26 4
9574 4
22 4
20 3->更改的元素
16 3->更改的元素
18237 3
1293 3
18 2->更改的元素
11 2
1 2->已更改的元素
7 1
为什么会发生这种情况??我做错了什么??
非常感谢您的帮助。std::sort不能保证“相等”元素的顺序保持不变。为此,您需要std::stable\u排序。在本上下文中,“相等”是指两个元素a和b
!((a < b) || (b < a))
!((a
尝试以下代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
int main()
{
std::vector<std::pair<int, int>> v;
// If your compiler does not support list initialization then you can use push_back
v.push_back( std::pair<int, int>( 1, 3 ) );
v.push_back( std::pair<int, int>( 2, 4 ) );
v.push_back( std::pair<int, int>( 4, 5 ) );
v.push_back( std::pair<int, int>( 33, 3 ) );
// The lambda expression can be substituted for a function with the same body
std::sort( v.begin(), v.end(),
[]( const std::pair<int, int> &p1, const std::pair<int, int> &p2 )
{
return ( p1.second > p2.second ||
( !( p2.second > p1.second ) && p1.first < p2.first ) );
} );
for ( const auto &p : v ) std::cout << p.first << '\t' << p.second << std::endl;
std::cout << std::endl;
}
您的谓词将如下所示
struct predicate
{
bool operator ()( const std::pair<int, int> &left, const std::pair<int, int> &right ) const
{
return ( left.second > right.second ||
( !( right.second > left.second ) && left.first < right.first ) );
}
};
struct谓词
{
布尔运算符()(常数std::pair&left,常数std::pair&right)常数
{
返回(左.秒>右.秒| |
(!(right.second>left.second)和&left.first
它被称为稳定排序,这意味着如果第二个值相同,那么它将遵循与输入相同的排序,就像输入中16 3在20 3之前一样。因此,在结果16 3将在20 3之前。在C++代码中,应该添加StaseLySoRo()而不是SoRT()。以下是我接受的代码:
#include <bits/stdc++.h>
using namespace std;
bool compare( const pair<long long int,long long int>& x, const pair<long long int, long long int>& y )
{
return (x.second>y.second);
}
int main()
{
vector<pair<long long int,long long int> >a;
long long int n,i,j,b,c;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>b>>c;
a.push_back(pair<long long int,long long int>(b,c));
}
stable_sort(a.begin(),a.end(),compare); //must include stable_sort
vector<pair<long long int,long long int> >::iterator p;
for(p=a.begin();p!=a.end();p++)
{
cout<<p->first<<" "<<p->second<<endl;
}
return 0;
}
#包括
使用名称空间std;
布尔比较(常数对和x、常数对和y)
{
返回(x秒>y秒);
}
int main()
{
向量;
长整型n,i,j,b,c;
cin>>n;
对于(i=1;i>b>>c;
a、 向后推(对(b,c));
}
稳定排序(a.begin(),a.end(),compare);//必须包括稳定排序
向量::迭代器p;
对于(p=a.begin();p!=a.end();p++)
{
库蒂不知道稳定排序,这只是完美地回答了我的问题,解决了我的问题,谢谢你,伙计!:)就像F。。。。。。。。。。。。。。。。
#include <bits/stdc++.h>
using namespace std;
bool compare( const pair<long long int,long long int>& x, const pair<long long int, long long int>& y )
{
return (x.second>y.second);
}
int main()
{
vector<pair<long long int,long long int> >a;
long long int n,i,j,b,c;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>b>>c;
a.push_back(pair<long long int,long long int>(b,c));
}
stable_sort(a.begin(),a.end(),compare); //must include stable_sort
vector<pair<long long int,long long int> >::iterator p;
for(p=a.begin();p!=a.end();p++)
{
cout<<p->first<<" "<<p->second<<endl;
}
return 0;
}