c/c++ 缓冲区的刷新

利用string 对象查看缓冲区的变化,因为每个string对象在输入时会以空格作为分界。

#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值,那么将依次输出这几个值。

而在非循环时,会将空格隔开的几个值依次赋值给要求输入的string变量。

原文地址:https://www.cnblogs.com/little-snake/p/4890651.html