config是地图中的地图:
std::map<std::string, std::map<std::string, std::string>> config;
我正在尝试解析配置文件(.ini)。
所以,
config[sectionName] = map<std::string, std::string>>();
config[sectionName][optionKey] = value; //when option and value store a string.
现在,
我正在尝试实现此功能。 我得到错误:一个是“sections == section”和“sections.second ==”false“”无法比较。我收到的错误是“错误:二进制表达式的操作数无效”。
有谁能请解释我有什么问题?
/*
* Searches for option's value in section and converts it to a bool if possible.
*
* If the value isn't one of the options below or if section/option
* don't exist the conversion fails.
*
* Returns a pair of <bool, bool>
* first is set to true if conversion succeeded, false otherwise.
* If the conversion succeeds, second is the converted boolean value.
* If the conversion fails, it doesn't matter what second is set to.
*
* Converts to true: "true", "yes", "on", "1"
* Converts to false: "false", "no", "off", "0"
*
*/
pair<bool, bool> ConfigParser::getBool(const string& section,
const string& option) const
{
for(const auto& sections : config){
if(sections == section && sections.first != ""){
if(sections.second == "true" || sections.second == "yes" ||
sections.second == "on" || sections.second == "1"){
return pair<bool, bool>(true, true);
}
if(sections.second == "false" || sections.second == "no" ||
sections.second == "off" || sections.second == "0"){
return pair<bool, bool>(true, false);
}
}
}
return pair<bool, bool>(false, false);
}
答案 0 :(得分:2)
考虑你的代码片段:
for(const auto& sections : config) {
if(sections == section && sections.first != "") {
此处sections
为pair<string, map<string, string>>
,section
为string
。
那些不具有可比性。
如果您只想查找section
部分,则可以使用更多方式。例如:
pair<bool, bool> ConfigParser::getBool(const string& section,
const string& option) const
{
auto it = config.find(section);
if (it == config.end()) { return {false, false}; }
auto jt = it->second.find(option);
if (jt == it->second->end()) { return {false, false}; }
// parse jt->second
return {true, /* parse result */ };
}