C++程序将终止无需进行矢量的输入即可
我正在尝试将n
整数输入向量。 以下是我用过的代码。首先,我要求值n,然后输入所有这些n
整数。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> input(n);
for (int i = 0; i < n; i++)
{
cin >> input[i];
}
return 0;
}
我不知道为什么该程序在不进行任何输入的情况下终止,我看不到此代码的任何缺陷,而且对他人来说似乎不错,但对我来说却没有。
输入示例:
5
5 3 1 5 2
I am trying to input n
integers into the vector.
Following is the code I have used to do so. First, I ask for the value n, then input all those n
integers.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> input(n);
for (int i = 0; i < n; i++)
{
cin >> input[i];
}
return 0;
}
I don't know why the program is terminating without taking any input, I can't see any flaws with this code, and it seems to work fine for others but not for me.
This is what the terminal looks like after executing this program.
Input Example:
5
5 3 1 5 2
When I try to compile the program in the windows terminal, I get this error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当输入流上有一些数据时,这些数据不可解析为编号,
n
将设置为0。这可能是引起的,当您的终端向输入流发送线路时,或者不是完全设置,要转发终端输入(例如约翰提到)。
将其从不同的终端运行,或者添加解决方法,只接受大于0的数字:
When there is some data on the input stream, that is not parseable as number,
n
is set to 0.This may be caused, when your terminal sends line-breaks to the input stream, or is not setup at all, to forward terminal input (like john mentioned).
Either run it from a different terminal or add a workaround, to only accept numbers greater than 0:
编辑一个问题更新:
错误的屏幕截图表明问题不是您的代码,而是安装在计算机上的软件。由于加载了标准库,因此提出了此错误,但是找不到您使用的编译器功能特异性。
一个解释是,您安装了一个编译器,另一个是您将某些错误的dll复制到使用应用程序中的目录。您也可能会提供一些奇怪的编译器标志。
因此,请检查机器上已安装的内容,并且您的应用程序当前目录中有什么(是否有DLL)?
命令行的屏幕截图(请不要这样做)表示您以错误的方式构建项目。使用
GCC
请勿将C ++标准库链接到代码。您应该使用g ++
,然后将标准库隐含地链接到您的程序。奇怪的是,您的程序无法构建(控制台屏幕截图),并且您以某种方式运行它导致运行时错误(错误消息框图像)。
旧答案
仍然有效。
请提供输入的示例。
std :: cin
处于错误状态。在这种情况下,std :: cin
在处理错误之前不做任何事情。这是演示适当输入,负大小或无效的无数值值。
Edit after question update:
The screenshot of error indicates that problem is not your code, but software installed on your machine. This error was raised since standard library was loaded, but function specificity for compiler you used was not found.
One explanation is you have more then one compiler installed, other is you copied some wrong dlls to directory with your application. It is also possible you provided some strange compiler flags.
So please inspect what has been installed on your machine and what is in current directory of your application (are there some dlls)?
Screenshot of command line (please do not do that) indicates that you are building project in wrong way. Use of
gcc
do not link C++ standard library into a code. You should useg++
then standard library will be linked to your program implicitly.This is strange that your program failed to build (console screenshot) and you run it somehow which lead to runtime error (error message box image).
Old answer
Can be still valid.
Please provide example of input.
std::cin
is in error state. In such casestd::cin
doesn't do anything until error is handled.Here is demo of proper input, negative size or invalid none numeric value.
您在声明和分配值时是错误的。请记住,您使用的是
vector
,而不是任何类型的array
。根据您的设置,正确的语法将是:
实际上,向量是动态的。您无需给它一个预定义的维度。它的默认尺寸声明,每次达到最大尺寸时,都会加倍。它使用舒适,但由于所需的破碎和未使用的区域,它并不有效。
在像您这样的程序上,但是您可以使用动态数组。
最后一件事,如果您使用的是向量,请考虑
#include&lt; vector&gt;
库You are wrong in declaring and assigning the value. Remember that you are using a
vector
and not anarray
of any type.According to your setting the correct syntax would be:
Indeed, a vector is dynamic. You don't need to give it a pre-defined dimension. It is declared with a default size and every time it reaches the maximum size, it double. It is comfortable to use, but it is not efficient due to the fragmentation and unused areas it requires.
On programs like yours, however you can use dynamic array.
Last thing, if you are using vectors take into account the
#include <vector>
library