You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
771 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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