利用string 對象查看緩衝區的變化,由於每一個string對象在輸入時會以空格做爲分界。ios
#include<iostream> #include<string> using namespace std; int main() { string s1; string s2; string s3; cout<< "this is a while circle, and it's different with getline():"<<endl; string word; while( cin>>word ) { cout<<word<<endl; if( word == "exit" ) break; } cout<<"first try; input a char, then output; three times:"<<endl; cin>>s1; cout<<s1<<endl; cin>>s2; cout<<s2<<endl; cin>>s3; cout<<s3<<endl; cout<<"second try; input three char once, then output:"<<endl; cin>>s1>>s2>>s3; cout<<s1<<endl; cout<<"s2:"<<s2<<endl; cout<<"s3:"<<s3<<endl; cout<<"s1:"<<s1<<endl; return 0; }
在循環時,若是輸入的是用空格隔開的幾個string值,那麼將依次輸出這幾個值。this
而在非循環時,會將空格隔開的幾個值依次賦值給要求輸入的string變量。spa