输入输出

cin/cout 数据类型自动识别/使用简单,缺点是速度慢。若遇到比较大的数据规模,很大可能被卡。改掉这个习惯!

scanf()和getchar()函数是从输入流缓冲区中读取值的,而不是从键盘终端缓冲区读取。

读取字符时,scanf()以space enter tab 结束一次输入,不会舍弃最后的回车符。(会读取回车符)

getchar()以 enter 结束输入,也不会舍弃最后的回车符。

 1 #include<iostream>
 2 #include<string>
 3 #include<stdio.h>
 4 using namespace std;
 5 int main()
 6 {
 7     char ch1,ch2;
 8     scanf("%c",&ch1);
 9     scanf("%c",&ch2);
10     printf("%d %d
",ch1,ch2);
11     return 0;
12 }
13 
14 #include<iostream>
15 #include<string>
16 #include<stdio.h>
17 using namespace std;
18 int main()
19 {
20     char ch1,ch2;
21     ch1=getchar();
22     ch2=getchar();
23     printf("%d %d
",ch1,ch2);
24     return 0;
25 }
只读入一个字符

gets()只可读入字符串。(char形式)

读取字符串时,scanf()以 space enter tab 结束一次输入。(只读一个单词 回到空格结束读入)

gets()以 enter 结束输入,接受空格,舍弃最后的回车符。(不读取回车符)(读一整行 包括空格)

getline 读入整个string (string形式)(读一整行 包括空格)

No matter how you feel, get up , dress up , show up ,and never give up.
原文地址:https://www.cnblogs.com/Kaike/p/9787232.html