getline()函数yyds!震惊,取代了cin的函数竟是它!!!

在QT进行cin(全网最清晰教程) 全网最清晰教程 一、配置工作 配置.pro文件,在CONFIG 中添加 console,添加后注意进行“构建->执行qmake” 否则配置无效。 二、运行设置,勾选Run in terminal 三、mian.cpp中添加头文件 #include <iostream> #include <cmath> using namespace std; int main() { double a,b; cin >> a >> b; 阅读详情

起因:

虽然c++中使用cin读取数据十分的方便,但在某些情况下还是有所局限,因为当 cin 读取数据时,它会传递并忽略任何前导白色空格字符(空格、制表符或换行符)一旦它接触到第一个非空格字符即开始阅读,当它读取到下一个空白字符时,它将停止读取。

解决方法

因此我们可以使用getline()函数,此函数可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中。

getline(cin, inputLine);

其中 cin 是正在读取的输入流,而 inputLine 是接收输入字符串的 string 变量的名称。

getline()的原型:
头文件:#include

istream& getline ( istream &is , string &str , char delim );

其中,istream &is 表示一个输入流,譬如cin;

string&str表示把从输入流读入的字符串存放在这个字符串中(可以自己随便命名,str什么的都可以);

char delim表示遇到这个字符停止读入,在不设置的情况下系统默认该字符为’\n’,也就是回车换行符(遇到回车停止读入)。

注意

很多同学可能会想到使用while(getline(cin,line))语句控制输入,

但注意这里默认回车符停止读入,按Ctrl+Z或键入EOF回车才可退出循环。

在这个语句中,首先getline从标准输入设备上读入字符,然后返回给输入流cin,注意了,是cin,所以while判断语句的真实判断对象是cin,也就是判断当前是否存在有效的输入流。在这种情况下,我想只要你的电脑不中毒不发神经你的输入流怎么会没有效?
所以这种情况下不管你怎么输入都跳不出循环,因为你的输入流有效,跳不出循环。
最好在后面加个逗号,并加一个判别式。

Qt基础-如何才能使用cout和cin Qt使用cout和cin 阅读详情

相关推荐

利用Arcgis发布wmts服务

使用发布 GIS 服务连接连接到 ArcGIS Server 站点后,可以将各种 GIS 资源(如地图文档、数据库以及服务定义)发布到服务器上。此外,还可以添加、删除、启动和停止服务。使用管理 GIS 服务器连接到 ArcGIS Server 站点后,可以编辑各种服务器属性,例如配置存储位置、集群配置、参与到该站点中的计算机列表以及管理站点的数据存储。通过使用 GIS 服务连接建立连接后,可以查看并使用发布到该站点的服务。但不能编辑服务器或服务属性、发布服务,也不能添加、删除、启动、停止或暂停服务。

ghgfsasxda的博客 2395

C++基础特性

前言 1.C++很难学?——是的 C++整体学习难度和Java整体学习难度是一样的 不要想着精通C++。 一、命名空间 命名空间是为了防止冲突,有时候全局变量定义了好多,这时候会导致命名冲突,这时候的命名空间的作用就是给文件定了一个作用域,在这里就可以限定变量,函数的名称。 1.什么是命名空间? 命名空间就是namespace 后面跟的一串字符。这一串字符就叫做命名空间。命名空间的名字可以随便起。 代码举例定义: 命名空间里的变量a,和函数Add是独立的。和其他命名空间隔开的。 namespace N1//

Code Writers 979

使用Docker搭建MaxKB智能体平台

【代码】使用Docker搭建MaxKB智能体平台。

janthinasnail的博客 711

c++编程小总结(长期不定时更新)

1.cin与getline()的使用 1)C++编程语言互换流中的标准输入流,需要iostream.h支持,即开始的时候,要有#include &amp;amp;lt;iostream.h&amp;amp;gt;(新版本编译器为#include )。 cin代表标准输入设备,使用提取运算符 “&amp;amp;gt;&amp;amp;gt;” 从设备键盘取得数据,送到输入流对象cin中,然后送到内存。使用cin可以获得多个从键盘的输入值,其具体使用格式如下: ...

elinice 259

2024从零开始使用VSCode编程(静态页面制作篇)HTML+CSS+JS阶段

VSCode第一个前端静态页面的编写

ssrsrrn的博客 9591

jane street market prediction 冠军方案 奇巧淫技与topline链接整理(3/3)

目录前言将income 作为loss(优化目标)读取数据,定义优化目标模型训练与微调提交结果多目标学习Topline整理AE+MLP (rank10)Current 17th solution: Ensembles of deep (49 layer) MLPsmixup augmentation: a way to learn from trades with weight=0 (rank4)My (perhaps) over-complicated LSTM solutionCurrent 62nd .

weixin_51484067的博客 2641

c++中的cin和getline()函数

cin 输入接触到第一个非空格字符时开始阅读,当遇到下一个空格字符(空格、制表符、换行符)时就会停止读取, cin 输入接触到第一个非空格字符时开始阅读:当遇到下一个空格字符(空格、制表符、换行符)时就会停止读取: 为了避免cin对于空白符的忽略问题,可以用getline函数:getline函数可以读取整行,包括前面和中间的空格,并将其存储在字符串对象中。在头文件#include中,是普通函数,有四种重载形式。 读取的istream是作为参数 is 传进函数的,读取的字符串保存在s

JYHZZ的博客 9803

目标检测旋转增强源码带方向

解法1: import cv2 import math import numpy as np import os def get_degree(p1,p0): aaa = math.degrees(math.atan2((p1[1] - p0[1]), (p1[0] - p0[0]))) if aaa < 0: aaa = 360 + aaa aaa = (aaa + 90) if aaa > 360: aa...

jacke121的专栏 2086

[转]c++: fstream文件操作及对二进制文件的操作

 要写一些简单的日志到文本文件中,参考了以下两篇文章:ifstream 和 ofstream( 引用)C++标准库 之 iostream库的学习笔记(二)fstream库以及ofstream类的使用ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I

笑风生 7788

EtherCAT ROS2 入门教程(一)

本文介绍了EtherCAT驱动与ROS2的集成方案,主要包括三部分内容:1)在Ubuntu 22.04上安装IgH EtherCAT主站的详细步骤,包括内核配置、源码编译和网络适配器设置;2)构建ethercat_driver_ros2,通过ros2_control描述文件配置硬件接口和EtherCAT从站模块插件;3)使用方法,包括启动EtherCAT主站和运行配置包。该方案支持通过参数文件定义硬件接口,并提供通用和专用两种EtherCAT模块插件,便于开发基于EtherCAT的工业应用。

机器人控制与仿真应用 1531

黑猫带你学eMMC协议第9篇:eMMC初始化过程(initial)

本文依据eMMC JEDEC5.1及个人工作经验整理而成,如有错误请留言。 文章为个人辛苦整理,付费内容,禁止私自转载。 文章所在专栏:《黑猫带你学:eMMC协议详解》 1 什么是emmc的初始化 对于小白来说,可能不太明白什么是emmc初始化。举个栗子:当你拿到一个电脑,接上电源,是不是就立马可以用了呢?并不是吧,你还需要去开机,等待电脑启动,配置完各种参数,等出现了电脑桌面,你才能用。那么同样,对于emmc芯片来说,也不是提供电源就能用了,需要输入各种命令后,让其进入到tran state后,才能正.

黑猫奥利奥的博客 6742

数据输入之cin、get()、getline()

cin:只能输入一个单词 getline():每次读取一行字符串输入,它通过回车键输入的换行符来确定输入结尾。getline()成员函数在读取到指定数目的字符或遇到换行符时停止读取,并丢弃换行符 get():get()用法同getline(),但是get()不丢弃换行符。 使用cin.get()可读取下一个字符  ...

weifu的qt学习之路 910

AgentScope 2.0 扩展开发完全指南:从中间件到模型适配器的深度实践

本文系统讲解 AgentScope 2.0 框架的五大扩展机制,涵盖中间件系统的洋葱模型与钩子设计、ChatModelBase 基类与多供应商适配器开发、工作空间(本地/Docker/E2B)的抽象接口与生命周期管理、凭据工厂与 MCP 协议集成、以及工具协议与存储后端扩展。文中提供完整的架构图、代码示例与故障排查指南,帮助开发者快速掌握 AgentScope 的插件化扩展体系。

program哲学 298

C++重温笔记(十): C++输入输出流

1. 写在前面 c++在线编译工具,可快速进行实验: https://www.bejson.com/runcode/cpp920/ 这段时间打算重新把c++捡起来, 实习给我的一个体会就是算法工程师是去解决实际问题的,所以呢,不能被算法或者工程局限住,应时刻提高解决问题的能力,在这个过程中,我发现cpp很重要, 正好这段时间也在接触些c++开发相关的任务,所有想借这个机会把c++重新学习一遍。 在推荐领域, 目前我接触到的算法模型方面主要是基于Python, 而线上的服务全是c++(算法侧, 业务那边基本上

Miracle8070 2832

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

lqb日志03

一只小蒟蒻备考蓝桥杯的日志。

m0_73354184的博客 1033

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

C++的string类

/ 删除从 pos 处开始的 n 个字符。// str.find_first_of() 方法在字符串str中从指定位置开始向后(默认为索引 0 处)查找参数中任何一个字符首次出现的位置。// string.find()查找字符/字符串,若找到,返回该子字符串首次出现时其首字母的索引。

智能小小鹤的博客 231

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

cin用法介绍(特别是学习了cin.clear和cin.sync)

 cin用法介绍  摘自http://hi.baidu.com/jrckkyy/blog/item/0a4a3026b10a8f128b82a184.html2008年06月04日 星期三 下午 01:11第一次搞cin,真是搞昏人了,现在逐渐开始明白是什么东东,一起探讨一下吧! 首先大家要知道cin是怎么一回事,为什么要有?在C中,输入输出要用printf和s

SearchLife的专栏 1万+

QT学习 控制台输入输出cin cout

QT5 控制台

圆 5155

getline函数与cin的区别

文章目录一.cin二.getline三.通过例子得出结论四.getline的实战案例 一.cin #include<iostream> #include<cstring> using namespace std; int main() { string s; cin>>s; cout<<s; return 0; } 二.getline #include<iostream> #include<cstring> using na

William_fang的博客 1184

C++对C的补充

C++的输入和输出、变量的引用、字符串变量、动态内存分配和释放

heminzheng2008的专栏 657

关于getline函数的一点点心得

getline的一个原型:getline(cin,str,'\n');,在需要输入包含空格的字符串时能用得到,这个函数的特点是遇到回车就结束输入,getline(cin,m1,'\n');cout>m;getline(cin,m1,'\n'); 之间加个getchar(); 吸收换行符就好了

xialin3211的专栏 546

c 中的cin等价于c语言中的什么函数,南京大学计算机基础教学部全国计算机等级考试二级C语言7~12章内容复习.ppt...

南京大学计算机基础教学部全国计算机等级考试二级C语言7~12章内容复习位逻辑运算符的运算优先级都不相同:         ~ & ^ | 高 低 除 ~ (单目运算符,第二优先级)外的位逻辑运算符的运算优先级都低于关系运算符。运算规...

weixin_30873127的博客 218

cin ch等价于c语言中,c++ --> cin和cout输入输出格式

cin和cout输入输出格式Cout 输出1>. bool型输出cout << true <cout << boolalpha << true << " or " << false <cout << noboolalpha << true <cout << boolalpha &...

weixin_33316366的博客 1480

c 语言中cin用法,c中cin的用法

c语言中 符号的用法你知道吗?下面小编就跟你们详细介绍下c语言中 符号的用法,希望对你们有用。c语言中 符号的用法如下:1、cin>>用法1:最基本,也是最常用的用法,输入一个数字:#include using namespace std;main (){int a,b;cin>>a>>b;cout<}输入:2[回车]3[回车]输出:5注意:>&gt...

weixin_29774679的博客 1万+

用 read 替换 cin 函数

用 read 替换 cin 输入流,效率高了10倍!! #define gc() (p1==p2 && (p1=bf,p2=bf+fread(bf,1,1<<21,stdin),p1==p2)?EOF:*p1++) char bf[1 << 21], *p1(bf), *p2(bf); template<class T> void read(T& x) { char c(gc()); char neg(0); T t(0); for

身在边城 心在编程 1141
上一篇: C++中的cctype头文件
下一篇: 【JVM】深入了解JAVA原理,你得先了解它!
Andy in boots
博客等级 码龄7年 73粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值