C++里getline()、get()、cin、getchar区别

目录

描述

C++代码


描述

1.cin>>

1)最常见的是获取输入的一个字符或数字,如

int a,b;

cin>>a>>b;

注意:cin>>会自动过滤掉不可见字符(如空格 回车 tab等)。若不想过滤掉空白字符,可以用noskipws流进行控制。

如下程序,没有过滤掉不可见字符,输入的空格字符存入了input[1]中,也可输出。

2)获取输入的字符串,可以用数组或string类型。如

char a[20];

cin>>a;

cout<<a<<endl;

或者string类型:

string s;

cin>>s;

cout<<s<<endl;

注意:遇到空格、回车等会结束获取输入的字符串,后面的字符串会过滤掉(存放在输入流中)。如果后面还需要输入字符串,则会从前面存放的字符串开始获取。

遇空格、回车等结束。

po存放在了string中。

 

2.cin.get()

1)cin.get(字符变量名),用来接收字符,只获取一个字符,可以接收空格,遇回车结束

2)cin.get(数组名,接收字符数目),用来接收字符串,可以接收空格,遇回车结束。

注意:数组的最后一个字符会是‘\0’,设接收字符数目为n,如果输入的字符串大于等于n,则实际接收到的输入是字符串的前面n-1个字符,包括空格(不包括回车,遇到回车就结束了),会自动在后面增加一个‘\0’。

3)cin.get(),没有参数,主要用于舍弃输入流中不需要的字符,或者舍弃回车,即舍弃输入流中的一个字符。

没有用cin.get(),则后面的s会从输入流中继续读入

有cin.get(),则h后面的s被省略了。

 

3.cin.getline()

实际是cin.getline(接收字符串到m,接收个数n,结束字符)。接收一个字符串,可以接收空格等,最后一个字符为‘\0’。结束符可以通过设置第三个参数自己设置,默认是回车。m不能为string类型。

注意:实际接收到的要比n少一个,因为最后一个字符为'\0'。

 

4.getline()

用于string类的。使用需包含头文件#include<string>。getline(cin,string s),接收一个字符串,可以接收空格、回车等

与cin.getline()的区别:1.cin.getline()接收输入字符串的是数组,getline()是string类型。

                                    2.cin.getline()可以接收空格,但不能接收回车;getline()可以接收空格和回车

                                    3.cin.getline()会在数组结尾是'\0',getline()不会

 

5.gets()

gets(m)用于string类的,需包含#include<string>。可以接收空格,遇回车结束。可用于多维数组。

6.getchar()

 m=getchar(),需包含#include<string>

C++代码

#include <iostream>
#include <string>

using namespace std;

//关于cin cin.getline cin.get getline gets getchar 的用法实例

void main(int argc, char* argv[])
{
    //1、cin>>
    //method one, 也就是最常用的方法 输入一个数字
    cout << "Test cin>> 用法1:" << endl;
    int a,b;
    cout << "input two integer:" << endl;
    cin >> a >> b;
    cout << "SUM =" << a + b << "\n" << endl;
    //method two,输入一个字符串,遇到“空格 回车 Tab”都结束
    cout << "Test cin>>用法2:" << endl;
    char array[10];
    cout << "input a char array:" << endl;
    cin >> array;
    cout << array << "\n" << endl;

    //2、cin.get()
    //one cin.get(字符变量名) 可以用来接收字符
    cout << "Test cin.get(字符变量名):" << endl;
    char ch;
    char cch;
    cout << "Input a char:" << endl;
    ch = cin.get(); //把之前输入的回车符号滤去
    cch = cin.get(); //or cin.get(ch);
    cout << cch << "\n" << endl;
    //two cin.get(字符数组,接收的字符数) 用来接收一行字符串可以接收空格
    cout << "Test cin.get(字符数组,接收的字符数):" << endl;
    char array1[20];
    cout << "Input a char array:" << endl;
    ch = cin.get(); //把之前输入的回车符号滤去
    cin.get(array1,10);
    cout << array1 << "\n" << endl;
    //注:cin.get(无参数)主要用来舍弃输入流中不需要的字符 或者舍弃回车
    //从而弥补了cin.get(字符数组,接收的字符数)的不足


    //3、cin.getline(cin,str)  接收一个字符串 可以接收空格
    cout << "Test cin.getline() 的用法:" << endl;
    char array2[20];
    cout << "Input a char array:" << endl;
    ch = cin.get(); //把之前输入的回车符号滤去
    cin.getline(array2,20);
    cout << array2 << "\n" << endl;
    //实际上cin.get(字符数组,接收的字符数) 和cin.getline(字符数组,接收的字符数)
    //有三个参数cin.getline(字符数组,接收字符数,结束字符) 第三个参数默认是'\0'
    //多维数组中也经常用到cin.getline(字符数组,接收的字符数)的用法
    cout << "cin.get(字符数组,接收的字符数) is used in multidimensional array:" << endl;
    char array3[3][10];
    for (int i = 0; i < 3; i ++)
    {
        cout << "请输入第" << i+1 << "行的字符串:" << endl;
        cin.getline(array3[i],10);
    }
    for (int j = 0; j < 3; j ++)
    {
        cout << "第" << j+1 << "行:" << array3[j] << endl;
    }

    //4、getline(cin,str)的用法 接收一个可以包含空格的字符串(这儿是string类型的) 需要包含头文件#include <string>
    //getline(cin,str)是string流不是i/o流
    cout << "Test getline(cin,str):" << endl;
    string str;
    cout << "Input a string:" << endl;
    //ch = cin.get(); //把之前输入的回车符号滤去
    getline(cin,str);
    cout << str << "\n" << endl;

    //5、gets(char *) 接收一个可以包含空格的字符串 需要包含头文件#include <string>
    cout << "Test gets(char *)的用法" << endl;
    char array4[20];
    cout << "input a char array:" << endl;
    ch = cin.get(); //把之前输入的回车符号滤去
    gets(array4);
    //The gets function reads a line from the standard input stream stdin and stores it in buffer.
    //The line consists of all characters up to and including the first newline character ('\n').
    //gets then replaces the newline character with a null character ('\0') before returning the line
    cout << array4 << "\n" << endl;
    //gets(char *)也可以用在多维数组里面 跟cin.getline()用法类似

    //6、getchar(无参数) 接收一个字符 需要包含头文件#include <string>
    cout << "Test getchar(无参数)的用法:" << endl;
    char ch1;
    cout << "input a char:" << endl;
    ch1 = getchar(); // 不能写成getchar(ch1);
    cout << ch1 << "\n" << endl;
    //getchar()是C的函数 C++是兼容C 所以也可以使用 但尽量不用或少用
}

参考资料:

http://blog.csdn.net/jiangxinnju/article/details/20492453

https://www.cnblogs.com/shrimp-can/p/5241544.html

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值