17.24
#include <iostream>
#include <regex>
using namespace std;
int main()
{
string phone =
"(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
regex r(phone);
smatch m;
string s;
string fmt = "$2.$5.$7";
while (getline(cin, s))
cout << regex_replace(s, r, fmt) << endl;
return 0;
}
17.25
#include <iostream>
#include <regex>
#include <vector>
using namespace std;
int main()
{
string phone =
"(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
regex r(phone);
string s;
string fmt = "$2.$5.$7";
while (getline(cin, s)) {
s = regex_replace(s, r, fmt);
sregex_iterator it(s.begin(), s.end(), r);
cout << it->str() << endl;
}
return 0;
}
17.26
#include <iostream>
#include <regex>
using namespace std;
int main()
{
string phone =
"(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
regex r(phone);
smatch m;
string s;
string fmt = "$2.$5.$7";
while (getline(cin, s)) {
s = regex_replace(s, r, fmt);
sregex_iterator it(s.begin(), s.end(), r), end_it;
auto beg = it;
if (beg == end_it) {
cout << "no match" << endl;
continue;
}
if (++beg == end_it)
cout << it->str() << endl;
else {
while (beg != end_it) {
cout << beg->str() << endl;
++beg;
}
}
}
return 0;
}
17.27
#include <iostream>
#include <regex>
using namespace std;
int main()
{
string pattern = "(\\d{5})([-])?(\\d{4})";
regex r(pattern);
string s;
string fmt = "$1-$3";
while (getline(cin, s))
cout << regex_replace(s, r, fmt) << endl;
return 0;
}
这篇博客主要探讨了C++ Primer第五版中的17.3.4节,包括了17.24、17.25、17.26和17.27四个练习题目。通过详细解析,深入理解C++中模板特化、模板元编程等关键概念。

290

被折叠的 条评论
为什么被折叠?



