使用正则表达式时c ++程序崩溃

Moh*_*ian 1 c++ regex gcc g++ mingw32

到达正则表达式部分时编译崩溃后的代码:

我想检查收到的字符串中是否存在任何数字或不存在.

#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;
}
Run Code Online (Sandbox Code Playgroud)

Sva*_*zen 7

您正在使用没有分配内存的指针.这会使你的程序崩溃.只需将其声明为字符串,并尝试避免使用裸指针:

std::string w;
Run Code Online (Sandbox Code Playgroud)