istringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来。
添加头文件 #include<sstream>
1 #include<iostream> 2 #include<sstream> 3 #include<stdio.h> 4 using namespace std; 5 6 int main() 7 { 8 char str[100],ss[100],w[100]; 9 while(1) 10 { 11 gets(ss); 12 istringstream str(ss); //构造一个字符串输入流 13 while(str>>w) //从输入流中 读入一个字符串 遇到空格是结束 14 printf("%s ",w); 15 } 16 return 0; 17 }