使用读取程序中的输入时遇到问题cin
。输入看起来像这样:
5 2
10 17 17 17 37
其中5是项目数,2是分隔符数(与该问题无关)。两行均以行尾字符结尾。
问题是,当项目数变大然后大约500时,项目数也变大(而不是17,例如50356),cin
在读取大输入时停在某个地方(它只是冻结了整个程序)。奇怪的是,它可以在较小的输入上完美运行(我的程序完全符合我的期望),但不适用于较大的输入。我还想在输入大小> 5000的情况下运行它。我不知道为什么它不起作用。可能存在缓冲区问题,我需要刷新。该解决方案可能非常简单。
void fill()
{
cin >> numberOfItems;
cin >> dividers;
vector<unsigned long int> roundedSum;
roundedSum.resize(numberOfItems);
unRoundedSum.resize(numberOfItems);
currentSum.resize(numberOfItems);
updatedSum.resize(numberOfItems);
unsigned long int tempValue;
cin >> tempValue;
roundedSum[0] = roundValue(tempValue);
unRoundedSum[0] = tempValue;
for (unsigned long int i = 1; i < numberOfItems; ++i){
cin >> tempValue;
tempValue += unRoundedSum[i - 1];
unRoundedSum[i] = tempValue;
roundedSum[i] = roundValue(unRoundedSum[i]);
}
currentSum = roundedSum;
updatedSum = roundedSum;
}
编辑:问题已解决问题不在于cin
功能,而在于我给程序输入的方式。将巨大的输入量粘贴到剪贴板上,然后在运行程序时将其作为参数放入终端,这是问题所在。当运行程序时,./program < input.in
其中input.in是一个文件,该文件以上述格式保存所有输入,则程序可以正常运行并且不再冻结。
问题已经解决了。问题不在于cin
功能,而在于我给程序输入的方式。将巨大的输入量粘贴到剪贴板上,然后在运行程序时将其作为参数放入终端,这是问题所在。当以./program < input.in
“ input.in”文件的形式运行程序时,该文件以上述格式保存所有输入,则程序可以正常运行并且不再冻结。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句