简体   繁体   English

数组中向量的 C++ 代码块问题

[英]C++ CodeBlock Issue With Vector In Array

I'm unable to modify a vector inside the vector array using CodeBlock 20.03.我无法使用 CodeBlock 20.03 修改向量数组中的向量。 Here's the "reduced" version of code:这是代码的“简化”版本:

#include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

vector<int> neighbor_fields[500000];
int main()
{
    neighbor_fields[0].push_back(0);

    return 0;
}

The program runs fine, but there's an error displayed after program finishes "Terminated with status -1073741510" .程序运行良好,但程序完成后显示错误"Terminated with status -1073741510" I've done some research and this script seems like it is perfectly legal in C++.我做了一些研究,这个脚本似乎在 C++ 中是完全合法的。

I think is is an issue related to CodeBlock/compiler(gcn gcc) because it compiles fine on elsewhere.我认为这是与 CodeBlock/compiler(gcn gcc) 相关的问题,因为它在其他地方编译得很好。

Thanks.谢谢。

EDIT: I've corrected the code(it should be an array of vectors instead of a single vector).编辑:我已经更正了代码(它应该是向量数组而不是单个向量)。 Another interesting thing is doing a push_back on a single vector also terminates with that error code:另一个有趣的事情是对单个向量执行 push_back 也会以该错误代码终止:

#include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

vector<int> neighbor_fields;
int main()
{
    neighbor_fields.push_back(0);
    return 0;
}

As shown in cppreference:vector operator[] , accessing a nonexistent element through this operator is undefined behavior.cppreference:vector operator[]所示,通过此运算符访问不存在的元素是未定义的行为。

For your case, neighbor_fields[0] means that it access the first element of the vector, which is nonexistent and resulting in the error.对于您的情况, neighbor_fields[0]意味着它访问向量的第一个元素,该元素不存在并导致错误。

it should be: neighbor_fields.push_back(0);应该是: neighbor_fields.push_back(0);

there are 2 misunderstanding in this code:这段代码有两个误解:

  1. when you declare neighbor_fields you create a container but you are not putting anything in it.当您声明neighbor_fields时,您创建了一个容器,但您没有在其中放入任何东西。 So if you try to access to an item of the container you are gonna get an undefined behavior, if you want to make the program safer you can use std::vector::at , it works like std::vector::operator[] but when the index is out of range throw an exeption so you can handle it.因此,如果您尝试访问容器的某个项目,您将获得未定义的行为,如果您想让程序更安全,您可以使用std::vector::at ,它的工作方式类似于std::vector::operator[]但是当索引超出范围时抛出异常,以便您可以处理它。

  2. std::vector::operator[] return a reference to a variable of the type stored by the container, in our case int , you can not use push_back() on a int variable but you use it for adding items at the container. std::vector::operator[]返回对容器存储类型变量的引用,在我们的例子中是int ,您不能在 int 变量上使用push_back() ,但可以使用它在容器中添加项目。 The code you are looking for is您正在寻找的代码是

    std::vector neighbor_fields; std::vector 邻居域;

    int main() { int main() {

     neighbor_fields.push_back(0); return 0;

    } }

this code create a container and add the value 0. You can assume that a std::vector is like a c-like arrays so int array[10] , but is possible change the size adding and erasing items.此代码创建一个容器并添加值 0。您可以假设 std::vector 类似于 c 的 arrays 所以int array[10] ,但可以更改大小添加和擦除项目。

I'm sorry for the code but I can't mark it us code很抱歉代码,但我无法将其标记为我们的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM