============

怎么处理一行空格中的逗号,

,,,
,,,
,,,
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++中处理输入输出的方法的更多相关文章

  1. C/C++中的输入输出重定向

    目录 一 C/C++中的输入输出重定向 1.1 C语言输入输出重定向 1.2 C++语言输入输出重定向 参考资料 注:原创不易,转载请务必注明原作者和出处,感谢支持! 一 C/C++中的输入输出重定向 ...

  2. 《算法竞赛入门经典》学习笔记 2.4.4 C++中的输入输出

    2.4.3 64位整数输入输出long long除了cin,cout,也可以借助于printf和scanf语句,但对应的占位符缺是和平台与编译器相关的:在Linux中,gcc很同意的用%lld:在Wi ...

  3. java中的输入输出<1>

    java中的输入输出基础(1) java中的IO支持通过java.io包下的类和接口来支持.在java.io包下主要包括输入.输出两种io流,每种输入.输出流又分为字节流和字符流. 字节流就是以字节为 ...

  4. JavaScript中Math对象的方法介绍

    1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...

  5. Android中锁定文件的方法

    androidSDK中并没有锁定文件相关的api. 但是android是基于linux操作系统的,linux比较底层,灵活性也更大,为了实现锁定文件的效果,大概有以下几种办法: 用chmod命令修改文 ...

  6. jQuery中的事件绑定方法

    在jQuery中,事件绑定方法大致有四种:bind(),live(), delegate(),和on(). 那么在工作中应该如何选择呢?首先要了解四种方法的区别和各自的特点. 在了解这些之前,首先要知 ...

  7. Eclipse中自动提示的方法参数都是arg0,arg1的解决方法

    Eclipse中自动提示的方法参数都是arg0,arg1,就不能根据参数名来推断参数的含义,非常不方便. 解决方法:Preferences->Java->Installed JREs,发现 ...

  8. Power BI官方视频(2) Power BI嵌入到应用中的3种方法

    今天给大家介绍3种将Power BI嵌入到应用中的方法. 本文原文地址:Power BI官方视频(2) Power BI嵌入到应用中的3种方法 Power BI系列文章地址:微软Power BI技术文 ...

  9. JQuery中each()的使用方法说明

    JQuery中each()的使用方法说明 对于jQuery对象,只是把each方法简单的进行了委托:把jQuery对象作为第一个参数传递给jQuery的each方法.换句话说:jQuery提供的eac ...

随机推荐

  1. C#队列

    队列(Queue)是插入操作限定在表的尾部而其它操作限定在表的头部进行的线性表.把进行插入操作的表尾称为队尾(Rear),把进行其它操作的头部称为队头(Front).当对列中没有数据元素时称为空对列( ...

  2. ThinkPHP_基础(1)目录结构

    (说明:文中的颜色一一对应) 目录结构 www WEB部署目录(或者子目录) ├─index.php 入口文件 ├─README.md README文件 ├─composer.json Compose ...

  3. sqlite实现oracle的rownum功能

    SELECT (SELECT COUNT(*) FROM [table] AS t2 WHERE t2.name <= t1.name) AS rowNum, id, name FROM [ta ...

  4. sumoselect插件

    由于项目需要,研究了下sumoselect插件,接下来简单介绍下sumoselect. 在百度上搜索“sumoselect.js”,查到的网页基本上都有对sumoselect的基本介绍,如下: 简单介 ...

  5. 数据库和3G的结合使用

    // //  设备数据管理类 #import <Foundation/Foundation.h> #import "DeviceInfo.h" @interface D ...

  6. matlab 批量提取CNN特征

    无类别,图像混合放置: clear close all addpath ./matlab model= './models/bvlc_reference_caffenet/deploy.prototx ...

  7. LintCode Subarray Sum

    For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...

  8. java问题排查可能用到的一些命令

    1. jmap查询jvm内存使用情况 -heap :打印jvm heap的情况 -histo: 打印jvm heap的直方图.其输出信息包括类名,对象数量,对象占用大小. -histo:live : ...

  9. iOS 统计App 的代码总行数

    打开Terminal,cd 到项目的根目录下,输入以下命令 find . -name "*.m" -or -name "*.mm" -or -name &quo ...

  10. laypage

    <%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> < ...