c++中的getline与cin.getline

  • cin.getline()函数与getline的区别:
    处理数组字符串的,其原型为cin.getline(char*,int),第一个参数为指针,第二个参数为数组字符串长度,还能读入空格以及其他字符。getline(cin,str)函数是处理string类的函数。第二个参数为string类型的变量

  • 实例一:

 #include <iostream>  
 #include <string>
 using namespace std;  
 const int SIZE=20; 
 int main()  
{  
  string str;  
  cout<<"string method:"<<endl;  
  getline(cin,str);  
  cout<<"the string is:"<<endl;  
  cout<<str<<endl;  
}  
  
  • 实例二:
 #include <iostream>
 #include <string>
using namespace std;
const int SIZE=20;
int main()
{
string str;
cout<<"string method:"<<endl;
getline(cin,str);
cout<<"the string is:"<<endl;
cout<<str<<endl;
cin.get();//接受最后一个结束符
char chs[SIZE];
cout<<"char * method:"<<endl;
cin.getline(chs,20);
cout<<"the string is:"<<endl;
cout<<chs<<endl;
return 0;
}
运行结果:
string method:
Hello String
the string is:
Hello String
char * method:
Hello Char *
the string is:
Hello Char *
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值