注册 登录
编程论坛 C++教室

为什么sort对一个struct排序总是崩溃 但是stable_sort就没事儿

指手画脚 发布于 2010-01-04 12:22, 1228 次点击
建了一个MFC控制台
为什么SortCompare里面总是有str2或str1为空的情况呢
程序代码:
#include <algorithm>
#include <vector>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object
struct STRU_Record
{
    CString strName;
    CString strFile;
    SYSTEMTIME stime;
};


CWinApp theApp;

using namespace std;

int SortCompare(const STRU_Record & str1, const STRU_Record & str2)
{   
    return str1.(str2.strName);
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        cerr << _T("Fatal Error: MFC initialization failed") << endl;
        nRetCode = 1;
    }
    else
    {
        // TODO: code your application's behavior here.
        CString strHello;
        strHello.LoadString(IDS_HELLO);
        cout << (LPCTSTR)strHello << endl;
    }
    vector<STRU_Record> arrRecord;
    for (int i=0; i<20; i++)
    {
        STRU_Record struProcess;
        GetSystemTime(&struProcess.stime);
        struProcess.strFile.Format(_T("%d"), i);
        struProcess.strName.Format(_T("%d"), i*2);
        arrRecord.push_back(struProcess);
    }
    sort(arrRecord.begin(), arrRecord.end(), SortCompare);
    for (i=0; i<arrRecord.size(); i++)
    {
        printf("%s\n", arrRecord[i].strName);
    }

    return nRetCode;
}

0 回复
1