是否有必要使用.str()在regex_search中存储match.suffix()?

问题描述 投票:-1回答:1

我必须使用regex_search的匹配结果的后缀。我拥有的字符串大约是百万行,平均只需要30毫秒,只需将其分配给临时字符串即可。如果我不使用.str(),那么程序工作正常并且平均需要相同的30ms。使用时间参考仅用于比较目的。

regex_search(input, match, re);
tempStr = match.suffix().str();

如果时间(毫秒)没有显着差异,请告诉我.str()的目的。

c++ regex string
1个回答
2
投票

是否有必要使用.str()在regex_search中存储match.suffix()?

没有.match.suffix()可以隐式转换为string,效果相当于调用.str()

我想只有在类型扣除的情况下才需要明确调用.str()

auto match(std::regex re, std::string s) {
    std::smatch m;
    std::regex_match(s, m, re);
    // return m.suffix(); // bad: the return value is dangling
    return m.suffix().str(); // OK: copies the matched part
}
© www.soinside.com 2019 - 2024. All rights reserved.