如何在mfc中对日期和时间进行排序?

时间:2011-03-23 06:54:20

标签: c++ mfc visual-c++

我知道如何在mfc中获取当前日期和时间。但我想在日期和时间数据类型的帮助下对数组进行排序。

我该怎么做?

此致

KARTHIK

1 个答案:

答案 0 :(得分:2)

CTime有一个“<”运算符,所以你可以使用std :: sort()

void SortTime(CTime vals[], size_t nVals)
{
    std::sort(vals, vals+nVals);
}

如果您有一个包含CTimes的对象,您可以创建自己的“<”操作

struct MyStuff
{
    std::string sName;
    int         nNumber;
    CTime       time;
};

bool operator < (const MyStuff &lhs, const MyStuff &rhs)
{
    return lhs.time < rhs.time;
}

void SortStuff(MyStuff vals[], size_t nVals)
{
    std::sort(vals, vals+nVals);
}

或更好

void SortStuff(std::vector<MyStuff> vals)
{
    std::sort(vals.begin(), vals.end());
}