Problem p00640 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

struct Button {
  int lx, ly, rx, ry, to;
  bool Check(int cx, int cy) {
    return (lx <= cx) && (cx <= rx) && (ly <= cy) && (cy <= ry);
  }
};

int N, W, H, Q;
vector<int> B(102);
vector<string> vs(102);
vector<Button> vbs[102];

int main() {
  while (cin >> N, N) {
    cin >> W >> H;

    vector<vector<string>> links(N, vector<string>());
    map<string, int> maps;
    string temp;
    int lx, ly, rx, ry;

    rep(i, N) {
      cin >> vs[i] >> B[i];
      maps.insert(make_pair(vs[i], i));
      vbs[i].clear();
      rep(j, B[i]) {
        cin >> lx >> ly >> rx >> ry >> temp;
        vbs[i].push_back((Button){lx, ly, rx, ry, -1});
        links[i].push_back(temp);
      }
    }

    rep(i, N) {
      rep(j, links[i].size()) { vbs[i][j].to = maps[links[i][j]]; }
    }

    cin >> Q;
    string query;
    int cx, cy;
    vector<string> buffer(1, vs[0]);
    vector<string>::iterator itr;
    int ptr = 0;

    rep(q, Q) {
      cin >> query;
      if (query == "click") {
        cin >> cx >> cy;
        int cur = maps[buffer[ptr]];
        rep(i, B[cur]) {
          if (vbs[cur][i].Check(cx, cy)) {
            if (ptr != buffer.size() - 1) {
              itr = buffer.begin();
              advance(itr, ptr + 1);
              buffer.erase(itr, buffer.end());
            }
            buffer.push_back(vs[vbs[cur][i].to]);
            ++ptr;
            // printf("clicked success to %s. current is %s:%d\\n",
            //        vs[ vbs[cur][i].to ].c_str(),
            //        buffer[ptr].c_str(),
            //        ptr);
            break;
          }
        }
      } else if (query == "show") {
        cout << buffer[ptr] << endl;
      } else if (query == "back") {
        if (ptr > 0)
          --ptr;
      } else if (query == "forward") {
        if (buffer.size() - 1 >= ptr + 1)
          ++ptr;
      } else {
        puts(query.c_str());
        assert(false);
      }
      // printf("state %3d:", q);
      // rep(i,buffer.size()) printf("[%s]",buffer[i].c_str()); puts("\\n");
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

struct Button {
  int lx, ly, rx, ry, to;
  bool Check(int cx, int cy) {
    return (lx <= cx) && (cx <= rx) && (ly <= cy) && (cy <= ry);
  }
};

int N, W, H, Q;
vector<int> B(102);
vector<string> vs(102);
vector<Button> vbs[102];

int main() {
  while (cin >> N, N) {
    cin >> W >> H;

    vector<vector<string>> links(N, vector<string>());
    map<string, int> maps;
    string temp;
    int lx, ly, rx, ry;

    rep(i, N) {
      cin >> vs[i] >> B[i];
      maps.insert(make_pair(vs[i], i));
      rep(j, B[i]) {
        cin >> lx >> ly >> rx >> ry >> temp;
        vbs[i].push_back((Button){lx, ly, rx, ry, -1});
        links[i].push_back(temp);
      }
    }

    rep(i, N) {
      rep(j, links[i].size()) { vbs[i][j].to = maps[links[i][j]]; }
    }

    cin >> Q;
    string query;
    int cx, cy;
    vector<string> buffer(1, vs[0]);
    vector<string>::iterator itr;
    int ptr = 0;

    rep(q, Q) {
      cin >> query;
      if (query == "click") {
        cin >> cx >> cy;
        int cur = maps[vs[ptr]];
        rep(i, B[cur]) {
          if (vbs[cur][i].Check(cx, cy)) {
            if (ptr != buffer.size() - 1) {
              itr = buffer.begin();
              advance(itr, ptr + 1);
              buffer.erase(itr, buffer.end());
            }
            buffer.push_back(vs[vbs[cur][i].to]);
            // printf("clicked success to %s\\n", vs[ vbs[cur][i].to ].c_str());
            ++ptr;
            break;
          }
        }
      } else if (query == "show") {
        cout << buffer[ptr] << endl;
      } else if (query == "back") {
        if (ptr > 0)
          --ptr;
      } else if (query == "forward") {
        if (buffer.size() - 1 >= ptr + 1)
          ++ptr;
      } else {
        assert(false);
      }
      // printf("state %3d:", q);
      // rep(i,buffer.size()) printf("[%s]",buffer[i].c_str()); puts("");
    }
  }
  return 0;
}