本文用于练习C++中的各种输入
题目:
不同输入函数的使用场景
#include <iostream> #include <string> using namespace std; int main(int *argc, int **argv) { string str; char a,c[20]; getline(cin, str);//类似cin.getline(c,20); 入口参数为string对象,读取包含空格的一行字符串 cout << str; gets(c);//类似getline(cin, str); 但入口参数为字符数组,读取包含空格的一行字符串 cout << c; a=getchar();//只能读取一个字符 cout << a; return 0; }
#include <iostream> using namespace std; int main(int *argc, int **argv) { char a,b,c[20],d[2][20]; cin >> a >> b>>c;//输入流中遇'\0' '\n' '\t'读取结束 cout << a << " " << b << " " << c << endl; cin.get(a);//入口参数为一个字符,即只能读取一个字符 cout << a << endl; cin.get(c, 20);//入口参数为字符数组,可读取包含空格的一行字符串 19+'\0' cout << c << endl; cin.getline(c, 20);//类似cin.get(c,20); cout << c << endl; cin.getline(c, 20, 'a');//字符读取至'a'结束 cout << c << endl; for (int i = 0; i < 2; i++)//读取二维字符串 cin.getline(d[i], 20); cout << d[0] << " " << d[1] << endl; return 0; }
参考资料:
https://blog.csdn.net/zhanghaotian2011/article/details/8868577