2

可以这样串流吗?我正在尝试使用 ifstream 阅读并进行转换。

string things = "10 11 12 -10";
int i1;
int i2;
int i3;
int i4;
stringstream into;
into << things;
into >> i1;
into >> i2;
into >> i3;
into >> i4;

我希望它是:

i1 = 10
i2 = 11
i3 = 12
i4 = -10

那是对的吗?

可以多次使用同一个字符串流变量吗?

当我尝试时,第一次还可以,但后来一切都只是 0。

4

5 回答 5

6

那肯定行得通。您甚至可以混合类型,如下所示:

string things = "10 Nawaz 87.87 A";
int i;
std::string s;
float f;
char c;

stringstream into;
into << things;
into >> i >> s >> f >> c; //all are different types!

cout << i <<"  "<< s <<"  "<< f <<"  "<< c;

输出:

10 Nawaz 87.87 A

ideone 演示:http ://www.ideone.com/eb0dR

于 2011-02-20T05:24:17.427 回答
1

它有效吗?我会这样做的方式是:

istringstream into(things);
into >> i1;

等等。这将产生您发布的输出。

于 2011-02-20T05:18:32.977 回答
1

您发布的内容与 Jeremiah Willcock 的解决方案一起使用istringstream。但是也可以考虑使用scanf系列函数(对于几个 int 并没有太大的区别,但是对于更高级的输入,使用 scanf 可能比使用流操纵器更简洁):

string things = "10 11 12 -10";
int i1, i2, i3, i4, i5, i6;
sscanf(things.c_str(), "%d %d %d %d", &i1, &i2, &i3, &i4);

您的示例在此之后仅给出 0 的原因是,stringstream一旦您提取 -10,缓冲区就为空:您必须先在缓冲区中插入更多内容,然后才能提取更多内容。您可以多次使用同一个stringstream实例,但您要么每次都需要完全使用缓冲区,要么意识到在您将下一个项目插入缓冲区之前缓冲区中还有更多内容:

string things = "10 11 12 -10", temp;
int i1, i2, i3, i4;
stringstream into;
into << things; //buffer is now "10 11 12 -10"
into >> i1; //buffer is now " 11 12 -10"
into >> i2; //" 12 -10"
into >> i3; //" -10"
into >> i4; //""

//more code here...

//come back and use the instance again
into << "more: 1 2 3"; //"more: 1 2 3"
into >> temp; //temp is "more:"; into's buffer is " 1 2 3"
into >> i1; //buffer is " 2 3"

//more code here...

//reuse into once again
into << "4 5 6"; // buffer is now " 2 3 4 5 6"
into >> i1; //i1 gets the 2 from before, not the 4 just inserted; buffer now " 3 4 5 6"
into >> i2; //i2==3; buffer is " 4 5 6"

此外,ios(从中stringstream继承)还定义了!运算符和强制转换,void*以便您可以方便地检查提取是否失败(技术上检查是否设置failbitbadbit设置,我相信当缓冲区中没有足够时failbit设置eofbit):

string things = "10 11 12 -10";
int i1, i2, i3, i4;
stringstream into;
into << things;
into >> i1 >> i2 >> i3 >> i4;
if (into >> i5) {
    cout << "extraction to i5 succeeded" << endl;
}
if (!(into >> i6)) {
    cout << "extraction to i6 failed" << endl;
}
于 2011-02-20T05:28:37.500 回答
0

至于你的第二个问题 ,可以多次使用同一个字符串流变量吗?,
可能clearseekof 流在重用之前需要。
例如,以下代码将解决您的第二个问题:

int main() {
  string  s = "-1 2";
  stringstream  ss;
  ss << s;
  int i, j;
  ss >> i >> j;
  cout<< i <<' '<< j <<endl;
  ss.clear();
  ss.seekg( 0 );
  i = j = 0;
  ss >> i >> j;
  cout<< i <<' '<< j <<endl;
}

希望这可以帮助。

于 2011-02-20T14:50:32.787 回答
0

是的,它会起作用。
我要做的唯一区别是:

string things = "10 11 12 -10";
int i1;
int i2;
int i3;
int i4;

stringstream into(things);    // Initialize in the constructor
into >> i1 >> i2 >> i3 >> i4; // Chain the inputs.

注意 linestream 就像一个普通的流,当你读完它的结尾时,它会用完项目并设置失败状态。

stringstream into(things)
int val;
while(into >> val)                   // loop exits after 4 numbers as into.eof() 
{                                    // returns true after trying to read the 5 number.
    std::cout << "G(" << val << ")\n";
}

例如,在读取具有 4 个数字的多行时,我喜欢执行以下操作:

std::string  line;
while(std::getline(std::cin, line))
{
    /*
     * This way if a line is formatted incorrectly (only 3 numbers)
     * The error is local to the line and we will pick it up in linestream
     * without affecting the scanning of subsequent lines
     */
    std::stringstream linestream(line);

    int i1,i2,i3,i4;
    linestream >> i1 >> i2 >> i3 >> i4;
}
于 2011-02-20T07:23:30.617 回答