可读空格:
cin.get();
cin.getline();
gets();
getline();
前三istring流 最后一个string流
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define bug(x) cout<<"#"<<x<<endl;
#define bug2(a,b) cout<<"#"<<a<<" "<<"#"<<b;
void test1()
{
/*
gets();
只能读C语言的字符串(istream流)
可以读空格,遇到回车读入结束
可以读二维的
*/
char a[20];
gets(a);
bug(a);
char b[20][20];
for(int i=1;i<=3;i++)
gets(b[i]);
for(int i=1;i<=3;i++)
bug(b[i]);
}
void test2()
{
/*
cin.get(str,n);
(1)
istring流
可读空格
在读字符串上理解为和cin.getline()一样
(2)
读字符 输入一行字符串,只读一个
*/
char s[20];
cin.get(s,20,'s');
bug(s);
char c=cin.get();
bug(c);
}
void test3()
{
/*
cin.getline(str,n,bj);
是 istring流
可以读空格
第二个参数定义了读几个字符,可以少于这个数字,但不能大于!!
第三个参数定义了读到哪个字符停止,默认回车'\n'
可读二维
*/
char a[20];
cin.getline(a,20);
bug(a);
char b[5][20];
for(int i=1;i<=3;i++)
cin.getline(b[i],5);
for(int i=1;i<=3;i++)
bug(b[i]);
}
void test4()
{
/*
getline(cin,string)
stirng 流
可读空格
不可读二维
*/
string s;
getline(cin,s);
bug(s);
}
void test5()
{
/*
回车也吸收
*/
char x;
x=getchar();
bug(x);
x=getchar();
bug(x);
}
int main()
{
//test1();
//test2();
//test3();
//test4();
test5();
return 0;
}