【发布时间】:2014-07-25 08:45:04
【问题描述】:
在我的程序中,有一部分我需要对结构数组进行排序。 一切都很顺利,直到我认为结束。 对于某些条目,一切都很好并且可以工作,直到数组末尾的某些条目。 它会引发分段错误,我不知道为什么。
struct overview_table{
string o_timestamp;
string n_timestamp;
string dbID;
};
sort(overview.begin(),overview.end(),compareStrings);
static bool compareStrings(const overview_table &a_timestamp, const overview_table &b_timestamp){
cout << "744" << endl;
if ( a_timestamp.n_timestamp.compare(b_timestamp.n_timestamp) <= 0){
cout << "746" << endl;
return true;
} else {
cout << "749" << endl;
return false;
}
}
供参考:输出仅用于检查引发分段错误的位置。它介于 744 和 746 之间,以及我在数组末尾的想法。但我不知道为什么
【问题讨论】:
-
我们缺少一半的代码来帮助您。
-
是时候启动调试器了。
-
试试
a_timestamp.n_timestamp.compare(b_timestamp.n_timestamp) < 0(或a_timestamp.n_timestamp < b_timestamp.n_timestamp)。std::sort需要严格的弱顺序。<=不是自反的,因此也不是严格的弱排序。