gpt4 book ai didi

c++ - sregex_token_iterator:Stroustrup示例是否有勘误?

转载 作者:行者123 更新时间:2023-12-01 14:51:36 31 4
gpt4 key购买 nike

我正在测试Stroustrup C++ 4th Ed Page 1069中的示例,其中可能未列出勘误表here。有谁知道应该如何修改此代码才能正确打印以下输出?这本书声称这应该是输出,但是我的测试什么也不输出(没有匹配项)。

#include <regex>
#include <iostream>
using namespace std;

int main()
{
string input {"aa::bb cc::dd ee::ff"};
regex pat {R"((\w+)([[:punct:]]+)(\w+)\s∗)"};
sregex_token_iterator end {};
for (sregex_token_iterator p {input.begin(),input.end(),pat,{1,3}};
p!=end; ++p)
cout << *p << '\n';

return 0;
}
书本声明输出应为:
aa 
bb
cc
dd
ee
ff
汇编:
clang++ -Wall -std=c++11 -pedantic test248.cc && ./a.out

Compilation finished at Wed Aug 5 20:09:52

最佳答案

    regex pat {R"((\w+)([[:punct:]]+)(\w+)\s∗)"};
最后的“星号”不是实际的 *星号,而是Asterisk Operator Unicode字符 U+2217。将其替换为 *和代码 will work

关于c++ - sregex_token_iterator:Stroustrup示例是否有勘误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63275170/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com