判断是否为C++合法标识符
需要C++11的支持
源代码
#include <iostream>
#include <regex>
#include <string>
using namespace std;
bool Jugement(const string &temp_str) {
regex reg("[_[:alpha:]][_[:alnum:]]*");
if (regex_match(temp_str, reg)) {
return true;
}
else {
return false;
}
}
void F() {
string temp_str;
cout << "请输入字符串: " << endl;
while (cin >> temp_str) {
cout << temp_str << " ";
if (Jugement(temp_str)) {
cout << "yes" << endl;
}
else {
cout << "no" << endl;
}
}
}
int main() {
F();
return 0;
}