1.每次读入一个单词
#include<iostream> #include<string> using namespace std; int main(){ string word; int n=1; while(cin>>word){ cout<<n<<": "<<word<<endl; n++; } cout<<endl; system("pause"); return 0; }
2.每次读入一行文本
#include<iostream> #include<string> using namespace std; int main(){ string line; int n=1; while(getline(cin,line)){ cout<<n<<": "<<line<<endl; n++; } cout<<endl; system("pause"); return 0; }