c++ ignore用法
转自 http://blog.sina.com.cn/s/blog_4b3336c50102v45n.html
std::cin.ignore() can be called three different ways:
1.No arguments: A single character is taken from the input buffer and discarded:
std::cin.ignore(); //discard 1 character
2.One argument: The number of characters specified are taken from the input buffer and discarded:
std::cin.ignore(33); //discard 33 characters
3.Two arguments: discard the number of characters specified, or discard characters up to and including the specified delimiter (whichever comes first):
std::cin.ignore(26, '\n'); //ignore 26 characters or to a newline, whichever comes first
举例:
①
cin.ignore(1000, '\n')的含义是把缓冲区内从当前字符开始知道'\n'之前字符(如果有1000个的话)忽略掉,实际上你这里假设一行不会超过1000个字符,所以含义是忽略一行
②新建个文件abc.txt,然后把下面这几句话拷贝到里面:
the, quick, brown, fox, jumps, over, the, lazy, dog 运行程序,输入"abc.txt"。注意,abc.txt这个文件,一定要跟你这个.cpp源文件在同一个目录里。
infile.ignore(200,','); //跳过200个字符,直到遇到','为止,所以跳过了"the,"
infile>>a; //读入一个字符串,即"quick,",因为默认情况下空格是读取分隔符
infile.ignore(200,','); //跳过200个字符,直到遇到','为止,所以跳过了"brown,"
infile>>b; //读入一个字符串,即"fox,",注意空格是分隔符
infile.ignore(200,','); //跳过"jumps,"
infile>>c; //读取"over,"
最后的输出结果就是
quick,
fox,
over,
③
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
class Person{
public:
int id;
string name;
string age;
};
istream& operator>>( istream& is, Person& per ){
is>>per.id && is.ignore() && getline(is,per.name,',') && is>>per.age;//ignore忽略掉一个字符,读完id后跳过1个字符
return is;
}
ostream& operator<<(ostream& os, const Person& per){
return os << per.id << ',' << per.name << ',' << per.age;
}
int _tmain(int argc, _TCHAR* argv[])
{
ifstream inFile("1.txt");
if (!inFile){
return -1;
}
vector personVec;
for(Person per; inFile>>per;){
personVec.push_back(per);
}
for(vector::iterator itor=personVec.begin(); itor!=personVec.end(); ++itor){
cout << *itor << '\n';
}
cout << endl;
return 0;
}
文本为:
1001,xiaoming,30
1002,Laoming,40
1003,Dming,50
若文本为
1001-xiaoming-30 则需要getline(is,per.name,',')改为getline(is,per.name,'-')
若文本为
xiaoming-30,2 则需要getline(is,per.name,'-') && is>>per.age && is.ignore() && is>>per.id;
若文本为
IF1304,2014-12-23 09:15.500
IF1305,2014-12-24 09:16.600
IF1306,2014-12-25 09:17.700
则
istream& operator>>( istream& is, Person& per ){
//is>>per.id && is.ignore() && getline(is,per.name,'-') && is>>per.age;//ignore忽略掉一个字符,读完id后跳过1个字符
//getline(is,per.name,'-') && is>>per.age && is.ignore() && is>>per.id;
getline(is,per.inst,',') && is >> per.year && is.ignore() && is >> per.month && is.ignore() && is >> per.day && is.ignore()
&& is >> per.hour && is.ignore() && is >> per.min && is.ignore() && is >> per.tick;
return is;
}
ostream& operator<<(ostream& os, const Person& per){
return os << per.inst << ',' << per.year << '-' << per.month << "-" << per.day << " " << per.hour << ":" << per.min << "." <<per.tick;
}
其中类为
class Person{
public:
string inst;
int year;
int month;
int day;
int hour;
int min;
int sec;
int tick;
};
若文本为
IF1305 455 33
IF1304 4535 344
IF1345 4553 35
is >> per.inst >> per.year >> per.month;若文本中均为空格 则直接读就行,因为----cin自动过滤tab、空格、回车!!!
c++ ignore用法的更多相关文章
- sqlite "insert or replace" 和 "insert or ignore" 用法
insert or replace:如果不存在就插入,存在就更新insert or ignore:如果不存在就插入,存在就忽略只对UNIQUE约束的字段起作用.举例:建表:CREATE TABLE T ...
- C++ cin.ignore()用法
cin.ignore(int a,char b); a为一行中最大读取字符长度,b为某一个字符.在缓冲区中寻找b,找到后忽略b以前的所有字符(包括b).如果在a的范围内还没有找到b,则忽略b以前的所有 ...
- svn ignore 的用法
一个很简单的需求,我想在add一个文件时忽略里面某种格式的文件,怎么弄? 选中文件夹,然后tortoiseSvn->setting-> global ignore pattern:是客户端 ...
- svn ignore 的用法(忽略文件及目录)
svn ignore 的用法(忽略文件及目录) 若想创建了一个文件夹,并且把它加入版本控制,但忽略文件夹中的所有文件的内容: $ svn mkdir spool $ svn propset svn:i ...
- mac 下 svn ignore 操作
如何在svn中设备忽略的文件或者文件夹 1.如果你还没有对你的文件夹进行版本控制,则可以直接图形操作上进行ignore,或者在命令中运行 svn propedit svn:ignore 文件夹名 . ...
- linux下SVN忽略文件/文件夹的方法
linux下SVN忽略文件/文件夹的方法 假设想忽略文件temp 1. cd到temp所在的目录下: 2. svn propedit svn:ignore . 注意:请别漏掉最后的点(.表示当前目录) ...
- mysql insert 主键 重复问题
转自:http://blog.163.com/liuweiyoung@126/blog/static/173131045201222122732435/ mysql中insert into和repla ...
- linux 下svn忽略文件
假设想忽略文件temp 1. cd到temp所在的目录下: 2. svn propedit svn:ignore . 注意:请别漏掉最后的点(.表示当前目录),如果报错请看下面 3. 打开的文件就是忽 ...
- mysql crash cource 书中实例
样例表 CREATE TABLE customers( cust_id int NOT NULL AUTO_INCREMENT, cust_name char(50) ...
随机推荐
- 一个很变态的SQL
select max(s.operat_time) as pzTime from ws_state_record s where s.status = (select p1.node_id from ...
- 使用css实现无滚动条滚动+使用插件自定义滚动条样式
使用css实现无滚动条滚动,摘抄自:曹小萌博客 使用css实现无滚动条滚动,大体思路是在div外面再套一个div.这个div设置overflow:hidden.而内容div设置 overflow-x: ...
- GOOGLE RANKBRAIN 完整指南
[译]GOOGLE RANKBRAIN 完整指南 ( 2018 最新版 ) 2018.01.29 来源 http://www.zhidaow.com/post/google-rankbrain ...
- TypeError: 'NoneType' object is not subscriptable
运行,显示TypeError: 'NoneType' object is not subscriptable错误信息,原因是变量使用了系统内置的关键字list 重新定义下这个变量就好了
- 如何巧妙的使用ArrayList的Clone方法
一.ArrayList的Clone方法的源码 返回一个Object对象,所以在使用此方法的时候要强制转换. ArrayList的本质是维护了一个Object的数组,所以克隆也是通过数组的复制实现的,属 ...
- SpringBoot系列: Spring项目异常处理最佳实践
===================================自定义异常类===================================稍具规模的项目, 一般都要自定义一组异常类, 这 ...
- [再寄小读者之数学篇](2014-06-22 发散级数 [中国科学技术大学2012年高等数学B考研试题])
设 $a_n>0$, $S_n=a_1+a_2+\cdots+a_n$, 级数 $\dps{\vsm{n}a_n}$ 发散, 证明: $\dps{\vsm{n}\cfrac{a_n}{S_n}} ...
- [物理学与PDEs]第1章习题7 载流线圈的磁场
设一半径为 $R$ 的圆周电路上的电流强度为 $I$. 试计算在通过圆心垂直于圆周所在平面的直线上, 由该圆周电路产生的磁场的磁感强度. 解答: 由对称性知在该直线 $l$ 上, ${\bf B}$ ...
- [物理学与PDEs]第1章第6节 电磁场的标势与矢势 6.2 电磁场的标势与矢势
1. 标势.矢势: $$\beex \bea \Div{\bf B}=0&\ra \exists\ {\bf A},\st {\bf B}=\rot{\bf A},\\ \rot{\bf ...
- git秘钥生成
#修改git配置 vi .gitconfig #生成秘钥 ssh-keygen -t rsa -C "邮箱地址" #查看秘钥 cat id_rsa.pub