std::regex_match的结果似乎是错误的
Result of std::regex_match seems wrong
我正在尝试理解以下代码的行为。
我认为这可能是一个错误,因为结果对我来说看起来是错误的。
#include <iostream>
#include <regex>
int main(int ac, char **av)
{
std::regex reg("lib(.*)\.so");
std::smatch match;
std::cout << std::regex_match(std::string("libscio.so"), match, reg) << std::endl;
std::cout << match.str(1) << std::endl;
return 0;
}
我期待
1
scio
但它给了我
1
ocio
在 x86_64 GNU/Linux 上使用 gcc 版本 4.9.2 (Debian 4.9.2-10) 编译
我必须以不同的方式构建程序,因为它在VS 2015中无法编译。也许这导致了你的问题,你的编译器,也是?请注意字符串临时变量。
#include <iostream>
#include <regex>
int main(int ac, char **av)
{
std::regex reg("lib(.*)\.so");
std::smatch match;
std::string target = "libscio.so";
std::cout << std::regex_match(target, match, reg) << std::endl;
std::cout << match.str(1) << std::endl;
return 0;
}
这在VS 2015中产生1和scio,如预期的那样。
根据@LogicStuff发布的链接,这是因为传入临时对象意味着匹配项指向当临时对象超出范围时无效的迭代器。因此,您看到的可能是删除临时字符串时留下的垃圾。
相关文章:
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- Static_cast转换为错误的数据类型,但结果仍然正确?
- C++ 犰狳库中的sort_index()函数给出了错误的结果
- C++:使用类在向量中搜索特定元素时,我得到了错误的结果
- 检查 n2 是否是 n1 的倍数后结果错误,但根本没有错误
- 为什么我的性能基准给了我错误的结果?
- 使用具有默认参数的函数模板进行 decltype 会使结果混乱(一个有趣的问题或 gcc 的错误)
- 斐波那契检验(C++)的结果错误
- 在发布版本中划分两个双精度的结果错误
- 2D FFT后的图像结果错误
- 计算文本文件中的单个字母并打印结果 - 错误的输出 C++
- C++字符和 int 之间的转换,结果错误
- char*与std字符串的比较结果错误
- 斯波吉购物结果错误
- 超载"+"操作员,结果错误?
- 执行高斯消去程序时结果错误
- hash_djb2 PHP结果错误
- 按位操作比较结果错误
- 使用'if'语句和"<<"时的结果错误,'>>'运算符比较 3 个整数