c++中cin对象中getline()方法和get()方法的区分

来源:互联网 发布:网站域名申请 编辑:程序博客网 时间:2024/06/03 17:05

getline 和get的区别:

getline 每次读取一行,舍弃最后的换行符,下一个getline来的时候,直接读取下一行!
get每次读取一行,但是将每一行最后的换行符保留在输入队列中!下一个get来的时候,先读取输入队列中的换行符!

#include <iostream>using namespace std;int main(){    const int Size=20;    char s1[Size];    char s2[Size];    cout<<"Enter the s1"<<endl;    cin.get(s1,Size);    cout<<"Enter the s2"<<endl;    cin.get(s2,Size);    cout<<"s1:"<<s1<<endl;    cout<<"s2:"<<s2<<endl;    return 0;}


#include <iostream>using namespace std;int main(){    const int Size=20;    char s1[Size];    char s2[Size];    cout<<"Enter the s1"<<endl;    cin.get(s1,Size);    cout<<"Enter the s2"<<endl;    cin.get(s2,Size);    cout<<"s1:"<<s1<<endl;    cout<<"s2:"<<s2<<endl;    return 0;}



0 0
原创粉丝点击