【发布时间】:2016-05-31 00:50:50
【问题描述】:
我需要订购一组对(一个是 int,第二个是 char),我需要像这样订购我的集合: 12 G, 11 F, 10 A, 10 B, 10 C(降序排列第一个,升序排列第二个) 第一。这是我迄今为止尝试过的,我得到了一些错误:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <utility>
#include <set>
using namespace std;
set <pair <int,char> > s;
bool myfunction( const pair<int, char>& i, const pair<int, char>& j ) {
if( i.first < j.first ) return false;
if( j.first < i.first ) return true;
return j.second < i.second;
}
void writes()
{ set <pair<int,char> >::iterator it;
for (it = s.begin();it<= s.end();it++) /// line (18)
cout<<(*it).second<<" "<<(*it).first<<"\n\n";
}
int main()
{ ifstream f("info.in");
int n;
f>>n;
for (int i=1;i<=n;i++)
{ pair<int,char> x;
int st;
char nd;
f>>st;
f>>nd;
x.first=st;
x.second=nd;
s.insert(x);
}
writes();
}
我得到的第一个错误在第 (18) 行:'operator::.....
非常感谢您的帮助
我的输入文件如下所示:
5
10 B
10 A
10 C
11 F
12 G
@Sam Varshavchik,谢谢!这解决了我的错误问题。 但是,我仍然没有得到我需要的输出。 我只得到:
10 A
10 B
10 C
11 F
12 G
是否可以成对更改订单条件?如果没有,您会建议改用什么?
看起来排序标准的 myfunction 仍然被程序忽略。我怎么能在我的配对中超载它?看起来,它只是坐在那里,从未使用过。无论如何,该程序都可以正常工作
我也试过这个:Using custom std::set comparator 但是还是不行
using namespace std;
struct lex_compare {
bool operator()(const pair<int, char>& i, const pair<int, char>& j )
{
if( i.first != j.first )
{
return (i.first > j.first);
}
return (j.second > i.second);
}
} // forgot ";", after adding it, it works perfectly.
set <pair <int,char>, lex_compare > s; ///line (22)
void writes()
{ set <pair<int,char> >::iterator it;
for (it = s.begin();it!= s.end();it++) /// line (18)
cout<<(*it).second<<" "<<(*it).first<<"\n\n";
}
int main()
{ ifstream f("info.in");
int n;
f>>n;
for (int i=1;i<=n;i++)
{ pair<int,char> x;
int st;
char nd;
f>>st;
f>>nd;
x.first=st;
x.second=nd;
s.insert(x);
}
writes();
}
错误:第 (22) 行:'s' 之前的声明符无效;
【问题讨论】:
-
// forgot ";", after adding it, it works perfectly.基于一个简单的印刷错误投票结束。
标签: c++