使用操纵器忽略标点符号
是否可以在 cin 上使用 std 操纵器忽略标点符号?例如,假设您有一个输入流(在实际情况下是一个文件),例如:“一,二三”。我希望能够做到:
f >> ignore_punct >> a;
f >> ignore_punct >> b;
f >> ignore_punct >> c;
最后 a=="one"
, b=="two"
, c==" Three"
代码>.
Is it possibile to ignore punctuacion using std manipulator on cin? For example suppose you have an input stream (in the actual case a file) like: "one, two three". I want to be able to do:
f >> ignore_punct >> a;
f >> ignore_punct >> b;
f >> ignore_punct >> c;
at the end a=="one"
, b=="two"
, c=="three"
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
它使用本地来过滤标点符号。
这允许其余代码保持不变。
Try this:
It uses the local to filter out punctuation.
This allows the rest of the code to remain unchanged.
没有标准库的方法可以做到这一点,但如果我理解正确的话,这很容易做到。 一样,那么您可以使用接受谓词而不是单个分隔符的
getline
版本:如果您想读取一个字符串直到一些标点符号,就好像它是换行符 示例:
如果您还想在换行符上中断,那么您可以编写一个函子:
并调用
getline(std::cin, my_string, punct_or_newline())
来代替。希望这有帮助!There's no standard-library way to do it, but it's pretty easy to do, if I understand you correctly. If you want to read a string until some punctuation as though it were a newline, then you could use a version of
getline
that accepts a predicate instead of a single delimiter:Usage example:
If you also want to break on newlines, then you can write a functor:
And invoke as
getline(std::cin, my_string, punct_or_newline())
instead. Hope this helps!