c++ :cin的用法

1.cin介绍

cin 是 C++ 的标准输入流对象,cin 主要用于从键盘输入读取数据,其是从缓冲区读取数据,缓冲区为空时,cin等待数据的写入;缓冲区有数据时,cin函数取读取数据。

1-1.cin的几种常见读取方法
cin>>,cin.get(),cin.getline()。

2.cin常见用法详解

2.1. cin>>
(1). cin>>用于连续从键盘读取数据,遇到空格,Tab,Enter会结束输入。cin是智能指针,若缓冲区开始的字符为分隔符时,cin>>忽略并清除这些,直到遇到非分隔符为止;字符读取结束后,缓冲区里的分隔字符留在缓冲区内,cin>>不会对其作出处理。
例子1:

#include <iostream>
#include <string>
using namespace std;
int main(void)
{
 int a,b;
 string c;
 cout <<"输入:";
 cin>>a>>b>>c;
 cout <<"输出:"<<a<<"  "<<b<<"  "<<c<<endl;
 return 0;
}

输入:1【Enter】2【Enter】hello【Enter】
结果:

输出:1  2  hello

例2:

#include <string> 
#include <iostream>
using namespace std;
int main()
{
 string str1;
 cin>>str1;
 cout<<"str1:"<<str1<<endl;
 string str2;
 getline(cin,str2);//输出的str2什么都没有,由此可见,cin>>读取成功后,字符后面的分隔符仍然留在缓冲区的,不做处理。
 cout<<"str2:"<<str2<<endl;
 return 0;
}

输入:hello【Enter】
结果:

str1:hello
str2:

(2). cin>>和cin.operator>>()是一样的。
例子:

#include <iostream>
using namespace std;
int main(void)
{
   int a;
   cin.operator>>(a);
   cout <<a;
   return 0;
}

输入:7
结果如下:

7
--------------------------------
Process exited after 1.884 seconds with return value 0
请按任意键继续. . .

2.2.cin.get()
个人认为cin.get()的存在目的是因为从c移植到c++时,cin.get()相等于c中的getchar(),因为只有cin.get()返回值为int型,和getchar()相对应。

(1). cin.get()从缓冲区中读取包括分隔符在内的当个单个字符或者一行,其不会像cin>>一样忽略分隔符。
例子:

#include <iostream>
using namespace std;
int main()
{
char a;
 char b;
 char array[30];
 a=cin.get();//读取单个字符f
 cin.get(b);//读取第一个f后的Enter到b中
 cin.get();//吃掉第二个f
 cin.get();//吃掉第二个f后的Enter
 cin.get(array,30);//利用数组(必须是char数组类型)读入一行
 cout<<"a:"<<a<<"  b:"<<b<<"c:"<<array<<endl;//在这里输出由于b读的是换行符 ,所以换行了。
 cout <<"******";
 return 0;
}

输入f[Enter]f[Enter]hello[Enter]
结果:

a:f  b:
c:hello
******

(2). cin.get()并非只能如上面的方法所用,以下列出cin.get()的几种常见重载形式。(注意一下给定空间大小参数的函数的注释)。如果第三个空间大小参数给的是a(这里假设给出的第二个参数空间大小小于输入的字符长度),那么cin.get ()会将剩下的字符串保留在缓冲区里,后面的cin可以读取。
例子:

#include<iosream>
using namespace std;
int main()
{
 char a,b,c;
 char array1[30];
 char array2[30];
 a=cin.get();//无参
 cin.get();  //读入缓冲区里的分隔字符
 cin.get(b);//一个参数
 cin.get();
 cin.get(array1,3);//两个参数,第二个参数为目标空间大小,这里给了3,但是结果会输出两个,因为还有一个系统会自动加上'\0'。
 cin.get(array2,20,a);//三个参数,第三个为读取结束的字符。
 cout<<"a="<<a<<",b="<<b<<",array1="<<array1<<",array2="<<array2<<endl;
 return 0;
}

输入:a【Enter】b【Enter】hello【Enter】scalar【Enter】
结果:

a=a,b=b,array1=he,array2=llo
sc

(3). 在必要的时候使用其无参数的重载函数一般用来吃掉缓冲区内无用的字符(如Enter等)。
(例子可以从上述几个代码中可以看出。)

2.3. cin.getline()
(1). cin.getline()用于从输入设备键盘读取字符串,并以指定的结束符结束。当输入超长时,
直接上代码啦:

#include <string> 
#include <iostream>
using namespace std;
int main()
{
    char array1[30];
    char array2[30];
    cin.getline(array1,30);//默认换行符结束 
    cin.getline(array2,30,'e');//以第三个参数为结束符 
    cout<<"array1="<<array1<<",array2="<<array2<<endl;
 return 0;
}

输入:hello【空格】hello【Enter】your【空格】name【Enter】

结果:

array1=hello hello,array2=your nam

3. cin.get ()和cin.getline()的不同点

cin.get() cin.getline()
遇到[enter]时会结束目前输入,不删除Enter 当遇到[enter]时会结束当前输入,删除Enter
超长时,函数会将剩下的字符串留在缓冲区内 超长时,会引起cin函数错误,使得后面的cin操作不再执行
当遇到给定的结束符时会结束输入,但不会删除缓冲区中的结束符 当遇到给定的结束符时会结束输入,并把结束符从缓冲区中删除

第三部分就不上码啦,自个阔以动手试试哈!!!

emm~~如果有说错的,欢迎大佬指正啦。嘿咻嘿咻,溜了哈!

发布了30 篇原创文章 · 获赞 6 · 访问量 1001

猜你喜欢

转载自blog.csdn.net/weixin_43677405/article/details/99877696