全能打印神器
全能打印神器是一款非常好用的打印软件,可以在电脑、手机、平板电脑等设备上使用。支持无线打印和云打印,操作非常简单,使用起来也非常方便,有需要的小伙伴快来保存下载体验吧!
A useful tool for string operations is regex. This may be found in virtually all high-level 当前的编程语言,包括C++。正则表达式(Regex)被用作 通用搜索模式。例如,通过构建一个简单的字符串 被称为正则表达式,我们可以使用至少实现密码验证逻辑 一个大写字母,一个小写字母,一个数字,一个特殊字符,并且总长度至少为 8个字符。
In this tutorial, we'll look at how to use C++ to display only the first letters of words included 在指定的字符串内。在这里,我们将看一个使用空格来分隔单词的句子 无论字符是大写还是小写,计算机都会读取 将字符串使用正则表达式分割,并返回每个单词的第一个字符。
To use regular expressions, we need to import the regex library using the ‘regex’ header. To 使用正则表达式,我们需要以下语法 -
regex obj_name( <regular expression> ) </regular>
After defining regex, we can use them in multiple ways. We will see our intended approach 在下面。现在要从单词中读取第一个字符,正则表达式的语法将会是这样的 下面的内容为:
\b[a-zA-Z]
在这里,‘\b’表示单词的开头。[a-zA-Z]表示大写或小写字母 lowercase letters which are in the range of ‘a’ to ‘z’ or ‘A’ to ‘Z’. And only one of them is taken. 现在让我们看一下正在用于读取所有选定匹配项的迭代器对象 -
regex_token_iterator<:iterator> iterator_name( <begin pointer of string>, <ending pointer of string>, <regular expression>, <submatch>); </submatch></regular></ending></begin></:iterator>在这个迭代器中,前两个参数是起始和结束指针的 string object. The third parameter is the given regular expression object which we have 在之前创建。第四个参数是子匹配。当子匹配为0时,它将 返回那些来自匹配的元素(在匹配时)的内容,当 submatch is -1, it represents where the matching is not done (reverse of the submatch 0). submatch为-1,表示匹配未完成的位置(与submatch 0相反)
#include <iostream> #include <regex> using namespace std; string solve( string s){ string ret = ""; regex e("\b[a-zA-Z]"); regex_token_iterator<:iterator> i(s.begin(), s.end(), e, 0); regex_token_iterator<:iterator> end; while (i != end) { ret += (*i++); ret += ", "; } return ret; } int main(){ string s = "A string to read only the first letter of words"; cout <h3>输出</h3> <pre class="brush:php;toolbar:false;">Given String: A string to read only the first letter of words The first letter of each word: A, s, t, r, o, t, f, l, o, w, Given String: Central Pollution Control Board The first letter of each word: C, P, C, B,
C++免费学习笔记(深入):立即学习
>在学习笔记中,你将探索 C++ 的入门与实战技巧!
已抢6799个
抢已抢91611个
抢已抢14418个
抢已抢50597个
抢已抢190557个
抢已抢86252个
抢