C ++中的复合std :: istream [英] Composite std::istream in C++

查看:108
本文介绍了C ++中的复合std :: istream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个std::istream对象的列表,需要将它们显示为单个std::istream对象.因此,如果我有三个istream,A,B和C,我希望能够创建一个istream,D,它将首先从A返回字节,然后从B返回字节,然后在EOF之前是C.到达.复合流将始终按顺序读取,并在读取所有字节后关闭.

I have a list of std::istream objects that I need to appear as a single std::istream object. So if I have three istreams, A, B and C, I want to be able to create an istream, D which will first return the bytes from A then the bytes from B, then C before EOF is reached. The composite stream will always be read sequentially and closed after all bytes are read.

是否有使用stl/boost做到这一点的简单方法,还是只需要编写自己的复合istream?

Is there a simple way to do this using stl/boost or do I just need to write my own composite istream?

推荐答案

另一个问题的题外答案( https://stackoverflow.com/a/17103292/1424877 )可能会为您提供帮助.

The off-topic answer to another question (https://stackoverflow.com/a/17103292/1424877) might help you.

#include <iostream>
#include <string>
#include <sstream>

class ConcatStreams : public std::streambuf
{
    int useBuf;
    std::streambuf *sbuf_[2];
    char buffer_[1024];

  public:
    ConcatStreams(std::istream& sbuf1, std::istream& sbuf2) :
        useBuf(0), sbuf_{sbuf1.rdbuf(), sbuf2.rdbuf()}
    { }

    int underflow()
    {
        if (this->gptr() == this->egptr()) {
            std::streamsize size = 0;
            while (useBuf < 2) {
                size = this->sbuf_[useBuf]->sgetn(this->buffer_, sizeof this->buffer_);
                if (!size) {
                    useBuf++;
                } else {
                    break;
                }
            } 
            this->setg(this->buffer_, this->buffer_, this->buffer_ + size);
        }
        return this->gptr() == this->egptr()
             ? std::char_traits<char>::eof()
             : std::char_traits<char>::to_int_type(*this->gptr());
    }
};

int main()
{
    std::istringstream is("hello world!\n");

    ConcatStreams cs_(is, std::cin); // prepend "hello world" to the input
    std::istream cs(&cs_);

    std::string s;
    while (cs >> s)
      std::cout << "'" << s << "'" << std::endl;
}

请注意,您不能使用此特殊技巧将std::cin与自身连接,甚至不能将is与自身连接;但将任意两个不同输入流进行串联应该可以正常工作.您甚至可以串联多个ConcatStreams实例!

Notice that you can't use this particular trick to concatenate std::cin with itself, or even is with itself; but it should work fine to concatenate any two different input streams. You can even concatenate multiple ConcatStreams instances!

这篇关于C ++中的复合std :: istream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆