C++ 使用索引为二维向量赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25937413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:28:51  来源:igfitidea点击:

Assigning values to 2D Vector by using indices

c++vectormultidimensional-array

提问by AvP

I am trying to add values to a 2D vector by using both indices. When I run my program, I get the windows message saying the program has stopped working. Using Dev-C++ to debug showed that there was a segmentation fault (I am not sure what this means). Please do not suggest using arrays, I have to use vectors for this assignment.

我正在尝试使用两个索引将值添加到 2D 向量。当我运行我的程序时,我收到 Windows 消息,说程序已停止工作。使用 Dev-C++ 调试显示存在分段错误(我不确定这是什么意思)。请不要建议使用数组,我必须在此作业中使用向量。

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char** argv) { 

vector< vector<int> > matrix;
cout << "Filling matrix with test numbers.";
for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {

        matrix[i][j] = 5; // causes program to stop working

    }
}
}

I have created a test case where I want to fill a 3X3 matrix with the value 5. I suspect that it has something to do with the size of the 2D vector not being specifically defined. How would I fill a 2D vector with values by using the indices?

我创建了一个测试用例,我想用值 5 填充一个 3X3 矩阵。我怀疑它与未具体定义的 2D 向量的大小有关。如何使用索引用值填充 2D 向量?

回答by Cory Kramer

As written, this is problematic, you are trying to write to a vector for which you did not yet allocate memory.

正如所写,这是有问题的,您正在尝试写入尚未为其分配内存的向量。

Option 1 - Resize your vectors ahead of time

选项 1 - 提前调整矢量大小

vector< vector<int> > matrix;
cout << "Filling matrix with test numbers.";
matrix.resize(4);  // resize top level vector
for (int i = 0; i < 4; i++)
{
    matrix[i].resize(4);  // resize each of the contained vectors
    for (int j = 0; j < 4; j++)
    {
        matrix[i][j] = 5;
    }
}

Option 2 - Size your vector when you declare it

选项 2 - 在声明向量时调整向量的大小

vector<vector<int>>  matrix(4, vector<int>(4));

Option 3 - Use push_backto resize the vector as needed.

选项 3 - 用于push_back根据需要调整矢量大小。

vector< vector<int> > matrix;
cout << "Filling matrix with test numbers.";
for (int i = 0; i < 4; i++)
{
    vector<int> temp;
    for (int j = 0; j < 4; j++)
    {
        temp.push_back(5);
    }
    matrix.push_back(temp);
}

回答by taocp

You have not allocated any space for your 2d vector. So in your current code, you are trying to access some memory that does not belong to your program's memory space. This will result in Segmentation Fault.

您尚未为 2d 向量分配任何空间。因此,在您当前的代码中,您正在尝试访问一些不属于程序内存空间的内存。这将导致分段错误。

try:

尝试:

 vector<vector<int> >  matrix(4, vector<int>(4));

If you want to give all elements the same value, you can try:

如果你想给所有元素相同的值,你可以尝试:

 vector<vector<int> >  matrix(4, vector<int>(4,5)); // all values are now 5

回答by Mohammad Jawad

vector<int> v2d1(3, 7);
    vector<vector<int> > v2d2(4, v2d1);
    for (int i = 0; i < v2d2.size(); i++) {
        for(int j=0; j <v2d2[i].size(); j++) {
                    cout<<v2d2[i][j]<<" ";
                }
                cout << endl;
            }