istream_iterator:接受额外输入
我无法让这个该死的东西正常工作。问题是,如果我想输入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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将流迭代器循环更改为:
Change your stream iterator loop to:
问题是 istream_iterator 不是在解引用时读取,而是在递增时读取:
换句话说,您有太多的
iiBegin++
读取了一个被丢弃的值。在任何经典迭代器上,最后一个增量会让你“越过终点”,但在这里它会触发从标准输入(显然没有终点)的不需要的读取。[编辑] 可能的解决方案:
The problem is that
istream_iterator
reads not when it's dereferenced, but when it's incremented :istream_iterator
is constructedIn 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 :
我认为问题是你没有传递 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).