是否可以 'throw away' 从输入流中读取值?
Is it possible to 'throw away' read value from input stream?
我用列中的数据处理一些数据,例如,
1 -0.004002415458937208 0.0035676328502415523
2 -0.004002415796209478 0.0035676331876702957
....
我只对最后两个值感兴趣。我通常发现读取这些值很方便:
std::ifstream file(file_name);
double a, b;
for (lines) {
// | throwing away the first value by reading it to `a`
file >> a >> a >> b;
store(a, b);
}
我不确定这对其他人来说可读性如何,并且当数据结构未知时可能会被认为是错误。 我能否以某种方式让它看起来更明确,我真的想丢弃第一个读取值?
我想要这样的东西,但没有任何效果:
file >> double() >> a >> b; // I hoped I could create some r-value kind of thing and discard the data in there
file >> NULL >> a >> b;
您可以使用 std::istream::ignore
.
例如:
file.ignore(std::numeric_limits<std::streamsize>::max(), ' '); //columns are separated with space so passing it as the delimiter.
file >> a >> b;
您可以使用 file::ignore(255, ' ') 忽略字符,直到下一个 space。
std::ifstream file(file_name);
double a, b;
for (lines) {
// skip first value until space
file.ignore(255, ' ');
file >> a >> b;
store(a, b);
}
或者您可以使用辅助变量来存储第一个值:
std::ifstream file(file_name);
double aux, a, b;
for (lines) {
// skip first value
file >> aux >> a >> b;
store(a, b);
}
如果您不想创建一个显式忽略的变量,并且您觉得通过调用操作流来显式忽略该值太冗长,您可以利用 operator>>
重载std::istream
接受一个 std::istream&(*)(std::istream&)
函数指针:
template <typename CharT>
std::basic_istream<CharT>& ignore(std::basic_istream<CharT>& in){
std::string ignoredValue;
return in >> ignoredValue;
}
像这样使用:
std::cin >> ignore >> a >> b;
如果你想验证它是一种可以读入类型的形式,你可以提供一个额外的模板参数来指定被忽略值的类型:
// default arguments to allow use of ignore without explicit type
template <typename T = std::string, typename CharT = char>
std::basic_istream<CharT>& ignore(std::basic_istream<CharT>& in){
T ignoredValue;
return in >> ignoredValue;
}
像这样使用:
std::cin >> ignore >> a >> b;
// and
std::cin >> ignore<int> >> a >> b;
我用列中的数据处理一些数据,例如,
1 -0.004002415458937208 0.0035676328502415523
2 -0.004002415796209478 0.0035676331876702957
....
我只对最后两个值感兴趣。我通常发现读取这些值很方便:
std::ifstream file(file_name);
double a, b;
for (lines) {
// | throwing away the first value by reading it to `a`
file >> a >> a >> b;
store(a, b);
}
我不确定这对其他人来说可读性如何,并且当数据结构未知时可能会被认为是错误。 我能否以某种方式让它看起来更明确,我真的想丢弃第一个读取值?
我想要这样的东西,但没有任何效果:
file >> double() >> a >> b; // I hoped I could create some r-value kind of thing and discard the data in there
file >> NULL >> a >> b;
您可以使用 std::istream::ignore
.
例如:
file.ignore(std::numeric_limits<std::streamsize>::max(), ' '); //columns are separated with space so passing it as the delimiter.
file >> a >> b;
您可以使用 file::ignore(255, ' ') 忽略字符,直到下一个 space。
std::ifstream file(file_name);
double a, b;
for (lines) {
// skip first value until space
file.ignore(255, ' ');
file >> a >> b;
store(a, b);
}
或者您可以使用辅助变量来存储第一个值:
std::ifstream file(file_name);
double aux, a, b;
for (lines) {
// skip first value
file >> aux >> a >> b;
store(a, b);
}
如果您不想创建一个显式忽略的变量,并且您觉得通过调用操作流来显式忽略该值太冗长,您可以利用 operator>>
重载std::istream
接受一个 std::istream&(*)(std::istream&)
函数指针:
template <typename CharT>
std::basic_istream<CharT>& ignore(std::basic_istream<CharT>& in){
std::string ignoredValue;
return in >> ignoredValue;
}
像这样使用:
std::cin >> ignore >> a >> b;
如果你想验证它是一种可以读入类型的形式,你可以提供一个额外的模板参数来指定被忽略值的类型:
// default arguments to allow use of ignore without explicit type
template <typename T = std::string, typename CharT = char>
std::basic_istream<CharT>& ignore(std::basic_istream<CharT>& in){
T ignoredValue;
return in >> ignoredValue;
}
像这样使用:
std::cin >> ignore >> a >> b;
// and
std::cin >> ignore<int> >> a >> b;