跳到正文

个人记录

stringstream 的简单用法

lyas145约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

遇到类似于 P1020 [NOIP1999 提高组] 导弹拦截的恶心输入怎么办?

stringstream!!!

这玩意能通过以下方式读入一行数(不给数的数量)。

首先定义一个字符串(string 类型)。

C++
string line;

然后把一行所有乱七八糟的东西全读入进来。

C++
getline(cin,line);

定义一个 stringstream,并把上述读入的乱七八糟的东西的字符串赋值给定义的 stringstream。

C++
stringstream sin(line);

最后是读入数组!

C++
while (sin>>a[++n]);

More importantly,它需要头文件 sstream或 bits/stdc++.h!

没了。

原文