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

标签 c++ ibm-midrange

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

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

为此,我使用了自定义比较器,但在 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;
    }
};

并在 map 中使用:

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[]>>作为字符串的所有者,允许您进行清理。

关于c++ - 在 IBM i 系列上为 std::map 使用自定义比较器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30916579/

相关文章:

c++ - 在 QNX Momentics 中为自定义构建配置定义自定义符号

c++ - 项目在 Windows 7 上运行,但无法在带有 Virtual Box 的 Linux 上运行

java - JT400 错误 CPF3CF1 在 QTOCNETSTS.SRVPGM 中调用 API QtocRtvTCPA

java - 如何从 AS400 检索特定的 JobList?

c++ - 类成员在其他类中充当友元 C++

c++ - 为什么 chrono::nanoseconds 的表示类型是有符号整数类型?

c++ - MPI 没有收到我通过阻塞发送/接收发送的相同缓冲区

Windows 上的 PHP -> AS400 有简单的方法吗?

ibm-midrange - 检索数据结构字段的名称

sql - 对连接字符串的计算字段求和(拆分 acct #s)