For循环未执行

For loop not getting executed

本文关键字:执行 循环 For      更新时间:2023-10-16

我有以下代码片段,在其中我试图将一些语句打印到XML文件中:

void parseXML::writeStruct(std::fstream& abc,std::string prnt)
{
    for (map<string,struct structSet>::iterator it = structData.begin();it != structData.end();it++)
    {
      if (((it->second.parent.compare("")==0) && (it->second.written == false)))
      {
        bool write = true;
        if (it->second.type.compare("")==0)
        {
            for (set<std::string>::iterator i = it->second.fields.begin(); i != it->second.fields.end(); i++)
            {
                map<string,struct fieldSet>::iterator fd = fieldData.find(*i);
                if (fd != fieldData.end())
                {
                    std::string type = fd->second.type;
                    map<string,struct structSet>::iterator ntC = structData.find(type);
                    if (ntC != structData.end())
                    {
                        if (ntC->second.type.compare("") != 0)
                        {
                           map<string,struct structSet>::iterator ntC = structData.find(ntC->second.type);
                           if (ntC == structData.end()|| ntC->second.type.compare("")!= 0||ntC->second.written == false)
                           {  
                              continue;
                           }
                        }
                        else
                        {
                            map<string,struct structSet>::iterator ntC = structData.find(ntC->second.type);
                            if (ntC->second.parent.compare(it->second.name))
                            {
                            }
                            else if (ntC->second.written == true)
                            {
                                abc << INDENT << "t" <<"t" << "<nonterminal ref= "" << ntC->second.name.c_str() << "">" << std::endl;
                                abc << INDENT << "t" << "t" <<"t" << "<name>" << fd->second.name.c_str() << "</name>" << std::endl;
                                abc << INDENT << "t"<< "t" << "</nonterminal >" << std::endl;
                            }
                        }
                    }

问题是它没有执行第一个for循环:

for (map<string,struct structSet>::iterator it = structData.begin(); it != structData.end(); it++)

可能的原因是什么?

这里有一个(希望)完整的列表,很抱歉有明显的条目,但我们不知道你有多有经验:

  • structData为空
  • structData已损坏,导致应用程序崩溃
  • parseXML::writeStruct从未执行
  • 执行了for循环,但以下(iffor)条件失败,您对此解释错误

选择调试器或添加跟踪消息(不要忘记使用endl,因为输出通常是行缓冲的,在崩溃时会丢失)。

注意:只发布代码的相关部分,其余部分对我们来说只是噪音(除非你想要一个代码审查https://codereview.stackexchange.com/)