当前位置:网站首页>每次读取一行字符串输入(有待补充)
每次读取一行字符串输入(有待补充)
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,空行以及其他问题(有待补充)
边栏推荐
- Construction and application of super large scale knowledge map of ants
- 【读书会第13期】音视频文件的封装格式
- Control mode of CPU
- 理解双亲委派模式
- The latest ijcai2022 tutorial of "figure neural network: foundation, frontier and application"
- QT with OpenGL (shadow mapping)
- 二舅治好我的精神内耗,也让我火出了B站
- J9 Digital Science Popularization: how does the double consensus of Sui network work?
- How to deal with the website after it is hacked and how to delete batch malicious code
- Implement Gobang game with C language
猜你喜欢

抖音直播监控-循环值守24小时-直播弹幕

JS ATM机输出

窗口函数over
![[unity] mapping 2D coordinates to ID](/img/e8/e6e56ba63dd56009bfabe647f2e3ed.png)
[unity] mapping 2D coordinates to ID

AI briefing how to use loss surfaces a model integration
![[NCTF2019]babyRSA1](/img/c1/52e79b6e40390374d48783725311ba.gif)
[NCTF2019]babyRSA1

Prepare for the interview and stick to the third sentence of the question - Branch sentences!

adb路径不能包含2空格remote couldn‘t create file: Is a directory
![[RoarCTF2019]RSA](/img/0e/8c8371ccf40094e5b03e502d6ae851.png)
[RoarCTF2019]RSA

JS 事件传播 捕获阶段 冒泡阶段 onclick addEventListener
随机推荐
How to deal with the website after it is hacked and how to delete batch malicious code
【zer0pts CTF 2022】 Anti-Fermat
[NCTF2019]babyRSA1
30余年的元宇宙,为我们带来了什么?
[flight control development foundation tutorial 6] crazy shell · open source formation UAV SPI (six axis sensor data acquisition)
二舅治好我的精神内耗,也让我火出了B站
If we were the developer responsible for repairing the collapse of station B that night
元宇宙办公,打工人的终极梦想
北欧岗位制博士申请有多难?
【飞控开发基础教程6】疯壳·开源编队无人机-SPI(六轴传感器数据获取)
Shell(3)
Prepare for the interview and stick to the third sentence of the question - Branch sentences!
窗口函数over
2022 latest Tiktok live broadcast monitoring full set of monitoring (V) product details monitoring
洛谷 P1009 [NOIP1998 普及组] 阶乘之和
Introduction to thesis writing | how to write an academic research paper
Assertion mechanism in test class
MFC提示this application has requested the runtime to terminate it in an unusual way editbox框已经删了还在使用
[GWCTF 2019]BabyRSA1
阿金的思考