31 lines
771 B
C++
31 lines
771 B
C++
#include <iostream>
|
||
#include <sstream> //istringstream 必须包含这个头文件
|
||
#include <string>
|
||
using namespace std;
|
||
int main() {
|
||
string source = "i an a boy";
|
||
|
||
// 声明一个字符串输入流
|
||
istringstream sin;
|
||
// 将字符串导入到sin输入流中
|
||
sin.str(source);
|
||
|
||
// 分离的方法1
|
||
string s;
|
||
while (sin >> s) {
|
||
cout << s << endl;
|
||
}
|
||
cout << "===============================" << endl;
|
||
|
||
// 重复使用必须先清空
|
||
sin.clear();
|
||
// 重新加载,准备二次输入,或者sin的重用
|
||
sin.str(source);
|
||
string a, b, c, d;
|
||
sin >> a >> b >> c >> d;
|
||
cout << a << endl;
|
||
cout << b << endl;
|
||
cout << c << endl;
|
||
cout << d << endl;
|
||
return 0;
|
||
} |