| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1225 人关注过本帖
标题:为什么sort对一个struct排序总是崩溃 但是stable_sort就没事儿
只看楼主 加入收藏
指手画脚
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:334
专家分:560
注 册:2009-12-28
结帖率:60%
收藏
 问题点数:0 回复次数:0 
为什么sort对一个struct排序总是崩溃 但是stable_sort就没事儿
建了一个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;
}

搜索更多相关主题的帖子: struct 没事儿 sort stable 
2010-01-04 12:22
快速回复:为什么sort对一个struct排序总是崩溃 但是stable_sort就没事儿
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019598 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved