I use std::regex r("-?[0-9]*(.[0-9]+)?(e-?[0-9]+)?")to check numbers (ints / fixed point / floating point). MWE below:
#include <iostream>
#include <string>
#include <vector>
#include <regex>
#include <algorithm>
using namespace std;
bool isNumber(string s) {
s.erase(remove(s.begin(), s.end(), ' '), s.end());
std::regex r("-?[0-9]*(.[0-9]+)?(e-?[0-9]+)?");
return regex_match(s, r);
}
int main() {
std::vector<string> test{
"3", "-3",
"1 3", " 13", " 1 3 ",
"0.1", "-100.123123", "1000.12312",
"1e5", "1e-5", "1.5e-10", "1a.512e4",
"a", "1a", "baaa", "1a",
"1e--5", "1e-",
};
for (auto t : test) {
cout.width(20); cout << t << " " << isNumber(t) << endl;
}
return 0;
}
I noticed that the compilation time is quite long compared to what I expected:
gcc 5.4 -O0 -std=c++11, 2.3 secondsgcc 5.4 -O2 -std=c++11, 3.4 secondsclang++ 3.8 -O0 -std=c++11, 1.8 secondsclang++ 3.8 -O2 -std=c++11, 3.7 seconds
I use this for an online application of a judge who has time at the compilation stage.
So, instructive questions:
- Why is compilation time so long? I get the impression that when using regular expressions in vim / emacs / grep / ack / ag, etc. (On the same computer) compilation really takes a lot less.
- Is there a way to reduce the regex compilation time in C ++?
source
share