当前位置:网站首页>每次读取一行字符串输入(有待补充)
每次读取一行字符串输入(有待补充)
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,空行以及其他问题(有待补充)
边栏推荐
- 14、 C pointer explanation (IV): pointer of pointer
- What foundation does Yolo need? How to learn Yolo?
- 阿金的思考
- TCP sticking and unpacking problem + Solution
- 物联网有助于应对气候变化的 3 种方式
- Latex常用总结(2):输入矩阵(输入矩阵、对角阵、方程组等)
- 服务器开放敏感端口
- Glory launched a number of products at the same time. The price of notebook magicbook V 14 starts from 6199 yuan
- C# 事件相关的练习代码。
- [Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring
猜你喜欢

Latex中如何加粗字体 & 如何打出圆圈序号

(12) 51 Single Chip Microcomputer -- use DS18B20 to measure the outdoor temperature in Gongjiang West

MFC提示this application has requested the runtime to terminate it in an unusual way editbox框已经删了还在使用
![[GWCTF 2019]枯燥的抽奖](/img/ea/34567ad06fae524bd685a26a6d4899.png)
[GWCTF 2019]枯燥的抽奖

BUUCTF-RSA roll
![[极客大挑战 2019]RCE ME](/img/ff/aff58f40f2051f7415d1e16517f824.png)
[极客大挑战 2019]RCE ME

【开发教程9】疯壳·开源蓝牙心率防水运动手环-心率监测

泵站远程监控

liux常用命令(查看及其开放防火墙端口号+查看及其杀死进程)

『百日百题 · 基础篇』备战面试,坚持刷题 第三话——分支语句!
随机推荐
(12) 51 Single Chip Microcomputer -- use DS18B20 to measure the outdoor temperature in Gongjiang West
JS 事件传播 捕获阶段 冒泡阶段 onclick addEventListener
What are the software operation and maintenance monitoring?
[RoarCTF2019]babyRSA威尔逊定理
[MRCTF2020]babyRSA
What foundation does Yolo need? How to learn Yolo?
Legendary Internet Setup tutorial with graphic explanation - GOM engine
Implement Gobang game with C language
The interviewer asked the thread safe list, and I'm not afraid after reading it!
Yuanuniverse office, the ultimate dream of migrant workers
解密 OOM 崩溃下降 90% 的秘密~
Senior how to determine the standard of software test completion
【C语言】字符串逆序(递归实现)
《数字经济 科技向善》大咖对谈干货来啦
[sel object of Objective-C language]
Latex common summary (2): input matrix (input matrix, diagonal matrix, equations, etc.)
[actf freshmen 2020] crypto AES
泵站远程监控
数据中台的那些“经验与陷阱”
Construction and application of super large scale knowledge map of ants