C++ 输入函数getline(cin,str) 与cin.getline(str,int)区别
2014-09-01 16:03
477 查看
1. cin.getline()函数
处理数组字符串的,其原型为cin.getline(char * , int),第一个参数为一个char指针,第二个参数为数组字符串长度。
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;
2. 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 *
注:getline(cin,str);处理后还留有结束符在输入流中,故需要使用cin.get();//接受最后一个结束符,才能接受后面得输入值。
处理数组字符串的,其原型为cin.getline(char * , int),第一个参数为一个char指针,第二个参数为数组字符串长度。
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;
2. 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 *
注:getline(cin,str);处理后还留有结束符在输入流中,故需要使用cin.get();//接受最后一个结束符,才能接受后面得输入值。
相关文章推荐
- C++ 输入函数getline(cin,str) 与cin.getline(str,int)区别
- C++ 输入函数getline(cin,str) 与cin.getline(str,int)区别
- C++中几个输入函数的用法和区别(cin、cin.get()、cin.getline()、getline()、gets()、getchar()))
- C++中cin>>、cin.get()、cin.getline()、getline()、gets()输入函数详解
- c++中 cin.get();cin.getline();getline();gets();getchar()等输入函数的小结
- c++中字符输入函数getline、cin.getline区分
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- c++中字符输入函数getline、cin.getline区分
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- cin cin.get() cin.getline() string类成员函数getline(cin,str)使用方法区别与联系
- cin、cin.get()、cin.getline()、getline()、gets()等函数的用法 学C++的时候,这几个输入函数弄的有点迷糊;这里做个小结,为了自己复习,也希望对后来者能有所
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- 【转载】C++中cin、cin.get()、cin.getline()、getline()、gets()函数详解
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- STL中流相关的输入输出符和get函数彻底总结:cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- 字符串输入函数cin.getline()、与cin.get()的区别
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- C++中cin、cin.get()、cin.getline()、getline()、gets()函数详解 .