【发布时间】:2020-06-20 15:19:09
【问题描述】:
我正在尝试为每个给定的时间间隔打印它在输入数据中出现的次数,例如对于输入: 4 1 5 6 8 6 8 4 7 该程序应打印: 1 5 : 1 6 8 : 2 4 7 : 1 这是我的代码
struct Interval
{
long unsigned int start, end;
};
bool operator<(const Interval& i1, const Interval& i2)
{
bool renvoie = true;;
if(i1.start < i2.start)
{
renvoie = true;
}
else if(i1.start== i2.start && i1.end>=i2.end)
renvoie = true;
else
renvoie = false;
return renvoie;
}
bool operator==(const Interval& i1, const Interval& i2)
{
return (i1.start==i2.start && i1.end==i2.end);
}
int main()
{
int nbr;
cin>>nbr;
map<Interval, int> occurence;
for(int i=0;i<nbr;i++)
{
Interval jetable;
cin>>jetable.start;
cin>>jetable.end;
occurence[jetable]++;
}
for(map<Interval,int>::iterator it = occurence.begin(); it!=occurence.end(); ++it)
{
cout << (it->first).start <<" " <<(it->first).end<<" "<<(it->second)<<endl;
}
}
问题是,两个相同的区间被认为是不同的,为什么??
【问题讨论】:
-
提示:如果
i1.start == i2.start和i1.end == i2.end在您的operator <中会发生什么?结果是什么,true或false?
标签: c++ dictionary struct stl operator-overloading