'regex' is not a member of 'std' 错误解决方法:使用 std::string::find() 进行子串匹配

如果您在编译代码时遇到 'regex' is not a member of 'std' 错误,这通常是因为您的编译环境不支持 C++11 的正则表达式库 std::regex

一种解决方法是使用 std::string::find() 函数来进行简单的子串匹配。以下是使用 std::string::find() 修改后的代码示例:cppvoid MONITOR_BASE::show_cpu_status(){ std::vector process_info = getTopProcesses_CPU(MONITOR_CONSTANT::FIFTY); LOG(INFO) << 'CPU Temperature: ' << getCpuTemperature() << '℃';

std::string root = std::string(std::getenv('AUTO_ROOT'));

for (auto& ele : process_info)    {        if (ele.COMMAND.find(root) != std::string::npos && ele.COMMAND != '')        {            LOG(INFO) << 'Process(' << getLastElement(ele.COMMAND)                      << ') take CPU(' << ele.CPU                      << '%), MEM(' << ele.MEM                      << '%), VSZ(' << ele.VSZ / 1024 << ' MB)';            std::string process_name = getLastElement(ele.COMMAND);            module_cpu_info(process_name, ele.CPU, ele.MEM, ele.VSZ);        }    }    clear_cpu_info_();    pub_memory_.publish(cpu_info_);}

在这个修改后的代码中:

  • 我们使用 std::string::find() 函数来查找 root 字符串在 ele.COMMAND 中的位置。- 如果 find() 函数返回 std::string::npos,则表示未找到匹配的子串。- 否则,find() 函数返回子串在字符串中的起始位置。

请确保在调用 show_cpu_status() 函数之前,std::getenv('AUTO_ROOT') 返回的环境变量值是正确的。

总结

使用 std::string::find() 函数可以简单有效地替代 std::regex 进行子串匹配,尤其是在编译环境不支持 C++11 正则表达式库的情况下。

'regex' is not a member of 'std' 错误解决方法:使用 std::string::find() 进行子串匹配

原文地址: http://www.cveoy.top/t/topic/GJr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录