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) ...
随机推荐
- Python——爬虫——数据提取
一.XML数据提取 (1)定义:XML指可扩展标记语言.标记语言,标签需要我们自行定义 (2)设计宗旨:是传输数据,而非显示数据,具有自我描述性 (3)节点关系: 父:每个元素及属性都有一个父. ...
- [SimplePlayer] 2. 在屏幕上显示视频图像
我们这里采用SDL(本文所用版本为SDL2.0.5)来进行图像输出,SDL在进行图像渲染时一般采用的会是direct3D或者opengl,SDL对它们进行了封装,不过我们这里只讨论SDL的使用,并不会 ...
- PHP——模糊匹配文件|目录
内置函数 glob函数 详解 http://www.w3school.com.cn/php/func_filesystem_glob.asp
- 第六十三天 js基础
一.JS三个组成部分 ES:ECMAScript语法 DOM:document对象模型=>通过js代码与页面文档(出现在body中的所有可视化标签)进行交互 BOM:borwser对象模型=&g ...
- mongoDB 集合(表)操作
mongoDB 集合(表)操作 集合命名规则 使用 utf8 字符(通常不会起中文名字) 不能含有 "\0" 字符 不要以 system. 开头(否咋会覆盖系统集合开头) 不要和关 ...
- Vue(小案例_vue+axios仿手机app)_首页(底部导航栏+轮播图+九宫格)
---恢复内容开始--- 一.前言 1.底部导航(两种做法) 2.轮播图 ...
- Python项目读取配置的几种方式
1. 将配置写在Python文件中 配置文件(config.py 或 settings.py) 通常放置在程序源代码的目录,方便引用 配置文件 # settings.py class Config(o ...
- 微服务之路由网关—Nginx
Nginx 简介Nginx 是一款自由的.开源的.高性能的 HTTP 服务器和反向代理服务器,它具有有很多非常优越的特性: • 作为 Web 服务器: 相比 Apache , Nginx 使用更少的 ...
- openstack项目【day24】:keystone部署及操作
阅读目录 一 前言 二 版本信息 三 部署keystone 四 keystone操作 五 验证 六 创建脚本 七 keystone使用套路总结 一 前言 任何软件的部署都是没有技术含量的,任何就部署讲 ...
- 安装mysql和xampp遇到问题
1.mysql的期望地址和配置的地址不一致: 解决方法:修改注册表 在附件命令提示符输入regedit 找[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Se ...