C++ 各种输入方式的整理

cin.get() OJ中请不要使用 测试用例需要完善

cin.get() 这里很多地方不严谨,仅是我个人的随笔,请勿当真

cin是在 <iostream> 中定义的对象,iostream中继承了输入流 <istream>
所以 .get() 函数的定义在 <istream> 中可以找到如下表述:

//single character (1)	
		int get();
		istream& get (char& c);
//c-string (2)	
		istream& get (char* s, streamsize n);
		istream& get (char* s, streamsize n, char delim);
//stream buffer (3)	
		istream& get (streambuf& sb);
		istream& get (streambuf& sb, char delim);

实际上对于cin.get()函数,输入流对于每一个变量会分配一个副本?因为换一个变量就又是从头读取了?

cin.get()获取单个字符测试(需要完善)

get函数输入只会获取一个字符,对于输入流中每一个字符都会挨个读取,没有读取完的内容需要继续读取,无法忽略。
不忽视空格和换行

代码内容

	char char1,char2;
	char str1[256];
	cout << "cin.get(char1) 输入两个字符:" << endl; cin.get(char1);
	cout << "cin.get(char1) 结果1:"<<char1 << endl;
	cout << "cin.get(char1) 还有一个字符未处理:" << endl; cin.get(char1);
	cout << "cin.get(char1) 结果2:" << char1 << endl;
	cout << "cin.get(char1) 上一行输入的是“ char char \\n”,所以这里的get会get到一个换行" << endl; cin.get(char1);
	cout << "cin.get(char1) 结果(换行):" << char1 << endl;

控制台输出

cin.get(char1) 输入两个字符:
ab
cin.get(char1) 结果1:a
cin.get(char1) 还有一个字符未处理:
cin.get(char1) 结果2:b
cin.get(char1) 上一行输入的是“ char char \n”,所以这里的get会get到一个换行
cin.get(char1) 结果(换行):

cin.get()获取字符串测试(需要完善)

本质上还是cin.get()单个字符的多次运算?反正使用起来很不方便。
代码内容

	char str1[6];
	cout << "cin.get(str1,6)最多支持五个字符,第六个需要留给换行" << endl;
	cout << "cin.get(str1,6) 输入字符:" << endl; cin.get(str1,6);
	cout << "cin.get(str1,6) 结果:" << str1 << endl << endl;

	cout << "当输入的字符数量(含换行符)在数组大小内" << endl;
	cout << "cin.get(str1,6) 输入字符:" << endl; cin.get(str1, 6);
	cout << "cin.get(str1,6) 结果:" << str1 << endl << endl;

控制台输出

cin.get(str1,6)最多支持五个字符,第六个需要留给换行
cin.get(str1,6) 输入字符:
abcdefg hijk
cin.get(str1,6) 结果:abcde

当输入的字符数量(含换行符)在数组大小内
cin.get(str1,6) 输入字符:
cin.get(str1,6) 结果:fg hi

cin.getline() 尚未进行测试,个人认为没有<string>中重载的getline()函数好用

用了C++的String不会还有人想用C风格字符串吧,不会吧不会吧
.getline() 函数的定义在 <istream> 中可以找到如下表述:

istream& getline (char* s, streamsize n );					//形式1
istream& getline (char* s, streamsize n, char delim );		//形式2

对于官方文档说明的个人翻译

Extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character).

从流中以“无格式化输入”的形式获取字符,并将其存入s中(C风格字符串);直到提取到字符是一个分隔符(delimiting character)或者n大小的字符已经被写入到s中(包含终止的空字符’\0’)。

The delimiting character is the newline character (’\n’) for the first form, and delim for the second: when found in the input sequence, it is extracted from the input sequence, but discarded and not written to s.

分隔符(delimiting character)对于形式1而言就是新一行的’\n’字符,对于形式2而言是 delim 字符。
当在输入序列中找到分隔符时,将从输入序列中提取它,但是将其丢弃并且不写入s中。

The failbit flag is set if the function extracts no characters, or if the delimiting character is not found once (n-1) characters have already been written to s. Note that if the character that follows those (n-1) characters in the input sequence is precisely the delimiting character, it is also extracted and the failbit flag is not set (the extracted sequence was exactly n characters long).

说了何时会有failbit被设置(略)

A null character (’\0’) is automatically appended to the written sequence if n is greater than zero, even if an empty string is extracted.

如果n大于零,即使提取了一个空字符串,也会自动将一个空字符(’\ 0’)附加到写入的序列中。

Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true). Then (if good), it extracts characters from its associated stream buffer object as if calling its member functions sbumpc or sgetc, and finally destroys the sentry object before returning.

getchar()函数内部如何实现读取机制(略)

The number of characters successfully read and stored by this function can be accessed by calling member gcount.

可以通过调用成员gcount来访问此函数成功读取和存储的字符数。(没用过,下一个)

This function is overloaded for string objects in header <string>: See getline(string).

这个函数在<string>中被重载了,去看看<string>中的getline函数吧**(强推)**

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值