-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathremove_chars.cpp
More file actions
26 lines (23 loc) · 882 Bytes
/
remove_chars.cpp
File metadata and controls
26 lines (23 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Write an efficient function that deletes characters from an ASCII string. Use
* the prototype
* string removeChars( string str, string remove );
* where any character existing in remove must be deleted from str.
* For example, given a str of "Battle of the Vowels: Hawaii vs. Grozny"
* and a remove of "aeiou", the function should transform str to
* "Bttl f th Vwls: Hw vs. Grzny".
*/
#include "remove_chars.hpp"
#include <set>
void remove_chars(std::string& s, const std::vector<char>& to_remove) {
std::set<char> chars_to_remove(to_remove.cbegin(), to_remove.cend());
size_t removed_count = 0;
for (size_t i = 0; i < s.length(); ++i) {
if (chars_to_remove.find(s[i]) != chars_to_remove.end()) {
removed_count++;
continue;
}
s[i - removed_count] = s[i];
}
s.resize(s.length() - removed_count);
}