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

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c/c++ 缓冲区的刷新

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/223212

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午12:00
下一篇 2023年2月13日 下午12:00

相关推荐