istream_iterator:接受额外输入

发布于 2024-10-02 02:48:03 字数 1280 浏览 5 评论 0原文

我无法让这个该死的东西正常工作。问题是,如果我想输入2个数字,实际上我必须输入3个。这是怎么回事?

namespace MT
{
    template<class IIT, class OIT>
    OIT copy_n(IIT iitBegin, size_t szCount, OIT oitBegin)
    {   
        for(size_t szI = 0; (szI < szCount); ++szI)
        {   
            *oitBegin++ = *iitBegin++;
        }   

        return oitBegin;
    }   
};

int main()
{
    vector<int> vNumbers;
    vector<char> vOperators;
    int iNumCount = 0;
    int iNumOperators = 0;

    cout << "Enter number of number(s) :) :\n";
    cin >> iNumCount;
    cout << "Enter number of operator(s) :\n";
    cin >> iNumOperators;

    int iNumber;
    cout << "Enter the " << iNumCount << " number(s):\n";
    MT::copy_n(istream_iterator<int>(cin), iNumCount, back_inserter(vNumbers));

    char cOperator;
    cout << "\nEnter the " << iNumOperators << " operator(s):\n";
    MT::copy_n(istream_iterator<char>(cin), iNumOperators, back_inserter(vOperators));

    copy(vNumbers.begin(), vNumbers.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
    copy(vOperators.begin(), vOperators.end(), ostream_iterator<char>(cout, " "));
    cout << endl;

    return 0;
}

I can't get this blasted thing working right. The problem is, If I want to enter 2 numbers, I actually have to enter 3. What is wrong?

namespace MT
{
    template<class IIT, class OIT>
    OIT copy_n(IIT iitBegin, size_t szCount, OIT oitBegin)
    {   
        for(size_t szI = 0; (szI < szCount); ++szI)
        {   
            *oitBegin++ = *iitBegin++;
        }   

        return oitBegin;
    }   
};

int main()
{
    vector<int> vNumbers;
    vector<char> vOperators;
    int iNumCount = 0;
    int iNumOperators = 0;

    cout << "Enter number of number(s) :) :\n";
    cin >> iNumCount;
    cout << "Enter number of operator(s) :\n";
    cin >> iNumOperators;

    int iNumber;
    cout << "Enter the " << iNumCount << " number(s):\n";
    MT::copy_n(istream_iterator<int>(cin), iNumCount, back_inserter(vNumbers));

    char cOperator;
    cout << "\nEnter the " << iNumOperators << " operator(s):\n";
    MT::copy_n(istream_iterator<char>(cin), iNumOperators, back_inserter(vOperators));

    copy(vNumbers.begin(), vNumbers.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
    copy(vOperators.begin(), vOperators.end(), ostream_iterator<char>(cout, " "));
    cout << endl;

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2024-10-09 02:48:03

将流迭代器循环更改为:

    for(size_t szI = 0; (szI < szCount); ++szI)
    {   
        *oitBegin = *iitBegin;
        if (szI < szCount - 1)
        {
          ++oitBegin;
          ++iitBegin;
        }
    }   

Change your stream iterator loop to:

    for(size_t szI = 0; (szI < szCount); ++szI)
    {   
        *oitBegin = *iitBegin;
        if (szI < szCount - 1)
        {
          ++oitBegin;
          ++iitBegin;
        }
    }   
情仇皆在手 2024-10-09 02:48:03

问题是 istream_iterator 不是在解引用时读取,而是在递增时读取:

  • 构造 istream_iterator 时读取第一个值,
  • 迭代器时 copy_n 读取 iNumCount 附加值递增

换句话说,您有太多的 iiBegin++ 读取了一个被丢弃的值。在任何经典迭代器上,最后一个增量会让你“越过终点”,但在这里它会触发从标准输入(显然没有终点)的不需要的读取。

[编辑] 可能的解决方案:

template<class IIT, class OIT>
OIT copy_n(IIT iitBegin, size_t szCount, OIT oitBegin)
{   
  *oitBegin++ = *iitBegin;
  for(size_t szI = 0; (szI < szCount - 1); ++szI)
    *oitBegin++ = *++iitBegin;
  return oitBegin;
}

The problem is that istream_iterator reads not when it's dereferenced, but when it's incremented :

  • A first value is read when the istream_iterator is constructed
  • iNumCount additional values are read by copy_n when the iterator is incremented

In other words, you have one too many iiBegin++ which reads a value that gets discarded. On any classic iterator, this last increment would get you 'past the end', but here it triggers an unwanted read from the standard input (which obviously doesn't have an end).

[EDIT] Possible solution :

template<class IIT, class OIT>
OIT copy_n(IIT iitBegin, size_t szCount, OIT oitBegin)
{   
  *oitBegin++ = *iitBegin;
  for(size_t szI = 0; (szI < szCount - 1); ++szI)
    *oitBegin++ = *++iitBegin;
  return oitBegin;
}
万水千山粽是情ミ 2024-10-09 02:48:03

我认为问题是你没有传递 EOF 符号。如果您在 Linux 下工作,请尝试在插入第二个数字后键入 CTRL+D(在 Windows 下应该是 CTRL+Z,但我不确定)。

I think the problem is that you are not passing the EOF symbol. If you are working under Linux, try to type CTRL+D after inserting the second number (under Windows should be CTRL+Z but I'm not sure).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文