C++程序将终止无需进行矢量的输入即可

发布于 2025-02-06 17:02:26 字数 987 浏览 3 评论 0原文

我正在尝试将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


当我尝试在Windows终端中编译程序时,我会得到此错误

“运行错误”

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.
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
error

run error

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

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

发布评论

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

评论(3

少女七分熟 2025-02-13 17:02:26

当输入流上有一些数据时,这些数据不可解析为编号,n将设置为0。

这可能是引起的,当您的终端向输入流发送线路时,或者不是完全设置,要转发终端输入(例如约翰提到)。

将其从不同的终端运行,或者添加解决方法,只接受大于0的数字:

int n = 0;
while (n == 0) {
  cin >> n;
}
// ...

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:

int n = 0;
while (n == 0) {
  cin >> n;
}
// ...
滥情稳全场 2025-02-13 17:02:26

编辑一个问题更新:

错误的屏幕截图表明问题不是您的代码,而是安装在计算机上的软件。由于加载了标准库,因此提出了此错误,但是找不到您使用的编译器功能特异性。

一个解释是,您安装了一个编译器,另一个是您将某些错误的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 use g++ 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.

  • If you provided negative value then exception can be thrown terminating program without any output.
  • other explanation you have entered invalid value and std::cin is in error state. In such case std::cin doesn't do anything until error is handled.

Here is demo of proper input, negative size or invalid none numeric value.

黄昏下泛黄的笔记 2025-02-13 17:02:26

您在声明和分配值时是错误的。请记住,您使用的是vector,而不是任何类型的array

根据您的设置,正确的语法将是:

#include <bits/stdc++.h>

using namespace std;

int main() {
    int numbers;

    vector<int> input;

    cout<<"How many numbers? ";
    cin>>numbers;

    int temp; /* temporary value used in vet.push_back() */

    for (int i = 0; i < numbers; i++) {
        cout<<"Vector input: ";
        cin>>temp;
        input.push_back(temp);
    }

    cout<<"Size of vector: "<<input.size()<<"\n";

    // for output
    for (int i = 0; i < input.size(); i++)
        cout<<input[i]<<" ";

    return 0;
}

实际上,向量是动态的。您无需给它一个预定义的维度。它的默认尺寸声明,每次达到最大尺寸时,都会加倍。它使用舒适,但由于所需的破碎和未使用的区域,它并不有效。

在像您这样的程序上,但是您可以使用动态数组。

最后一件事,如果您使用的是向量,请考虑#include&lt; vector&gt;

You are wrong in declaring and assigning the value. Remember that you are using a vector and not an array of any type.

According to your setting the correct syntax would be:

#include <bits/stdc++.h>

using namespace std;

int main() {
    int numbers;

    vector<int> input;

    cout<<"How many numbers? ";
    cin>>numbers;

    int temp; /* temporary value used in vet.push_back() */

    for (int i = 0; i < numbers; i++) {
        cout<<"Vector input: ";
        cin>>temp;
        input.push_back(temp);
    }

    cout<<"Size of vector: "<<input.size()<<"\n";

    // for output
    for (int i = 0; i < input.size(); i++)
        cout<<input[i]<<" ";

    return 0;
}

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

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