getline的实现(istream& is,string& str) [英] Implementation of getline ( istream& is, string& str )

查看:81
本文介绍了getline的实现(istream& is,string& str)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,如何实现getline(istream,string)?
如何解决具有固定大小的char数组(如getline(char * s,streamsize n))的问题?
他们在使用临时缓冲区并多次调用新char [length]或其他整洁的结构吗?

My Question is very simple, how is getline(istream, string) implemented? How can you solve the problem of having fixed size char arrays like with getline (char* s, streamsize n ) ? Are they using temporary buffers and many calls to new char[length] or another neat structure?

推荐答案

getline(istream& ;, string&)的实现方式是读取一行。没有确定的实现。每个库可能互不相同。

getline(istream&, string&) is implemented in a way that it reads a line. There is no definitive implementation for it; each library probably differs from one another.

可能的实现:

istream& getline(istream& stream, string& str)
{
  char ch;
  str.clear();
  while (stream.get(ch) && ch != '\n')
    str.push_back(ch);
  return stream;
}

这篇关于getline的实现(istream& is,string& str)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆