如果值是一个字符串,则该函数在映射中进行迭代;如果是映射,则将其添加到字符串文本中,以调用该函数,依此类推。
string functest(map<string, any> input)
{
string text;
std::map<std::string, std::any>::iterator it;
for (it = input.begin(); it != input.end(); it++)
{
std::string key = it->first;
auto value = it->second;
std::string type = value.type().name();
std::string stringType = typeid(std::string).name();
if (!stringType.compare(type))
{
text.append(any_cast<std::string>(value));
}
else
{
map<string, any> temp = any_cast<map<string, any>>(value);
text.append(functest(temp));
}
}
return text
}
std::map<std::string, std::any> map1;
std::map<std::string, std::any> map2;
std::map<std::string, std::any> map3;
map2["1"] = "test";
map3 = map2;
map1["test"] = map2;
map1["test2"] = map3;
functest(map1);
在functest的第二次调用之后,我们返回到第一次调用。但是,它的(迭代器)值是结束值,而不是终止值。我该如何解决?