[英]Using getline in an overloaded input operator
Book.h
:
#ifndef BOOKDATE
#define BOOKDATE
#include <iostream>
#include <string>
class Book{
friend std::istream& operator>>(std::istream&, Book&);
private:
std::string title, author;
int number;
};
std::istream& operator>>(std::istream&, Book&);
#endif // BOOKDATE
Book.cpp
:
#include "BookDate.h"
using namespace std;
istream& operator>>(istream& is, Book& rhs){
getline(is, rhs.title);
getline(is, rhs.author);
is >> rhs.number;
if(!is)
rhs = Book();
return is;
}
我想知道应该如何精确地为Book
类创建输入运算符。 title
和author
不只一个字,因此很适合我需要使用getline
来接收该数据。 与getline
有关的问题是,自上次使用cin
以来,它可能会获取流中剩余的所有'\\n'
。 例如;
int x;
cin >> x; //newline is not extracted and left behind
Book a;
cin >> a; //"title" is automatically made empty!
我可以改用cin.ignore(256, '\\n')
但是使用它的责任是用户的还是类author
的? 用户在输入Book
对象之前使用.ignore
还是类作者将.ignore
放在输入操作的开头?
似乎在前一种情况下,用户将必须了解.ignore
方法,但这样做必须了解Book
的输入运算符的实现,这是不希望的。 在后一种情况下,将.ignore
放在运算符中意味着我的运算符可能无法适应某些情况,因为它总是希望在处理之前遇到换行符。 例如,从输入文件中读取数据,例如:
book1
author1
1
book2
author2
2
手段book1
得到由忽略cin.ignore(256,'\\n')
要使operator>>
行为更类似于内置类型的操作符,可以在读取输入之前使用ws
操纵器跳过空格。
只需使用
is >> ws;
在输入运算符的开头,流将定位在当前位置之后的第一个非空白字符处。
为了适当地使提取运算符超载,您可以将输入格式更改为要填充的三个变量的序列,即:
(title, author, number)
并将您的operator>>
修改为:
istream& operator>>(istream& is, Book& rhs){
// just a suggestion: it is better if there is no input to do nothing
if(!is) return is;
string title, author;
int number;
char par1, comma, par2;
cin >> skipws >> par1 >> title >> comma >> author>> comma >> number >> par2;
if (par1 != '(' || comma != ',' || par1 != ')'){
// set failbit to indicate invalid input format
is.clear(ios_base::failbit);
}
rhs(title, author, number);
return is;
}
放置is.ignore();
在getline(is, rhs.title);
之前getline(is, rhs.title);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.