我正在尝试使用std :: map,其中键是c风格的字符串而不是std :: strings但是在编译IBM iSeries目标v7r1m0时遇到问题
我想使用c风格的字符串,因为使用性能资源管理器(PEX),为了地图查找而创建大量临时字符串似乎非常昂贵.
为此,我使用了自定义比较器,但在iSeries上进行编译时出现错误:
"/QIBM/include/t/xtree.t",第457.30行:CZP0218(30)该调用
与"const mycompany :: myapp :: cmp_str :: operator()"的任何参数列表都不匹配.
"/QIBM/include/t/xtree.t",第457.21行:CZP1289(0)
类型为"mycompany :: myapp :: cmp_str&" 的隐式对象参数无法使用类型为"const mycompany ::"的隐含参数进行初始化MYAPP :: cmp_str".
我的比较器定义为:
struct cmp_str
{
bool operator()(char const *a, char const *b)
{
return std::strcmp(a, b) < 0;
}
};
Run Code Online (Sandbox Code Playgroud)
并在地图中使用:
class LocalSchema : public RecordSchema
{
public:
int Operation;
//map<string, int> FieldLookup;
map<char *, int, cmp_str> FieldLookup;
};
Run Code Online (Sandbox Code Playgroud)
我做了些蠢事吗?
编辑:改为
std::map<char const*, int, cmp_str>
Run Code Online (Sandbox Code Playgroud)
给出了同样的错误.进一步查看作业日志,我看到在处理以下函数时产生了此错误:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
更改
map<char *, int, cmp_str>
Run Code Online (Sandbox Code Playgroud)
至
std::map<char const*, int, cmp_str>
Run Code Online (Sandbox Code Playgroud)
也就是说,有std::和有const.
编辑:也使比较成员功能const,即
struct cmp_str
{
bool operator()(char const *a, char const *b) const
{
return std::strcmp(a, b) < 0;
}
};
Run Code Online (Sandbox Code Playgroud)
注1:IBM的C++编译器因为以微妙的方式不符合而臭名昭着,因此您可能仍会遇到问题.
注意2:您需要确保字符串比地图更长.例如,您可以使用vector<unique_ptr<char const[]>>字符串的所有者,以便清理.