1

我正在使用带有 MS Visual C++ 2010 的 Qt 4.7.4。

我正在使用以下 QMap:

QMap<T_FileMapKey, HANDLE>  m_oMapHistHandle; 

其中 T_FileMapKey 定义为:

typedef struct tagT_FileMapKey
{
    int iSubscriptIdx;
    int iFilterIdx_1;
    int iFilterIdx_2;
} T_FileMapKey;

为了让整个事情顺利进行,我重载了 < 运算符:

bool operator< (const T_FileMapKey& tVal1, const T_FileMapKey& tVal2)
{
    if(tVal1.iSubscriptIdx < tVal2.iSubscriptIdx)
    {
        return true;
    }
    else
    {
        if(tVal1.iFilterIdx_1 < tVal2.iFilterIdx_1)
        {
            return true;
        }
        else
        {
            if (tVal1.iFilterIdx_2 < tVal2.iFilterIdx_2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}; 

正如您可能预测的那样,整个操作是以 3 维数组的顺序存储文件句柄。我正在使用 QMap,因为只使用了少数索引组合,而且它们可能是大数字。

我的问题是:

if (INVALID_HANDLE_VALUE == m_oMapCurrHandle.value(tFileKey, fdInvalidHandle))
....

if (false == m_oMapHistHandle.contains(tFileKey))
....

(其中 tFileKey 是 T_FileMapKey 变量)并不总是返回正确的值。

在正常情况下,QMap 会随着时间的推移而增长,这意味着如果遇到新的索引组合,则会打开文件并将条目添加到 QMap。如果我在调试模式下启动应用程序,Qt Visual Studio 插件允许我查看存储的键值对。我可以看到调试监视中的条目存在(例如 {0, 32767, 0}),但是两个函数调用(包含和值)告诉我 QMap 没有存储这样的键。通常在 QMap 具有至少 15 个键值对之后会遇到此行为。

这可能是 Qt 4.7.4 中的错误吗?做错了什么?

4

2 回答 2

4

operator<错了。为了解释,让我们考虑一个更简单的原因来写作operator<just pair<int, int>。您的版本是这样实现的:

bool operator<(const pair<int, int>& lhs, const pair<int, int>& rhs) 
{
    if (lhs.first < rhs.first) {
        return true; // (1)
    }
    else if (lhs.second < rhs.second) {
        return true; // (2)
    }
    else {
        return false; // (3)
    }
}

所以{1,4}<{2,3}因为 (1)。但是{2,3}<{1,4}因为 (2)!所以我们最终得到一个operator<不建立排序的。

建立字典比较的最简单方法是依次完全处理每个术语:

bool operator<(const pair<int, int>& lhs, const pair<int, int>& rhs)
{
    if (lhs.first != rhs.first) {
        return lhs.first < rhs.first;
    }
    else {
        return lhs.second < rhs.second;
    }
}

同样的想法可以很容易地扩展到你的三元组。

于 2015-01-14T15:07:39.013 回答
0

我认为您的问题在于您的 less 运算符,因为您从不与更大的符号进行比较。你应该有这样的东西:

bool operator< (const T_FileMapKey& tVal1, const T_FileMapKey& tVal2)
{
    if(tVal1.iSubscriptIdx < tVal2.iSubscriptIdx)
    {
        return true;
    }
    else if (tVal2.iSubscriptIdx < tVal1.iSubscriptIdx)
    {
        return false ;
    }
    else
    {
        if(tVal1.iFilterIdx_1 < tVal2.iFilterIdx_1)
        {
            return true;
        }
        else if (tVal2.iFilterIdx_1 < tVal1.iFilterIdx_1 )
        {
            return false ;
        }
        else
        {
            if (tVal1.iFilterIdx_2 < tVal2.iFilterIdx_2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}; 

我只使用了较少的运算符,因为您可能没有其他定义

于 2015-01-14T15:08:55.243 回答