【发布时间】:2010-08-27 00:56:49
【问题描述】:
作为练习,我正在尝试创建一个输入流操纵器,它将吸收字符并将它们放入一个字符串中,直到它遇到特定字符或直到它到达 eof。这个想法来自 Bruce Eckel 的“Thinking in c++”第 249 页。
这是我目前的代码:
#include <string>
#include <iostream>
#include <istream>
#include <sstream>
#include <fstream>
#include <iomanip>
using namespace std;
class siu
{
char T;
string *S;
public:
siu (string *s, char t)
{
T = t;
S = s;
*S = "";
}
friend istream& operator>>(istream& is, siu& SIU)
{
char N;
bool done=false;
while (!done)
{
is >> N;
if ((N == SIU.T) || is.eof())
done = true;
else
SIU.S->append(&N);
}
return is;
}
};
并对其进行测试....
{
istringstream iss("1 2 now is the time for all/");
int a,b;
string stuff, zork;
iss >> a >> b >> siu(&stuff,'/');
zork = stuff;
}
这个想法是 siu(&stuff,'/') 会从 iss 中吸取字符,直到遇到 /。我可以使用调试器观看它,因为它通过 '/' 获取字符 'n' 'o' 'w' 并终止循环。在我看到 Stuff 之前,这一切似乎都在顺风顺水。 Stuff 现在有字符等但每个字符之间有 6 个额外字符。这是一个示例:
- &东西0x0012fba4 {0x008c1861“nìììýìììýwìììýiìììýsìììýtìììýhìììýeìììýtəiìììýmìììýeìììýfìììýoìììýrəaìììýlìììýlə”}
发生了什么事?
【问题讨论】: