0

我有这种情况,我需要从具有这种格式的文件中的每一行中获取两个 int 值:

43=>113
344=>22

是否可以做一些事情,比如设置一个等于 => 的分隔符,而不是使用 >> 运算符来分配整数?

ifstream iFile("input.in");
int a,b;
iFile >> a >> b;

也可以自动完成以类似格式输出吗?

oFile << a << b;

代替

oFile << a << "=>" << b;

谢谢。

4

3 回答 3

1

You can't do it directly, without any extra code when reading or writing, but you can write a manipulator which handles it for you more explicitly:

std::istream&
mysep( std::istream& source )
{
    source >> std::ws;      //  Skip whitespace.
    if ( source.get() != '=' || source.get() != '>' ) {
        //  We didn't find the separator, so it's an error
        source.setstate( std::ios_base::failbit );
    }
    return source;
}

Then, if you write:

ifile >> a >> mysep >> b;

, you will get an error is the separator is absent.

On output, you can use a similar manipulator:

std::ostream&
mysep( std::ostream& dest )
{
    dest << "=>";
    return dest;
}

This has the advantage of keeping the information as to what the separator is isolated in these two specific functions (which would be defined next to one another, in the same source file), rather than spread out where ever you are reading or writing.

Also, these data presumably represent some particular type of information in your code. If so, you should probably define it as a class, and then defined operators >> and << over that class.

于 2013-07-16T08:41:13.740 回答
1

给定ab是内置类型的变量,您不能定义自己的用户定义运算符来流式传输它们(标准库已经提供了此类函数)。

你可以用你想要的行为写出代码......

int a, b;
char eq, gt;

// this is probably good enough, though it would accept e.g. "29 = > 37" too.
// disable whitespace skipping with <iomanip>'s std::noskipws if you care....
if (iFile >> a >> eq >> gt >> b && eq == '=' && gt == '>')
    ...

ORa将and包装bclassor struct,并为此提供用户定义的运算符。有很多 SO 问题的答案解释了如何编写这样的流函数。

或者写一个支持函数......

#include <iomanip>

std::istream& skip_eq_gt(std::istream& is)
{
    char eq, gt;

    // save current state of skipws...
    bool skipping = is.flags() & std::ios_base::skipws;

    // putting noskipws between eq and gt means whatever the skipws state
    // has been will still be honoured while seeking the first character - 'eq'

    is >> eq >> std::noskipws >> gt;

    // restore the earlier skipws setting...
    if (skipping)
        is.flags(is.flags() | std::ios_base::skipws);

    // earlier ">>" operations may have set fail and/or eof, but check extra reasons to do so
    if (eq != '=' || gt != '>')
        is.setstate(std::ios_base::failbit)

    return is;
}

...然后像这样使用它...

if (std::cin >> a >> skip_eq_gt >> b)
    ...use a and b...

这个函数“有效”,因为流被设计为接受重新配置流的某些方面的“io 操纵器”函数(例如std::noskipws, : std::istream& (std::istream&).

于 2013-07-16T08:14:46.190 回答
0

如果你一直有=>分隔符,你可以编写一个函数来解析文档的行。

void Parse(ifstream& i)
{
   string l;
   while(getline(i,l))
   {
      //First part
      string first = l.substr(0, l.find("=>"));

      //Second part
      string second = l.substr(l.find("=>")+2, l.length());

      //Do whatever you want to do with them.
   }
}
于 2013-07-16T08:12:50.793 回答