给定一个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.
更多推荐
发布评论