zoukankan      html  css  js  c++  java
  • c++中字符输入函数getline、cin.getline区分

    1、cin>>s; s能够是:string  s、char s[];

    这个是ostream中的函数。遇到‘ ’(空格) , ' '(换行),就会自己主动结束,因此假设用cin读取字符串,那么这个字符串中不能含有空格和换行。

    cin由于不识别空格和换行,因此假设在输入字符串的时候,在字符開始处输入了空格或换行,没有不论什么影响。

    #include <iostream>
    #include <string>
    #include <stdio.h>
    using namespace std;
    
    
    int main()
    {
        string s;
        int n;
        while(cin>>s)
        {
            cout<<s<<endl;
        }
    }

    2、getline(cin , s , ' ') , s仅仅能是 string s

    getline属于string类的字符读取函数。这个函数的第三个參数能够不写(getling(cin , s))。第三个參数默认是'',这样的情况下假设要结束一个字符串输入。对于有些编译器(VC , VS), 必须得连续输入两个换行。

    对于getline(cin , s , 'z') ,能识别空格和换行

    对于getline仅仅要截止字符(第三个參数)设置得当。能读取多行

    #include <iostream>
    #include <string>
    #include <stdio.h>
    using namespace std;
    
    
    int main()
    {
        string s;
        int n;
        while(getline(cin , s , 'z'))
        {
            cout<<s<<endl;
        }
    }

    3、cin.getline(s , 100 , '/n') 。 仅仅能是 char s[];

    cin.getline属于ostream 。 和getline类似,仅仅是仅仅能对char s[]类型进行读取。除这点外,其它和getline都同样。


    
  • 相关阅读:
    Java设计模式—状态模式
    Java设计模式—备忘录模式
    android AsyncTask介绍
    Android UI线程和非UI线程
    Java设计模式—代理模式
    Java设计模式—命令模式
    <Android 应用 之路> MPAndroidChart~PieChart
    FPGA的EPCS 配置的2种方法 FPGA下载程序的方法(EPCS)
    如何将.sof转换成.jic
    quartus ii工程文件的分析
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7086854.html
Copyright © 2011-2022 走看看