c++ ignore

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、空格、回车!!!

posted @ 2022-02-21 17:05  myd620  阅读(76)  评论(0编辑  收藏  举报