• 6705阅读
  • 5回复

[提问]请问一下结构体成员的排序 [复制链接]

上一主题 下一主题
离线looyu
 
只看楼主 倒序阅读 楼主  发表于: 2012-06-20
用QLinkedList<用自己定义的结构体> 怎样排序的?
离线XChinux

只看该作者 1楼 发表于: 2012-06-20
没讲明白问题吧?
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线looyu
只看该作者 2楼 发表于: 2012-06-20
struct T{    int a;    int b;};
#include <QCoreApplication>#include <QDebug>#include <QLinkedList>int main(int argc, char *argv[]){    QCoreApplication a(argc, argv);    QLinkedList<T> list;    T temp;    temp.a = 2;    temp.b = 23;    list.append(temp);    temp.a = 1;    temp.b = 5;    list.append(temp);    temp.a = 5;    temp.b = 13;    list.append(temp);    temp.a = 6;    temp.b = 6;    list.append(temp);    //    //怎样以结构体(T.a)从小到大排序?    //    foreach (T tt,list)        qDebug() << "a="                 << tt.a                 << ",b="                 << tt.b;    return a.exec();}
离线looyu
只看该作者 3楼 发表于: 2012-06-20
struct T
{
    int a;
    int b;
};

#include <QCoreApplication>
#include <QDebug>
#include <QLinkedList>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QLinkedList<T> list;
    T temp;
    temp.a = 2;
    temp.b = 23;
    list.append(temp);
    temp.a = 1;
    temp.b = 5;
    list.append(temp);
    temp.a = 5;
    temp.b = 13;
    list.append(temp);
    temp.a = 6;
    temp.b = 6;
    list.append(temp);
    //
    //怎样以结构体(T.a)从小到大排序?
    //
    foreach (T tt,list)
        qDebug() << "a="
                 << tt.a
                 << ",b="
                 << tt.b;
    return a.exec();
}
离线passion_wu
只看该作者 4楼 发表于: 2012-06-20
我记得是这样的
struct T
{
    int a;
    int b;
   friend bool operator < (const T & t1, const T & t2){
        return t1.a < t2.a;
  }
};
离线looyu
只看该作者 5楼 发表于: 2012-06-21
引用第4楼passion_wu于2012-06-20 18:55发表的  :
我记得是这样的
struct T
{
    int a;
    int b;
.......

这个在结构体里应该没作用吧
快速回复
限100 字节
 
上一个 下一个