区分cin.get()和cin.getline() [英] difference between cin.get() and cin.getline()

查看:371
本文介绍了区分cin.get()和cin.getline()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的新手,我对 get() getline() C ++。

I am new to programming, and I have some questions on get() and getline() functions in C++.

我对这两个函数的理解:

My understanding for the two functions:

getline / code>函数读取整行,并使用Enter键发送的换行符来标记输入结束。 get()函数很像 getline(),而不是读取和丢弃换行符, get()在输入队列中留下该字符。

The getline() function reads a whole line, and using the newline character transmitted by the Enter key to mark the end of input. The get() function is much like getline() but rather than read and discard the newline character, get() leaves that character in the input queue.

我正在阅读的书(C ++ Primer Plus) get() over getline()。我的困惑是,不是 getline()更安全get(),因为它确保结束行'\\\
'
。另一方面, get()只会将字符挂起在输入队列中,从而可能导致问题?

The book(C++ Primer Plus) that I am reading is suggesting using get() over getline(). My confusion is that isn't getline() safer than get() since it makes sure to end line with '\n'. On the other hand, get() will just hangs the character in the input queue, thus potentially causing problem?

推荐答案

有相同数量的优点和缺点,而且实际上取决于您正在阅读的内容: get()在队列中留下分隔符,因此可以将其视为下一个输入的一部分。 getline()舍弃它,所以下一个输入将在它之后。

There are an equivalent number of advantages and drawbacks, and -essentially- all depends on what you are reading: get() leaves the delimiter in the queue thus letting you able to consider it as part of the next input. getline() discards it, so the next input will be just after it.

换行字符从控制台输入,它是非常有意义的丢弃它,但如果我们考虑一个文件的输入,可以使用作为分隔符下一个字段的开始。

If you are talking about the newline character from a console input,it makes perfectly sense to discard it, but if we consider an input from a file, you can use as "delimiter" the beginning of the next field.

什么是好或安全,取决于你在做什么。

What is "good" or "safe" to do, depends on what you are doing.

这篇关于区分cin.get()和cin.getline()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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