我正在尝试使用 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/