数组在排序后保持不变。这怎么可能?我可以看到健康的比较结果。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