• 5997阅读
  • 9回复

关于 c++的sort()函数[未解决完] [复制链接]

上一主题 下一主题
离线shifan
 
只看楼主 倒序阅读 楼主  发表于: 2009-08-23
std::vector<std::vector<QString> > &content;
bool fieldCompare(std::vector<QString> &parameter1,std::vector<QString> &parameter2);

有了以上的定义和相应的实现后:
sort(content.begin(),content.end(),fieldCompare);
为什么会报错呢?
显示说调用没有匹配的函数。。。
[ 此帖被shifan在2009-08-25 14:37重新编辑 ]
离线shifan
只看该作者 1楼 发表于: 2009-08-23
//countPage.h
#ifndef COUNTPAGE_H
#define COUNTPAGE_H
#include<qwidget.h>
#include<vector>

class QHBoxLayout;
class QVBoxLayout;
class QComboBox;
class QDateEdit;
class QPushButton;
class QTable;
class QRadioButton;
class QLabel;
class QHButtonGroup;

class countPage:public QWidget
{
public:
    countPage(std::vector<std::vector<QString> > &content1,QWidget *parent);
public slots:
    void countBegin();
private:
    void clearTable();
    QString getType();
    void dealData();
    bool validation();
    bool fieldCompare(std::vector<QString> &parameter1,std::vector<QString> &parameter2);
    bool specialCompare(std::vector<QString> &parameter1,std::vector<QString> &parameter2);
    bool handmanCompare(std::vector<QString> &parameter1,std::vector<QString> &parameter2);
    bool typeCompare(std::vector<QString> &parameter1,std::vector<QString> &parameter2);
private:
    QTable *displayTable;
    QDateEdit *from;
    QDateEdit *to;
    QComboBox *measureType;
    QHButtonGroup *typeGroup;
    QRadioButton *income;
    QRadioButton *output;
    QRadioButton *none;
    QPushButton *count;    
    QLabel *fromLabel;
    QLabel *toLabel;
    QLabel *measureTypeLabel;
    QVBoxLayout *vertical;
    QHBoxLayout *displayH;
    QHBoxLayout *chooseH;
    QHBoxLayout *countH;
    QLabel *displayLabel;
    std::vector<std::vector<QString> > &content;
};
#endif
离线shifan
只看该作者 2楼 发表于: 2009-08-23
#include"countPage.h"
#include<algorithm>
#include<qmessagebox.h>
#include<qdatetime.h>
#include<qcombobox.h>
#include<qdatetimeedit.h>
#include<qpushbutton.h>
#include<qtable.h>
#include<qradiobutton.h>
#include<qlabel.h>
#include<qhbuttongroup.h>
#include<qlayout.h>
using namespace std;
countPage::countPage(std::vector<std::vector<QString> > &content1,QWidget *parent)
    :QWidget(parent),content(content1)
{
    vertical=new QVBoxLayout(this);
    
    displayH=new QHBoxLayout(vertical);        

    displayTable=new QTable(30,5,this);
    displayTable->setMaximumSize(350,400);
    displayH->addWidget(displayTable);
    displayLabel=new QLabel(this);
    displayH->addWidget(displayLabel);

    chooseH=new QHBoxLayout(vertical);        
    
    fromLabel=new QLabel("from",this);
    fromLabel->setMaximumSize(40,30);
    from=new QDateEdit(this);
    toLabel=new QLabel("to",this);
    toLabel->setMaximumSize(40,30);
    to=new QDateEdit(this);
    QDate max(2100,1,1);
    QDate min(2000,1,1);
    from->setRange(min,max);    
    to->setRange(min,max);    
    from->setDate(QDate::currentDate());
    to->setDate(QDate::currentDate());
    chooseH->addWidget(fromLabel);
    chooseH->addWidget(from);
    chooseH->addWidget(toLabel);
    chooseH->addWidget(to);
    

    measureTypeLabel=new QLabel("measureType",this);
    measureTypeLabel->setMaximumSize(90,30);
    measureType=new QComboBox(this);
    measureType->insertItem("field");
    measureType->insertItem("special");
    measureType->insertItem("handman");
    measureType->insertItem("type");
    chooseH->addWidget(measureTypeLabel);
    chooseH->addWidget(measureType);

    typeGroup=new QHButtonGroup("type",this);
    income=new QRadioButton("income",typeGroup);    
    output=new QRadioButton("output",typeGroup);    
    none=new QRadioButton("none",typeGroup);    
    typeGroup->setButton(0);
    typeGroup->setRadioButtonExclusive(true);

    countH=new QHBoxLayout(vertical);        
    count=new QPushButton("count",this);
    countH->addWidget(typeGroup);
    countH->addWidget(count);

    connect(count,SIGNAL(clicked()),this,SLOT(countBegin()));
}
bool countPage::fieldCompare(vector<QString> &parameter1,vector<QString> &parameter2)
{
    if(parameter1[1]>parameter2[1]) return true;
    else return false;
}
bool countPage::specialCompare(vector<QString> &parameter1,vector<QString> &parameter2)
{
    if(parameter1[4]>parameter2[4]) return true;
    else return false;
}
bool countPage::handmanCompare(vector<QString> &parameter1,vector<QString> &parameter2)
{
    if(parameter1[8]>parameter2[8]) return true;
    else return false;
}
bool countPage::typeCompare(vector<QString> &parameter1,vector<QString> &parameter2)
{
    if(parameter1[2]>parameter2[2]) return true;
    else return false;
}

void countPage::countBegin()
{
    if(validation()==true)
    {
        dealData();
    }    
}
bool countPage::validation()
{
    if(measureType->currentText()!="type"&&typeGroup->selected()==none)
    {
        QMessageBox::information(this,"Error","You must choose a type");                
        return false;
    }
    if(from->date()>to->date())
    {
        QMessageBox::information(this,"Error","it's a wrong date choose");                
        return false;
    }
    return true;
}

void countPage::dealData()
{
    clearTable();
    int WAY;
    if(measureType->currentText()=="field")
    {
        sort(content.begin(),content.end(),fieldCompare);
        WAY=3;
    }
     else if(measureType->currentText()=="special")
    {
        sort(content.begin(),content.end(),specialCompare);
        WAY=4;
    }
    else if(measureType->currentText()=="handman")
    {
        sort(content.begin(),content.end(),handmanCompare);
        WAY=8;
    }
    else if(measureType->currentText()=="type")
    {
        sort(content.begin(),content.end(),typeCompare);
        WAY=2;
    }        
    
    struct row
    {
        QString name;
        int n;
        int incomeTotal;
        int outputTotal;
        int pureTotal;
    };
    row tableGang;
    tableGang.name="";
    tableGang.n=0;
    tableGang.incomeTotal=0;
    tableGang.outputTotal=0;
    tableGang.pureTotal=0;

    int i=0;

    vector<vector<QString> >::iterator temp;
    
    if(WAY==2)
    {
        for(temp=content.begin();temp!=content.end()&&i<29;temp++)
        {
            if(from->date()<=QDate::fromString((*temp)[1])&&to->date()>=QDate::fromString((*temp)[1])&&tableGang.name!=""&&(*temp)[WAY]!=tableGang.name)
            {
                displayTable->setText(i,0,tableGang.name);        
                displayTable->setText(i,1,QString::number(tableGang.n));        
                displayTable->setText(i,2,QString::number(tableGang.incomeTotal));        
                displayTable->setText(i,3,QString::number(tableGang.outputTotal));        
                displayTable->setText(i,4,QString::number(tableGang.pureTotal));
                i++;
                tableGang.name=(*temp)[WAY];
                tableGang.n=1;
                tableGang.incomeTotal=(*temp)[5].toInt();
                tableGang.outputTotal=(*temp)[6].toInt();
                tableGang.pureTotal=tableGang.incomeTotal-tableGang.outputTotal;        
            }
            else if(from->date()<=QDate::fromString((*temp)[1])&&to->date()>=QDate::fromString((*temp)[1]))
            {
                tableGang.name=(*temp)[WAY];
                ++tableGang.n;
                tableGang.incomeTotal+=(*temp)[5].toInt();
                tableGang.outputTotal+=(*temp)[6].toInt();
                tableGang.pureTotal=tableGang.incomeTotal-tableGang.outputTotal;        
            }
        }
    }
    else
    {
        for(temp=content.begin();temp!=content.end()&&i<29;temp++)
        {
            if(from->date()<=QDate::fromString((*temp)[1])&&to->date()>=QDate::fromString((*temp)[1])&&tableGang.name!=""&&(*temp)[WAY]!=tableGang.name&&(*temp)[2]==getType())
            {
                displayTable->setText(i,0,tableGang.name);        
                displayTable->setText(i,1,QString::number(tableGang.n));        
                displayTable->setText(i,2,QString::number(tableGang.incomeTotal));        
                displayTable->setText(i,3,QString::number(tableGang.outputTotal));        
                displayTable->setText(i,4,QString::number(tableGang.pureTotal));
                i++;
                tableGang.name=(*temp)[WAY];
                tableGang.n=1;
                tableGang.incomeTotal=(*temp)[5].toInt();
                tableGang.outputTotal=(*temp)[6].toInt();
                tableGang.pureTotal=tableGang.incomeTotal-tableGang.outputTotal;        
            }
            else if(from->date()<=QDate::fromString((*temp)[1])&&to->date()>=QDate::fromString((*temp)[1])&&(*temp)[2]==getType())
            {
                tableGang.name=(*temp)[WAY];
                ++tableGang.n;
                tableGang.incomeTotal+=(*temp)[5].toInt();
                tableGang.outputTotal+=(*temp)[6].toInt();
                tableGang.pureTotal=tableGang.incomeTotal-tableGang.outputTotal;        
            }
        }
    }
                displayTable->setText(i,0,tableGang.name);        
                displayTable->setText(i,1,QString::number(tableGang.n));        
                displayTable->setText(i,2,QString::number(tableGang.incomeTotal));        
                displayTable->setText(i,3,QString::number(tableGang.outputTotal));        
                displayTable->setText(i,4,QString::number(tableGang.pureTotal));
}

QString countPage::getType()
{
    if(typeGroup->selected()==income)return QString("income");
    if(typeGroup->selected()==output)return QString("output");
    if(typeGroup->selected()==none)return QString("none");
}
void countPage::clearTable()
{
    int i,j;
    for(i=0;i<30;i++)
        for(j=0;j<5;j++)
            displayTable->setText(i,j,"");
}
离线shifan
只看该作者 3楼 发表于: 2009-08-23
以上为源码,百思不得其解啊~
离线shifan
只看该作者 4楼 发表于: 2009-08-24
大家不要被代码吓住了啊,里面很多地方都是不用看的,我把他晒出来只是以防万一它对解决问题有帮助。。。。呵呵
离线wader
只看该作者 5楼 发表于: 2009-08-25
比较函数的声明格式不对吧
bool fieldCompare(std::vector<QString> &parameter1, std::vector<QString> &parameter2);
要比较的是vector<QString>中的元素,而不是vector本身应该声明如下
bool fieldCompare(QString &parameter1, QString &parameter2);
仅供参考
离线shifan
只看该作者 6楼 发表于: 2009-08-25
我是要比较vector本身,只不过比较的方法是通过比较一个QString。但还是谢谢您!
我这两天琢磨着有没有可能跟命名空间什么的有关系啊?还是一筹莫展啊~
离线wader
只看该作者 7楼 发表于: 2009-08-25
看错了,那个比较函数是不是应该声明为全局的或者静态的,楼主试试
离线shifan
只看该作者 8楼 发表于: 2009-08-25
十分感谢wader!!鞠躬!
最后是把比较函数的形参一律都加了const,晕啊,居然是const和非const类型不同造成的,憋了我两天,痛彻体肤啊!
希望大家以后不要跟我犯一样的错误啊~
离线shifan
只看该作者 9楼 发表于: 2009-08-25
引用第7楼wader于2009-08-25 09:19发表的  :
看错了,那个比较函数是不是应该声明为全局的或者静态的,楼主试试

确实还是要用static,十分感谢wader!
但是我不太明白这里为什么要用静态呢?
是什么原因呢?
快速回复
限100 字节
 
上一个 下一个