当前位置:网站首页>每次读取一行字符串输入(有待补充)
每次读取一行字符串输入(有待补充)
2022-07-27 21:36:00 【叽里呱啦呱】
c++中的istream类中的类cin提供了面向行的类成员函数:geiline()和get().
区别:两个函数读取到换行符时都会停止,然而geiline()会丢弃换行符而get()会保留在输入序列中
1,geiline()通过换行符来确定结尾,使用方式是cin.geiline(Arrayname,size),但是如果定义的数组空间只有30的话则该函数最多读取29个字符并自动在结尾处添加空字符(如果这行包含的字符不足29个getline()成员函数还可接受第三个可选参数),geiling()函数在读取指定数目的字符或者换行符时才会停止读取get()也一样。getline()每次只读取一行,通过换行符来确定行尾,但不保存换行符,在储存字符串时他会用空字符来代替换行符。
2,get()
get()不会丢弃换行符但是当连续调用它的时候会出现一种情况
#include<iostream>
using namespace std;
int main() {
char ammn[30] = {};
char ammr[30]={};
cin.get(ammn, 30);
cin.get(ammr, 30);
cout << ammn;
cout<<ammr;
return 0;
}由于第一次调用后换行符留在了输入队列中,因此第二次调用后看到的第一个字符就是换行符,因此get()以为已经到达了行尾而没用发现任何可读取的内容,如果不借助帮助,get()将不能跨过该换行符,使用不带任何参数的cin.get()调用可读取下一个字符。
#include<iostream>
using namespace std;
int main() {
char ammn[30] = {};
char ammr[30]={};
cin.get(ammn, 30);
cin.get();
cin.get(ammr, 30);
cout<<ammr;
cout << ammn;
return 0;
}当然还有一种get()方法可以使两个类成员函数拼接起来与以上效果相同,
#include<iostream>
using namespace std;
int main() {
char ammn[30] = {};
char ammr[30]={};
cin.get(ammn, 30).get();
cin.get(ammr, 30);
cout << ammn;
cout<<ammr;
return 0;
}当然getline()也由此用法如
#include<iostream>
using namespace std;
int main() {
char ammn[30] = {};
char ammr[30] = {};
cin.getline(ammn,30).getline(ammr,30);
cout << ammn;
cout<<ammr;
return 0;
}注释:c++允许函数有多个版本,但条件是这些版本的参数列表不同,如带参数的get()与get(),编译器就可以辨别出前者是读取一行字符串,而后者则是读取一个字符
注释:为什么使用get()而不是getline()呢?因为get()可以检查出读取停止是因为数组满了还是因为读取到了换行符。
3,空行以及其他问题(有待补充)
边栏推荐
- 窗口函数over
- MapReduce (III)
- ESP8266-----MQTT云下设备上云
- The 4-hour order exceeds 20000+, claiming to be "the most luxurious in a million". Is the domestic brand floating?
- 传奇服中怎么刷装备
- [NCTF2019]babyRSA1
- 4小时定单破20000+,自称“百万内最豪华”,国产品牌飘了?
- Latex常用总结(2):输入矩阵(输入矩阵、对角阵、方程组等)
- XSS Payload 学习浏览器解码
- Legendary server: what must be modified when the GOM geem2 engine is updated?
猜你喜欢
随机推荐
服务器开放敏感端口
Esp8266----- mqtt cloud device on cloud
(12) 51 Single Chip Microcomputer -- use DS18B20 to measure the outdoor temperature in Gongjiang West
永州二恶英实验室建设细节查看
require、loadfile、dofile、load、loadstring
Yuanuniverse office, the ultimate dream of migrant workers
Unity implements simple Sketchpad drawing function (notes)
Window function over
JS ATM output
[actf freshmen 2020] crypto AES
Use a grayscale filter
北欧岗位制博士申请有多难?
[NCTF2019]babyRSA1
抖音直播监控-循环值守24小时-直播弹幕
[Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring
Legendary Internet Setup tutorial with graphic explanation - GOM engine
[NCTF2019]babyRSA1
It was dog days, but I was scared out of a cold sweat: how far is the hidden danger of driving safety from us?
传奇服务端:GOM GeeM2引擎更新时必须要修改哪些地方?
【C语言】字符串逆序(递归实现)


![[NCTF2019]babyRSA1](/img/c1/52e79b6e40390374d48783725311ba.gif)





