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) 的作用是读一行数据吧?
这个函数是遇到回车就中止么?
展开
 我来答
zhao6014
2008-03-10 · 超过12用户采纳过TA的回答
知道答主
回答量:96
采纳率:0%
帮助的人:49.6万
展开全部
从流in里面每次读取一行,遇到\n返回,string s里面不包含\n,也就是getline会把缓冲区里面的\n取出来,并且丢弃掉。
百度网友35fcffa92
2008-03-10 · TA获得超过854个赞
知道小有建树答主
回答量:709
采纳率:0%
帮助的人:791万
展开全部
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

参考资料: MSDN

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式