Skip to content

Instantly share code, notes, and snippets.

@cesarkawakami
Created March 19, 2017 01:39
Show Gist options
  • Save cesarkawakami/df9c99225beff519c748c5bda7250014 to your computer and use it in GitHub Desktop.
Save cesarkawakami/df9c99225beff519c748c5bda7250014 to your computer and use it in GitHub Desktop.
#include <cctype>
#include <cstdio>
#include <string>
#include <algorithm>
std::string clean_string1(std::string w) {
w.erase(std::remove_if(w.begin(), w.end(), [](char &c) { return !isalpha(c); }), w.end());
std::transform(w.begin(), w.end(), w.begin(), tolower);
return w;
}
std::string clean_string2(std::string w) {
w.erase(std::remove_if(w.begin(), w.end(), [](char &c) -> bool {
if (!isalpha(c)) {
return true;
}
if (!islower(c)) {
c = tolower(c);
}
return false;
}), w.end());
return w;
}
std::string clean_string3(std::string w) {
auto it = w.begin();
for (auto jt = w.begin(); jt != w.end(); ++jt) {
if (!isalpha(*jt)) { continue; }
*it++ = tolower(*jt);
}
w.erase(it, w.end());
return w;
}
std::string clean_string4(std::string w) {
int i = 0;
for (int j = 0; j < (int)w.size(); ++j) {
if (!isalpha(w[j])) { continue; }
w[i++] = tolower(w[j]);
}
w.erase(w.begin() + i, w.end());
return w;
}
int main() {
const int N = 50;
const int LOOPS = 200000;
std::string chunk("abcdefghijklmnopqrstuvwxyz");
std::string s;
for (int i = 0; i < N; ++i) {
s += chunk;
}
for (int i = 0; i < LOOPS; ++i) {
std::string x(s);
x = clean_string3(x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment