C++中getline()的作用什么,工作过程是怎样的?
C++中getline()的作用什么,工作过程是怎样的?如下面程序:#include<iostream>#include<sstream>#include<fstream...
C++中getline()的作用什么,工作过程是怎样的?
如下面程序:
#include<iostream>
#include<sstream>
#include<fstream>
using namespace std;
int main(){
ifstream in("aaa.txt");
for(string s; getline(in, s); ){
int a, sum=0;
for(istringstream sin(s); sin>>a; sum += a);
cout<<sum<<endl;
}
}
getline(in, s) 的作用是读一行数据吧?
这个函数是遇到回车就中止么? 展开
如下面程序:
#include<iostream>
#include<sstream>
#include<fstream>
using namespace std;
int main(){
ifstream in("aaa.txt");
for(string s; getline(in, s); ){
int a, sum=0;
for(istringstream sin(s); sin>>a; sum += a);
cout<<sum<<endl;
}
}
getline(in, s) 的作用是读一行数据吧?
这个函数是遇到回车就中止么? 展开
展开全部
Extract strings from the input stream line-by-line.
template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str
);
template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str,
CharType _Delim
);
Parameters
_Istr
The input stream from which a string is to be extracted.
_Str
The string into which are read the characters from the input stream.
_Delim
The line delimiter.
Return Value
The first function returns getline( _Istr, _Str, _Istr.widen( '\n' ) ).
The second function replaces the sequence controlled by _Str with a sequence of elements extracted from the stream _Istr.
Remarks
In order of testing, extraction stops:
At end of file.
After the function extracts an element that compares equal to delim, in which case the element is neither put back nor appended to the controlled sequence.
After the function extracts str.max_size elements, in which case the function calls setstate(ios_base::failbit).
If the function extracts no elements, it calls setstate(failbit). In any case, it returns _Istr.
Requirements
Header: <string>
See Also
Reference
string::getline
Other Resources
<string> Members
template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str
);
template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str,
CharType _Delim
);
Parameters
_Istr
The input stream from which a string is to be extracted.
_Str
The string into which are read the characters from the input stream.
_Delim
The line delimiter.
Return Value
The first function returns getline( _Istr, _Str, _Istr.widen( '\n' ) ).
The second function replaces the sequence controlled by _Str with a sequence of elements extracted from the stream _Istr.
Remarks
In order of testing, extraction stops:
At end of file.
After the function extracts an element that compares equal to delim, in which case the element is neither put back nor appended to the controlled sequence.
After the function extracts str.max_size elements, in which case the function calls setstate(ios_base::failbit).
If the function extracts no elements, it calls setstate(failbit). In any case, it returns _Istr.
Requirements
Header: <string>
See Also
Reference
string::getline
Other Resources
<string> Members
参考资料: MSDN
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询