I was working on a competitive programming question. I wrote this question to count the number of "VK" substrings.
int count(string test) {
int answer = 0;
for (int i = 0; i <= test.size()-2; i++) {
if (test.substr(i,2) == "VK")
answer++;
}
return answer;
}
Why did I receive this error message when I try just "V" as the argument?
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 2) > this->size() (which is 1)
Shouldn't the statements in the for loop not execute because the loop's condition failed?