在 IBM i 系列上为 std::map 使用自定义比较器

Using a custom comparator for std::map on IBM i-Series

我正在尝试使用 std::map,其中的键是 c 风格的字符串而不是 std::strings,但是在以 v7r1m0 为目标的 IBM iSeries 上编译它时遇到问题

我想使用 C 风格的字符串,因为使用 Performance Explorer (PEX) 时,为地图查找创建大量临时字符串的成本似乎非常高。

为此,我使用了自定义比较器,但在 iSeries 上编译时出现错误:

"/QIBM/include/t/xtree.t", line 457.30: CZP0218(30) The call does not
match any parameter list for "const mycompany::myapp::cmp_str::operator()".
"/QIBM/include/t/xtree.t", line 457.21: CZP1289(0) The implicit object
parameter of type "mycompany::myapp::cmp_str &" cannot be initialized with an implied argument of type "const mycompany::myapp::cmp_str".

我的比较器定义为:

struct cmp_str 
{
    bool operator()(char const *a, char const *b)
    {
        return std::strcmp(a, b) < 0;
    }
};

并在地图中使用:

class LocalSchema : public RecordSchema
{
    public:
        int Operation;
        //map<string, int> FieldLookup;
        map<char *, int, cmp_str> FieldLookup;
};

我是不是在做傻事?

编辑: 更改为

std::map<char const*, int, cmp_str>

给出同样的错误。进一步查看作业日志,我发现这个错误是在处理以下函数时产生的:

inline int VxSubfile::IndexOfFieldInSchema(char * columnName)
{
    std::map<char const*, int, cmp_str>::iterator iter = _fieldLookup.find(columnName);
    if(iter == _fieldLookup.end())
    {
        return -1;
    }
    else{
        jdebug("Returning index : %d", iter->second);
        return iter->second;
    }
}

改变

map<char *, int, cmp_str>

std::map<char const*, int, cmp_str>

std::const.

编辑:同时制作比较成员函数const,即

struct cmp_str 
{
    bool operator()(char const *a, char const *b) const
    {
        return std::strcmp(a, b) < 0;
    }
};

注意 1:IBM 的 C++ 编译器因在微妙的方面不符合规范而臭名昭著,因此您可能仍然 运行 遇到问题。

注意 2:您需要确保字符串比映射更有效。例如。您可以使用 vector<unique_ptr<char const[]>> 作为字符串的所有者,以便您进行清理。