數組在排序後保持不變。這怎麼可能?我可以看到健康的比較結果。std :: sort在std :: vector上進行比較,但從未替換
有擁有一個std ::向量模型類:
private:
std::vector<Contact> contacts;
類接觸有:
- 即QString私有成員
- 即QString私有成員吸氣 - 的GetName()
- 處理成員的複製構造函數
- 處理成員的賦值運算符定義爲
- <操作如下:
bool Contact::operator < (const Contact& contact) const {
QString str1 = contact.GetName();
QString str2 = this->GetName();
bool b = (QString::compare(str1,str2) < 0);
return b;
}
排序時我調試這個方法,我找到了正確的「B」返回,每一次。名字被正確地檢索,被正確地比較,並且「b」返回碼總是正確的。
在擁有該向量類,我有一種方法......
void ContactsModel::sort()
{
qDebug("Before Sorting: size: %d", this->contacts.size());
for (int i=0; i< this->contacts.size(); i++)
{
QString str = contacts[i].GetName();
qDebug(str.toAscii());
}
// trying to sort...
std::sort(this->contacts.begin(), this->contacts.end());
// PROBLEM: Output here is identical to what I had before the sort. The vector is not sorted, not even close. It's 52 random names in the same order they were initially put in the vector.
qDebug("After Sorting: size: %d", this->contacts.size());
for (int i=0; i< this->contacts.size(); i++)
{
QString str = contacts[i].GetName();
qDebug(str.toAscii());
}
}
請發佈您的排序代碼。 – Richard 2011-03-23 15:23:18
也許是因爲它已經排序? :)誰知道沒有看到問題的一個例子。 – 2011-03-23 15:25:20
-1沒有發佈代碼,並提出問題,如果我們有第六感,當OP甚至不使用常識! – Nawaz 2011-03-23 15:33:54