转自  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用法的更多相关文章

  1. sqlite "insert or replace" 和 "insert or ignore" 用法

    insert or replace:如果不存在就插入,存在就更新insert or ignore:如果不存在就插入,存在就忽略只对UNIQUE约束的字段起作用.举例:建表:CREATE TABLE T ...

  2. C++ cin.ignore()用法

    cin.ignore(int a,char b); a为一行中最大读取字符长度,b为某一个字符.在缓冲区中寻找b,找到后忽略b以前的所有字符(包括b).如果在a的范围内还没有找到b,则忽略b以前的所有 ...

  3. svn ignore 的用法

    一个很简单的需求,我想在add一个文件时忽略里面某种格式的文件,怎么弄? 选中文件夹,然后tortoiseSvn->setting-> global ignore pattern:是客户端 ...

  4. svn ignore 的用法(忽略文件及目录)

    svn ignore 的用法(忽略文件及目录) 若想创建了一个文件夹,并且把它加入版本控制,但忽略文件夹中的所有文件的内容: $ svn mkdir spool $ svn propset svn:i ...

  5. mac 下 svn ignore 操作

    如何在svn中设备忽略的文件或者文件夹 1.如果你还没有对你的文件夹进行版本控制,则可以直接图形操作上进行ignore,或者在命令中运行 svn propedit svn:ignore 文件夹名 . ...

  6. linux下SVN忽略文件/文件夹的方法

    linux下SVN忽略文件/文件夹的方法 假设想忽略文件temp 1. cd到temp所在的目录下: 2. svn propedit svn:ignore . 注意:请别漏掉最后的点(.表示当前目录) ...

  7. mysql insert 主键 重复问题

    转自:http://blog.163.com/liuweiyoung@126/blog/static/173131045201222122732435/ mysql中insert into和repla ...

  8. linux 下svn忽略文件

    假设想忽略文件temp 1. cd到temp所在的目录下: 2. svn propedit svn:ignore . 注意:请别漏掉最后的点(.表示当前目录),如果报错请看下面 3. 打开的文件就是忽 ...

  9. mysql crash cource 书中实例

    样例表 CREATE TABLE customers(  cust_id      int       NOT NULL AUTO_INCREMENT,  cust_name    char(50)  ...

随机推荐

  1. SQL 中使用 WITH AS 提高性能

    一.WITH AS的含义WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是为了让S ...

  2. 支付宝支付demo(亲测)

    支付宝支付demo 这个是java后台调起支付的demo,直接将body返回给安卓端即可调起支付 package com.dyy.test; import java.text.SimpleDateFo ...

  3. WinForm登录验证

    概述:输错三次禁止登陆,15分钟后才能继续. 图示: Form1代码: using System; using System.Configuration; using System.Data.SqlC ...

  4. Python 正则处理_re模块

    正则表达式 动机 文本处理成为计算机常见工作之一 对文本内容搜索,定位,提取是逻辑比较复杂的工作 为了快速方便的解决上述问题,产生了正则表达式技术 定义 文本的高级匹配模式, 提供搜索, 替换, 本质 ...

  5. 基于Matlab实现多次最佳一致的函数逼近(类似求渐进函数)

    %%%做系统识别很重要,方法上完全符合系统识别最基础的理论 function [sun]=main(n) fplot(,],'r'); x=ones(n+,); :n+ x(j+)=cos(pi*(n ...

  6. BSGS+exBSGS POJ2417+POJ3243

    a^x=b(mod p)求x,利用分块的思想根号p的复杂度求答案,枚举同余式两端的变量,用hash的方法去找最小的答案(PS:hash看上去很像链式前向星就很有好感).然后如果p不是质数时,就利用同余 ...

  7. zkclient中包引用不对,导致NoSuchMethodError

    nidonglin commented on 31 Oct 2014 Exception in thread "main" java.lang.NoSuchMethodError: ...

  8. 集成Tomcat环境到Eclipse中

    集成Tomcat环境到Eclipse中 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装Eclipse环境 1>.安装JDK环境 官方地址:https://www.or ...

  9. Python Pandas 简单使用之 API熟悉

    1.read_csv li_index = ['round_id', 'index', 'c-sequen' ] dataset = pd.read_csv(file, low_memory=Fals ...

  10. SpringCloud笔记五:Feign

    目录 什么是Feign? 有了Ribbon我还要Feign干嘛? 新建consumer-feign 修改api项目 引入Maven文件 新建feign的接口 启动项目 报错 发泄发泄心情,一个段落 什 ...