Consider the following code
string s;
cin >> s;
vector<bool> deleted(int(s.size()));
int t;
cin >> t;
while (t--) {
char x;
cin >> x;
deleted[x - 'a'] = true;
}
for (int i = 0; i < s.size(); i++) {
if (deleted[s[i] - 'a']) continue;
cout << s[i];
}
cout << "\n";
The above code takes a string consisting of lowercase English letters and t
lowercase English letters to remove from s
.
Input like the following should cause accessing out of bounds, but it seems like it's valid, since it prints the correct result "hod"
howdy
2
w
y
w
and y
will access indices 22
and 24
which is obviously greater than vector size.