在结构中使用map时出现分段错误

时间:2016-03-21 17:03:47

标签: c++ dictionary segmentation-fault

所以,当程序进入for(它说“Here”)时我得到了一个分段错误,我无法理解为什么。每次制作新标签*对象时是否需要初始化地图?怎么样?

struct tag {
  map <string, tag*>   tg;
  map <string, string> at;
};

int main () {
  int              n, q;
  map<string, tag*> tags;
  vector<string>   current;
  string           s;
  char             c;

  // Parsing the HRML code
  in>>n>>q;
  for(int i = 1; i <= n; i++) {
    in>>c>>s;
    if(s[0] =='/') {
      current.pop_back();
    } else {
      current.push_back(s);
      tag *t = tags[current[0]];
      for(int i = 1; i < current.size(); i++) {
        t = t->tg[current[i]]; // <- Here
      }

      bool ok = true;
      while(ok) {
        in>>s;
        string S;
        in>>S>>c>>S;
        if(S[S.size()-1] == '>') {
          ok = false;
          S.erase(S.size()-1, 1);
        } S.erase(S.size()-1, 1);

        t->at[s] = S;
      }
    }
  }

0 个答案:

没有答案