#include <iostream>
#include <sstream>
#include <cctype> // toupper
using namespace std;
int main() {
string line;
getline(cin, line); // 读取整行输入
stringstream ss(line); //ss 就是 stringstream 类型的变量,像操作输入流 cin 一样,把字符串 line 按空格分割成一个个单词。
string word;
string abbr = "";
while (ss >> word) {
abbr += toupper(word[0]); // 取首字母并转为大写
}
cout << abbr << endl;
return 0;
}
stringstream 是 C++ 标准库中的一个类,定义在 <sstream> 头文件中。它允许你把字符串当成流来操作,类似于 cin 或 cout。常用来 从字符串中分词(提取单词或数字),或者做字符串与其他类型间的转换。
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
int main() {
string line = "Hello World 123";
stringstream ss(line); // 把字符串放入stringstream
string word;
while (ss >> word) { // 按空格依次提取单词
cout << word << endl;
}
return 0;