参考:https://blog.csdn.net/a3192048/article/details/80303547
一般情况下的输入输出
#include <bits/stdc++.h>
using namespace std;
char c;
string s1;
char s2[110];
char g[110][110];
int main(){
// 以字符为例
cin >> c;
cout << c << '\n';
// 读入的数据组数未知的情况下
while (cin >> c){
}
while (scanf("%c", &c) == 1){
}
// 读入string类
cin >> s1;
cout << s1 << '\n';
// 读入字符数组
scanf("%s", s2);
printf("%s\n", s2);
// 读入二维字符数组
int n;
cin >> n;
for (int i = 0; i < n; i++) scanf("%s", g[i]);
for (int i = 0; i < n; i++) printf("%s\n", g[i]);
// int类型 占多少个字符宽度
cin >> n;
printf("%8d %8d\n", n, n);
printf("%-8d %-8d\n", n, n);
// double 类型保留小数点后几位
printf("%.20lf\n", 1.0 / 3);
// 格式化输出的内容,一般建议用printf()搞定,很少用cout
double x = 3.1415926;
cout << x << '\n';
cout << setprecision(5) << x << '\n';
cout << setprecision(4) << x << '\n';
cout << setprecision(3) << x << '\n';
cout << setprecision(2) << x << '\n'; // 3.1
cout << setprecision(1) << x << '\n'; // 3
// setprecision 不计算小数点
// #include <iomanip>
// 与 setw 字段宽度不同的是
// setprecision 的精度设置将保持有效,直到更改为其他值为止
// 流操作符 setw 可用于建立指定宽度的打印区域
// 空格“填充”在前面,所以它被认为是右对齐的
// 左对齐也能搞,麻烦
// 用一回,写一回
int y;
cin >> y;
cout << setw(8) << y << setw(8) << y << '\n';
return 0;
}
输入原理
程序的输入都建有一个缓冲区,即输入缓冲区。每次输入过程是这样的,当一次键盘输入结束时会将输入的数据存入输入缓冲区,而cin函数直接从输入缓冲区中取数据(cin读取数据是从第一个非空白字符开始到下一个空白字符结束)。正因为cin函数是直接从缓冲区取数据的,所以有时候当缓冲区中有残留数据时,cin函数会直接取得这些残留数据而不会请求键盘输入。
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
cout << s << '\n';
cin >> s;
cout << s << '\n';
return 0;
}
由于cin在遇到空格/tab时,就会停止读取,所以如果我在第一次输入时,利用空格隔开两个字符串,那么cin在第一次取的时候,只会读取前一个字符串,到空格结束,此时缓冲区还保留着前面输入的第二个字符串,那么第二次cin就会直接从缓冲区取残留数据,而不会请求输入。
当然对于以上的情况,也有解决的方案,那就是在第二次调用cin>>str之前通过cin.sync()来清空输入缓冲区。(后面有实验截图)
cin >>
根据cin>>sth 中sth的变量类型读取数据,这里变量类型可以为int, float, char, char*, string等诸多类型。这一输入操作,在遇到结束符(Space、Tab、Enter)就结束,且对于结束符,并不保存到变量中。注意:最后一个enter也在缓冲区。
#include <bits/stdc++.h>
using namespace std;
int main(){
string s1, s2;
char c;
cin >> s1 >> s2;
cout << s1 << ' ' << s2 << '\n';
cin.get(c);
cout << (int)c << '\n';
return 0;
}
cin.get( ) 读入一个字符
其中结束符意味着遇到该符号结束字符串读取,默认为enter,读取的字符个数最多为(长度 - 1),因为最后一个为’\0’。要注意的是,cin.get(字符数组名,接收长度,结束符)操作遇到结束符停止读取,但并不会将结束符从缓冲区丢弃
#include <bits/stdc++.h>
using namespace std;
int main(){
char c1, c2;
cin.get(c1);
c2 = cin.get();
cout << c1 << ' ' << c2 << '\n';
return 0;
}
getline(cin, s) 和 cin.getline(s, 110)
#include <bits/stdc++.h>
using namespace std;
string s1;
char s2[110];
int main(){
getline(cin, s1);
cin.getline(s2, 110);
//cin.getline第三个参数表示间隔符,默认为换行符’\n’。读入不需要考虑最后的换行符。
cout << s1 << '\n';
printf("%s\n", s2);
return 0;
}
cin.ignore( )清空整个缓冲区
cin.ignore(1024, '\n');
cin.ignore(); // 用该函数的默认情况,丢弃一个字符,即上次输入结束的回车符
其实该函数最常用的方式是这样的,将第一个参数设的非常大,将第二个参数设为’\n’,这样就可以缓冲区中回车符中的所有残留数据,因为一般情况下前面输入残留的数据是没有用的,所以在进行新一次输入操作前将缓冲区中所有数据清空是比较合理。
#include <bits/stdc++.h>
using namespace std;
int main(){
char c1, c2;
cin >> c1;
cout << c1 << ' ';
cin.ignore();
cin >> c2;
cout << c2 << '\n';
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
char c1, c2;
cin >> c1;
cout << c1 << ' ';
cin.ignore(1024, '\n');
cin >> c2;
cout << c2 << '\n';
return 0;
}
gets( ) 读入字符串 unsafe
http://c.biancheng.net/cpp/html/260.html
C语言gets()函数:从流中读取字符串
#include <bits/stdc++.h>
using namespace std;
char s[110];
int main(){
gets(s);
printf("%s\n", s);
return 0;
}
getchar( ) 读入一个字符
https://www.runoob.com/cprogramming/c-function-getchar.html
从标准输入 stdin 获取一个字符(一个无符号字符
#include <bits/stdc++.h>
using namespace std;
char c;
int main(){
c = getchar();
putchar(c);
puts("");
return 0;
}
scanf( ) 读入字符数组
#include <bits/stdc++.h>
using namespace std;
char s[110];
int main(){
scanf("%s", s);
printf("%s\n", s);
return 0;
}
getchar( ) 读入字符数组
#include <bits/stdc++.h>
using namespace std;
char s[110];
int main(){
int i = 0;
while ((s[i] = getchar()) != '\n') i++;
printf("%s", s); //注意这个地方,我并没有 \n
return 0;
}
get( ) 读入字符数组
#include <bits/stdc++.h>
using namespace std;
char s[110];
int main(){
cin.get(s, 110);
printf("%s\n", s);
return 0;
}
//多行数据读入的时候
#include <bits/stdc++.h>
using namespace std;
char s[110];
int n = 3;
int main(){
while (n--){
cin.get(s, 110);
getchar(); //把行末的回车读进来,否则翻车
printf("%s\n", s);
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
char s[110];
int n;
int main(){
cin >> n;
getchar();
while (n--){
cin.get(s, 110);
getchar(); //把行末的回车读进来,否则翻车
printf("%s\n", s);
}
return 0;
}