【问题标题】:C++ Vector Subscript of of range范围的 C++ 向量下标
【发布时间】:2017-11-19 19:00:55
【问题描述】:

我正在制作整数的 MxN 矩阵类并将值存储到一维 std::vector 中。当我尝试使用将 2D 向量展平为 1D 向量的公式添加元素时:我得到向量下标超出范围错误,我不确定是什么原因造成的。我知道它是从我的addElement() 函数中抛出的。这是我的课程和 main.cpp

#include <vector>
#include <iostream>

template<unsigned Col, unsigned Row>
class Matrix2D {
public:
    const unsigned col_size = Col;
    const unsigned row_size = Row;
    const unsigned stride_ = col_size;
    const unsigned matrix_size = col_size * row_size;

private:
    std::vector<int> data_;

public:
    Matrix2D() {
        data_.reserve( col_size * row_size );
    }

    void addElement( unsigned x, unsigned y, int val ) {
        data_[(x * row_size + y)]= val;
    }

    int getElement( unsigned x, unsigned y ) {
        return data_[(x * row_size + y)];
    }

    int getElement( unsigned idx ) {
        return data_[idx];
    }
};

int main() {

    Matrix2D<4, 4> mat4x4;
    mat4x4.addElement( 0, 0, 0 );
    mat4x4.addElement( 1, 0, 1 );
    mat4x4.addElement( 2, 0, 2 );
    mat4x4.addElement( 3, 0, 3 );

    mat4x4.addElement( 0, 1, 4 );
    mat4x4.addElement( 1, 1, 5 );
    mat4x4.addElement( 2, 1, 6 );
    mat4x4.addElement( 3, 1, 7 );

    mat4x4.addElement( 0, 2, 8 );
    mat4x4.addElement( 1, 2, 9 );
    mat4x4.addElement( 2, 2, 10 );
    mat4x4.addElement( 3, 2, 11 );

    mat4x4.addElement( 0, 3, 12 );
    mat4x4.addElement( 1, 3, 13 );
    mat4x4.addElement( 2, 3, 14 );
    mat4x4.addElement( 3, 3, 15 );

    for ( unsigned i = 0; i < mat4x4.matrix_size; i++ ) {
        std::cout << mat4x4.getElement( i ) << "\n";
    }

    std::cout << "\nPress any key & enter to quit." << std::endl;
    char c;
    std::cin >> c;

    return 0;
}

编辑

读了几篇cmets后:我意识到我用的是std::vector&lt;T&gt;.reserve(count)而不是std::vector&lt;T&gt;.resize(count)

在构造函数中修复了那一行代码并运行程序后,我还在 main.cpp 中以错误的顺序添加了元素。现在正确的运行代码是:

#include <iostream>
#include <vector>

template<unsigned Col, unsigned Row>
class Matrix2D {
public:
    const unsigned col_size = Col;
    const unsigned row_size = Row;
    const unsigned stride_ = col_size;
    const unsigned matrix_size = col_size * row_size;

private:
    std::vector<int> data_;

public:
    Matrix2D() {
        data_.resize( col_size * row_size );
    }

    void addElement( unsigned x, unsigned y, int val ) {
        data_[(x * row_size + y)]= val;
    }

    int getElement( unsigned x, unsigned y ) {
        return data_[(x * row_size + y)];
    }

    int getElement( unsigned idx ) {
        return data_[idx];
    }
};

int main() {

    Matrix2D<4, 4> mat4x4;
    mat4x4.addElement( 0, 0, 0 );
    mat4x4.addElement( 0, 1, 1 );
    mat4x4.addElement( 0, 2, 2 );
    mat4x4.addElement( 0, 3, 3 );

    mat4x4.addElement( 1, 0, 4 );
    mat4x4.addElement( 1, 1, 5 );
    mat4x4.addElement( 1, 2, 6 );
    mat4x4.addElement( 1, 3, 7 );

    mat4x4.addElement( 2, 0, 8 );
    mat4x4.addElement( 2, 1, 9 );
    mat4x4.addElement( 2, 2, 10 );
    mat4x4.addElement( 2, 3, 11 );

    mat4x4.addElement( 3, 0, 12 );
    mat4x4.addElement( 3, 1, 13 );
    mat4x4.addElement( 3, 2, 14 );
    mat4x4.addElement( 3, 3, 15 );

    for ( unsigned i = 0; i < mat4x4.matrix_size; i++ ) {
        std::cout << mat4x4.getElement( i ) << "\n";
    }

    std::cout << "\nPress any key & enter to quit." << std::endl;
    char c;
    std::cin >> c;

    return 0;
}

这给出了正确的输出:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Press any key & enter to quit.

【问题讨论】:

  • .resize,而不是reserve。投票结束是错字,如果不是,请阅读文档。
  • Matrix2D() : data_( col_size * row_size ){}
  • 哇;我不敢相信我忽略了我使用vector&lt;T&gt;.reserve() 而不是vector&lt;T&gt;.resize() 的事实。感谢您的帮助;一个多小时我都无法绕开它……

标签: c++ exception vector


【解决方案1】:

像 Baum mit Augen 的评论一样,更新储备以调整大小,这将解决问题。

预留

Reserve() 函数改变向量容量。

调整大小

resize() 设置元素的数量,如果需要,可以设置默认值。

如果您将向量视为桶,容量就是桶的大小。在你添加一些东西之前,它仍然是一个空桶。 “调整大小”将设置您在该存储桶中有多少项目。


在下面的代码中请注意,即使向量的容量增加,元素的数量也如何为 0。

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> MyVector;
    size_t i = 0;
    std::cout << "No Elements\n";
    std::cout << "Capacity = " << MyVector.capacity() << "\n";
    std::cout << "Number Of Elements " << MyVector.size() << "\n";

    std::cout << "Resize Capacity still with no elements\n";
    MyVector.reserve(10);
    std::cout << "Capacity = " << MyVector.capacity() << "\n";
    std::cout << "Number Of Elements " << MyVector.size() << "\n";

    std::cout << "Add some Elements\n";
    MyVector.push_back(++i);
    MyVector.push_back(++i);
    MyVector.push_back(++i);
    MyVector.push_back(++i);
    MyVector.push_back(++i);
    std::cout << "Capacity = " << MyVector.capacity() << "\n";
    std::cout << "Number Of Elements " << MyVector.size() << "\n";

    return 0;
}

结果:

No Elements
Capacity = 0
Number Of Elements 0
Resize Capacity still with no elements
Capacity = 10
Number Of Elements 0
Add some Elements
Capacity = 10
Number Of Elements 5

【讨论】:

    猜你喜欢
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多