Я пытаюсь пройти через вектор строк с итератором, но ему нужно проверить несколько значений заранее. Я попытался сделать это со следующим кодом:
typedef std::vector<std::string>::iterator Cursor;
std::string check(Cursor& at) {
int number;
std::string ans = *at;
if (ans.empty()) {
return "NOT OK";
} else {
std::stringstream ss(ans);
ss >> number >> ans;
if (ans == "NOT OK") {
return "NOT OK";
} else if (ans == "VALUE") {
int x;
ss >> x;
std::string result("OK");
for (Cursor c = at; x > 0; ++c, --x) {
result = check(c);
}
return result;
} else {
return "OK";
}
}
}
Но этот код падает, когда он входит в цикл for, и я понятия не имею, почему.
int x;
ss >> x; // Here you should check whether x has the expected value.
std::string result("OK");
for (Cursor c = at; x > 0; ++c, --x) { // here you do not check whether c is within boundaries.
result = check(c);
}
// c should start from at+1, otherwise it may result in infinite recursion.
// Also, termination condition should be added.
for (Cursor c = at+1; x > 0 && c != myVec.end(); ++c, --x) {