cin对象如何根据用户需要将字符转换为不同类型? [英] How does cin object converts characters to different types as user needes?

查看:49
本文介绍了cin对象如何根据用户需要将字符转换为不同类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: cin 对象是 basic_istream< char> ( istream )的实例时,如何处理不同类型的对象?

How does std::cin object deal with different types while it is an instance of basic_istream<char> (istream)?

推荐答案

std :: basic_istream< CharT,Traits> 对类型为 CharT .它为该输入流提供了相对较低级别的访问和相对较高级别的访问.例如,您可以调用 std :: cin.get()以便从输入流中检索下一个字符.这将始终返回 CharT ,因为这是流中字符的基础类型.但是, basic_istream 还提供了 formatted输入函数,其目的是将字符流解释为某种类型的编码,可能是 int std :: basic_string< CharT,Traits> 或其他内容.因此,虽然流不由 int 组成,但是有一个 operator>> 通过从中连续读取数字来提取 int 值. char 流并将其解释为整数的以10为底的表示形式. operator>> 函数已重载,因此可以用来提取各种不同的类型.

The class std::basic_istream<CharT, Traits> models an input stream of characters of type CharT. It provides both relatively low-level and relatively high-level access to that input stream. You can, for example, call std::cin.get() in order to retrieve the next character from the input stream; this will always return CharT, since that's the underlying type of characters in the stream. However, basic_istream also provides the formatted input functions, whose purpose is to interpret that character stream as an encoding of some type, which could potentially be int, std::basic_string<CharT, Traits>, or something else. Thus, while the stream does not consist of ints, there is an operator>> that extracts an int value by reading digits successively from a char stream and interpreting them as the base-10 representation of an integer. The operator>> function is overloaded so that it can be used to extract various different types.

这篇关于cin对象如何根据用户需要将字符转换为不同类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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