可以迭代字符串流中的字符而不改变其状态?(Possible to iterate through characters in stringstream without changing its sta

编程入门 行业动态 更新时间:2024-10-25 13:21:12
可以迭代字符串流中的字符而不改变其状态?(Possible to iterate through characters in stringstream without changing its state?)

给定一个stringstream ss ,是否有一种巧妙的方法来迭代当前其中的字符,而不改变其状态?

当然,我可以stringstream ss2(ss.str()) ,然后吃掉ss2中的字符,但我希望有一种方法可以避免创建那个中间字符串,因为我的ss可能包含数百万个字符。


背景:我将一些数据编码为ss ,并希望在将CRC写入磁盘文件之前附加CRC。 它用于计算我想通过ss迭代的CRC。 要做的道德事情可能是将stringstream进行子类化,并在运行中维护CRC,但对于我的直接目的而言,这样做感觉太麻烦了。

Given a stringstream ss, is there a neat way to iterate through the characters currently in it, without changing its state?

Of course, I can stringstream ss2(ss.str()) and then eat the characters from ss2, but I am hoping there is a way to avoid creating that intermediate string because my ss may contain millions of characters.


Background: I encode some data into ss, and want to append a CRC before writing it to a disk file. It's for calculating the CRC that I want to iterate through ss. The moral thing to do would probably be to subclass stringstream and maintain the CRC on the fly, but that feels like too much trouble for my immediate purposes.

最满意答案

std::stringstream使用std::stringbuf作为其流缓冲区。 std::stringbuf是可寻找的,就像普通文件一样。

您可以根据心脏的需要迭代缓冲的字符串,然后寻找缓冲区的开头或字符串中的其他位置,然后将时钟倒回到原来的位置。

A std::stringstream uses a std::stringbuf as its stream buffer. A std::stringbuf is seekable, like an ordinary file.

You can iterate through the buffered string to your heart's desire, then seek back to the beginning of the buffer, or to some other spot inside the string, and rewind the clock back where it was.

更多推荐

本文发布于:2023-04-29 08:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   串流   而不   状态   迭代

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!