在 C++ 中连接字符串流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8231746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:13:18  来源:igfitidea点击:

concatenate stringstream in c++

c++concatenationstringstream

提问by Carlitos Overflow

How can I concatenate two stringstreams?

如何连接两个字符串流?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "types.h"    

int main () {
    char dest[1020] = "you";
    char source[7] = "baby";
    stringstream a,b;
    a << source;
    b << dest;
    a << b; /*HERE NEED CONCATENATE*/
    cout << a << endl;
    cout << a.str() << endl;
    return 0;
}

The output is the following in both tries:

两次尝试的输出如下:

0xbf8cfd20
baby0xbf8cfddc

The desired output is babyyou.

所需的输出是babyyou

回答by jli

Should be:

应该:

b << dest;
a << b.str();

stringstream::strreturns the underlying string in the stringstream.

stringstream::str返回stringstream.

回答by Michael Krelin - hacker

Or

或者

a << b.rdbuf();

provided that get pointer is on the beginning of the stream to avoid allocating yet another std::stringfor content.

前提是 get 指针位于流的开头以避免std::string为内容分配另一个指针。

回答by Nawaz

You don't need two instance of std::stringstream. One is enough for the purpose.

你不需要两个std::stringstream. 一个就够了。

std::stringstream a;
a << source << dest;

std::string s = a.str(); //get the underlying string

回答by sehe

More generically across iostreams:

更一般地跨 iostream:

std::istream is1, is2; // eg. (i)stringstream
std::ostream os;       // eg. (o)stringstream

os << is1.rdbuf();
os << is2.rdbuf();
os << std::flush;

This works for filestreams, std::cin etc. as well as for stringstream

这适用于文件流、std::cin 等以及字符串流

回答by FailedDev

Just replace a << bwith a << b.str()

只需替换 a << ba << b.str()

回答by Mr Ed

The question was "how to concatenate STREAMS", the answers explained how to concatenate the contents of the streams. Here is a class which can be used to concatenate two istreams into one istream (file ConcatStreams.h):

问题是“如何连接流”,答案解释了如何连接流的内容。这是一个可用于将两个 istream 连接成一个 istream(文件 ConcatStreams.h)的类:

class ConcatStreams
: public std::streambuf {
std::streambuf* sbuf1_;
std::streambuf* sbuf2_;
char*           buffer_;
int useBuf;
int bufSize;
public:
ConcatStreams(std::streambuf* sbuf1, std::streambuf* sbuf2)
    : bufSize(1024), sbuf1_(sbuf1), sbuf2_(sbuf2), buffer_(new char[bufSize]), useBuf(1) {
}
ConcatStreams(const ConcatStreams& orig);
virtual ~ConcatStreams() { delete[] this->buffer_; }
int underflow() {
    if (this->gptr() == this->egptr()) {
        // get data into buffer_, obtaining its input from
        // this->sbuf_; if necessary resize buffer
        // if no more characters are available, size == 0.
        std::streamsize size=0;
        if(useBuf==1) {
            size = this->sbuf1_->sgetn(this->buffer_, bufSize);
            if(!size) { useBuf++;}
        } 
        if(useBuf==2) {
            size = this->sbuf2_->sgetn(this->buffer_, bufSize);
            if(!size) { useBuf++;}
        }
        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());
 }
};

To use it:

要使用它:

#include "ConcatStreams.h"
istringstream msgIn1("this is a stream.");
istringstream msgIn2("this is another stream.");
ConcatStreams cs(msgIn1.rdbuf(), msgIn2.rdbuf());
istream msgIn(&cs);
cout << "'" << msgIn.rdbuf() << "'" << endl;

Basically the class uses the streambuf's from the streams passed to it to create a new streambuf which first reads the first streambuf and then reads the second streambuf when finished with the first one.

基本上,该类使用传递给它的流中的流缓冲来创建一个新的流缓冲,该流缓冲首先读取第一个流缓冲,然后在完成第一个流缓冲后读取第二个流缓冲。