可以用这种方式进行字符串流吗?从字符串转换为整数?

发布于 2024-10-18 09:02:06 字数 394 浏览 6 评论 0原文

可以像这样串流吗? 我正在尝试使用 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。

Is it possible to stringstream like this?
I am trying to read with ifstream and convert it.

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;

I expect it to be :

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

is that correct?

Can the same stringstream variable be used multiple times?

When I tried, the first time was ok but everything else later on is just 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一向肩并 2024-10-25 09:02:06

那绝对应该有效。您甚至可以混合类型,如下所示:

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

That should definitely work. You can even mix types, as shown below:

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;

Output:

10 Nawaz 87.87 A

Demo at ideone : http://www.ideone.com/eb0dR

药祭#氼 2024-10-25 09:02:06

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

istringstream into(things);
into >> i1;

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

Does it work? The way I would do it is:

istringstream into(things);
into >> i1;

and so on. That would produce the output you posted.

甜点 2024-10-25 09:02:06

您发布的内容以及 Jeremiah Willcock 使用 istringstream 的解决方案都有效。但也可以考虑使用 scanf 函数系列(仅需要几个整数)没有太大区别,但对于更高级的输入,使用 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"

另外, iosstringstream 继承自)还定义了 ! 运算符和对 void* 的强制转换,以便您可以方便地检查提取是否失败(技术上检查是否设置了 failbitbadbit,我相信 failbit 是与 failbit 一起设置的code>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;
}

What you posted works, along with Jeremiah Willcock's solution using istringstream instead. But consider using the scanf family of functions as well (for just a couple of ints it doesn't make much of a difference, but for more advanced input, using scanf can be much more concise than messing around with stream manipulators):

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);

The reason your example just gives 0's after that is because the stringstream buffer is empty once you extract the -10: you'll have to insert more into the buffer before you can extract more. You can use the same stringstream instance multiple times, but you'll either need to fully use the buffer each time, or realize that there's more in the buffer before the next item you insert into the buffer:

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"

Also, ios (from which stringstream inherits) also defines the ! operator and a cast to void* so that you can conveniently check if extraction failed (technically checks if failbit or badbit is set, and I believe failbit is the one that gets set along with eofbit when there's not enough in the buffer):

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;
}
∝单色的世界 2024-10-25 09:02:06

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

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.

请注意,线路流就像普通流一样,当您读取超过其末尾时,它将耗尽项目并设置失败状态。

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;
}

Yes it will work.
The only difference I would make is:

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.

Note the linestream is just like a normal stream and it will run out of items and set failure state when you read past its end.

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";
}

For example I like to do the following when reading multiple lines with 4 numbers:

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;
}
囚我心虐我身 2024-10-25 09:02:06

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

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;
}

希望这会有所帮助。

As for your second question
Can the same stringstream variable be used multiple times?,
probably clear and seek of stream are needed before reuse.
For example, the following code will serve your second question:

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;
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文