编译后我的代码在到达正则表达式部分时崩溃:
我想检查接收到的字符串中是否存在任何数字。
#include <iostream>
#include <regex>
using namespace std;
int main()
{
int in, count, rate;
char *w;
cin >> count;
for(in = 1; in < 5; in++) {
rate = 0;
cin >> w;
cout << "Case #";
cout << in;
cout << ":";
if (regex_match (std::string(w), regex("([0-9])")))
++rate;
cout << rate;
cout << endl;
}
return 0;
}
最佳答案
您正在使用未分配内存的指针。这将使您的程序崩溃。只需将其声明为字符串,并尽量避免裸指针:
std::string w;
关于使用正则表达式时 C++ 程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26183306/