c++中处理输入输出的方法
============
怎么处理一行空格中的逗号,
- ,,,
- ,,,
- ,,,
- c,c,d,d
使用c++的方法,可以这么处理:
#include <sstream>
#include<algorithm>
using namespace std;
将数据放入string dataline中,
replace(dataline.begin,dataline.end(),',',' ');
istringstream myInstream(dataline);
while(myInstream>>v){
cout<<v<<" ";
}cout<<endl;
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <algorithm>
- //#include "xiaozhao.h"
- #include <sstream>
- using namespace std;
- int main()
- {
- //Cxiao c;
- //c.test();
- freopen("input.txt","r",stdin);
- int N;
- int v;
- string dataline;
- cin>>N;
- for(int i = ;i<=N;i++){
- getline(cin,dataline);
- replace(dataline.begin(),dataline.end(),',',' ');
- istringstream myInstream(dataline);
- while(myInstream>>v){
- cout<<v<<" ";
- }cout<<endl;
- }
- return ;
- }
=============
在c中,常用的处理输入的函数有:scanf(),getchar(),gets();
在c++中,常用的处理输入输出的函数有:cin,getline
=======
#1知道输入数据组数n
scanf("%d",&n)
while(n--){
这里处理每一组输入,然后直接按照格式输入,没有必要开数组存储答案;
}
#2没有数据总数,以EOF结束
scanf();
while(scanf("%s|%d")!=EOF){
处理每一组数据,并输出
}
1 2 3
4 5 6
- #include <iostream>
- #include <stdio.h>
- //#include "xiaozhao.h"
- using namespace std;
- int main()
- {
- //Cxiao c;
- //c.test();
- freopen("input.txt","r",stdin);
- int N;
- scanf("%d",&N);
- while(N--){
- int a,b,c;
- scanf("%d%d%d",&a,&b,&c);
- printf("%d%d%d\n",a,b,c);
- }
- return ;
- }
2,没有数据总数,以EOF结尾
1 2 3
4 5 6
- #include <iostream>
- #include <stdio.h>
- //#include "xiaozhao.h"
- using namespace std;
- int main()
- {
- //Cxiao c;
- //c.test();
- freopen("input.txt","r",stdin);
- int a,b,c;
- while(scanf("%d%d%d",&a,&b,&c)!=EOF){
- printf("%d%d%d\n",a,b,c);
- }
- return ;
- }
--getchar()读入一个字符
while((ch=getchar())!=EOF){
}
--gets读入一行
while(gets(buf)!=NULL){
}
3,以0或-1结束的输入
while(scanf("%d",&n),n!=0){
}
1,2,3
4,5,6
-1
- #include <iostream>
- #include <stdio.h>
- //#include "xiaozhao.h"
- using namespace std;
- int main()
- {
- //Cxiao c;
- //c.test();
- freopen("input.txt","r",stdin);
- int a,b,c;
- while(scanf("%d%d%d",&a,&b,&c) && (a!=- && b!=- && c!=-)){
- printf("%d%d%d\n",a,b,c);
- }
- return ;
- }
============
getchar()和scanf(%c)的功能是一样的,都是接受从键盘输入一个字符;
需要注意的是,这两个函数读入的是输入流中当前位置的字符,比如:
scanf("%d",&n);
c = getchar();//
67/ (/表示换行),则n=67,c = /
===
同样gets和scanf(%s)类似
scanf("%s")在读入字符串时遇到空格或者回车是就会结束
get可以读入一样包含空格的字符串,遇到回车结束(不能读取回车);
=================
cin 读取字符串时,遇到空白符(空格,换行等)结束
===
char str[BUFFER];
while(cin>>str){}
===
getline读字符串时遇到换行符结束,用于读取一整行
char str[BUFFER];
while(cin.getline(str,BUFFER)){}
===
string str;
while(getline(cin,str)){}
c++中处理输入输出的方法的更多相关文章
- C/C++中的输入输出重定向
目录 一 C/C++中的输入输出重定向 1.1 C语言输入输出重定向 1.2 C++语言输入输出重定向 参考资料 注:原创不易,转载请务必注明原作者和出处,感谢支持! 一 C/C++中的输入输出重定向 ...
- 《算法竞赛入门经典》学习笔记 2.4.4 C++中的输入输出
2.4.3 64位整数输入输出long long除了cin,cout,也可以借助于printf和scanf语句,但对应的占位符缺是和平台与编译器相关的:在Linux中,gcc很同意的用%lld:在Wi ...
- java中的输入输出<1>
java中的输入输出基础(1) java中的IO支持通过java.io包下的类和接口来支持.在java.io包下主要包括输入.输出两种io流,每种输入.输出流又分为字节流和字符流. 字节流就是以字节为 ...
- JavaScript中Math对象的方法介绍
1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...
- Android中锁定文件的方法
androidSDK中并没有锁定文件相关的api. 但是android是基于linux操作系统的,linux比较底层,灵活性也更大,为了实现锁定文件的效果,大概有以下几种办法: 用chmod命令修改文 ...
- jQuery中的事件绑定方法
在jQuery中,事件绑定方法大致有四种:bind(),live(), delegate(),和on(). 那么在工作中应该如何选择呢?首先要了解四种方法的区别和各自的特点. 在了解这些之前,首先要知 ...
- Eclipse中自动提示的方法参数都是arg0,arg1的解决方法
Eclipse中自动提示的方法参数都是arg0,arg1,就不能根据参数名来推断参数的含义,非常不方便. 解决方法:Preferences->Java->Installed JREs,发现 ...
- Power BI官方视频(2) Power BI嵌入到应用中的3种方法
今天给大家介绍3种将Power BI嵌入到应用中的方法. 本文原文地址:Power BI官方视频(2) Power BI嵌入到应用中的3种方法 Power BI系列文章地址:微软Power BI技术文 ...
- JQuery中each()的使用方法说明
JQuery中each()的使用方法说明 对于jQuery对象,只是把each方法简单的进行了委托:把jQuery对象作为第一个参数传递给jQuery的each方法.换句话说:jQuery提供的eac ...
随机推荐
- business knowledge
Finance knowledge Trading---At the core of our business model is Trading, which involves the buying ...
- vld使用
1.下载VLD官方版本 2.安装 3.在vs里面的属性里->c/c++->常规->副含附加目录 C:\Program Files (x86)\Visual Leak Detecto ...
- 探索软件工程道路上的我II (Θ∀Θ#)
------作业要求------ 第一版本程序Prog1:+ 给定一个数组,实现数组元素求和:具体要求:实现对一维数组(a[100])的所有元素相加运算.+ 数据准备:a)数组长度:100:b)数组数 ...
- 信号量与PV操作
在计算机操作系统中,PV操作是进程管理中的难点.首先应弄清PV操作的含义:PV操作由P操作原语和V操作原语组成(原语是不可中断的过程),对信号量进行操作,具体定义如下: P(S):①将信号量S的 ...
- (转)CSS中的绝对定位与相对定位定位
层级关系为: <div ——————————— position:relative; 不是最近的祖先定位元素,不是参照物<div—————————-没有设置为定位元素,不是参照物<d ...
- 【JS】falsy与truthy
1.Falsy值,当进行逻辑判断时均为false(如!!false==false).六个Falsy值:false.undefined.null.正负0.NaN."". 2.其余所有 ...
- Python 程序员经常犯的 10 个错误
关于PythonPython是一种解释性.面向对象并具有动态语义的高级程序语言.它内建了高级的数据结构,结合了动态类型和动态绑定的优点,这使得... 关于Python Python是一种解释性.面向对 ...
- 解决1130 Host 'localhost' is not allowed to connect to this MySQL server
- CSS3实现边框锯齿效果
通过CSS3的linear-gradient实现的 <div class="bg"></div> .bg{ width:300px; height:50px ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...