(1)头文件
#include <sstream>
库定义了三种类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作。
(2)stringstream的作用
stringstream 主要是用在將一个字符串分割。
(读入字符串用getline(cin,s),而不用cin读入,因为直接用cin读入读不了空格。)
(3)stringstream ss(s);表达的是什么意思??
读取s中的单字,比如hello world ,就会读取hello和world。
(4)stream << t ; //向流中传值
stream >> result ; //向result中写入值
(5)例子
#include <cstdio> #include <iostream> #include <string> #include <cstring> #include <sstream> using namespace std; string s,a; int panduan; int main () { while(getline(cin,s)) //读入一行字符串(连空格也一起读入) { panduan=0; //控制最后一个不输出空格(先判断输出第一个a[0],后就可以输出空格了) stringstream ss(s); while(ss >> a) //输入的时候用while就可以将所有的字符串片段输入。 { if(panduan==1)cout << " "; if(a[0]>='a' && a[0]<='z') a[0]=a[0]-32;//存在以数字开头的情况 cout << a; panduan=1; } cout << endl; } }