如何连接两个字符串流?
#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;
}两次尝试的输出如下所示:
0xbf8cfd20
baby0xbf8cfddc所需的输出是babyyou。
发布于 2011-11-23 02:24:40
发布于 2011-11-23 02:27:04
或
a << b.rdbuf();假设get指针位于流的开头,以避免为内容分配另一个std::string。
发布于 2011-11-23 02:26:44
更普遍的是,在iostream中:
std::istream is1, is2; // eg. (i)stringstream
std::ostream os; // eg. (o)stringstream
os << is1.rdbuf();
os << is2.rdbuf();
os << std::flush;这适用于文件流、std::cin等,也适用于字符串
https://stackoverflow.com/questions/8231746
复制相似问题