RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 685926
Accepted
Desmond Fox
Desmond Fox
Asked:2020-07-01 17:53:18 +0000 UTC2020-07-01 17:53:18 +0000 UTC 2020-07-01 17:53:18 +0000 UTC

二维 QVector:如何正确声明和初始化?

  • 772

我正在编写游戏“Life”。有这样一个问题。比如有一个LifeMatrix类,里面会有一个二维的,dynamic的(不知道dynamic适不适合)vector,这样我就可以在同一个类中进行某些动作(比如count某个小区的邻居)

我在头文件中这样声明:

QVector<QVector<bool> *> *m_matrix;

我这样初始化它:

LifeMatrix::LifeMatrix(short horizontalCount, short verticalCount, QObject *parent) : QObject(parent)
{
    //
    m_matrix    = new QVector<QVector<bool> *>(verticalCount);
    for (int i = 0; i < verticalCount; i++)
        m_matrix[i] = new QVector<bool>(horizontalCount);

老实说,我不知道我做的对不对。当我想用一些数据填充它时:

for (int i = 0; i < m_matrix->count(); i++)
    for (int j = 0; j < m_matrix[i].count(); i++)
        m_matrix[i][j] = false;

显示几个错误:

no match for 'operator=' (operand types are 'QVector<QVector<bool>*>' and 'QVector<bool>*')
         m_matrix[i] = new QVector<bool>(horizontalCount);
                     ^

就在这里。但这通常很奇怪,因为我已经可以上传数据了。是不是?

ошибка: cannot convert 'bool' to 'QVector<bool>*' in assignment
             m_matrix[i][j] = false;
                            ^

我有个问题。m_matrix[i].count()出于某种原因,当我将点更改为时,我仍然发誓->。毕竟,向量存储指针。如果你留下一个点,错误就会消失。

 ошибка: base operand of '->' has non-pointer type 'QVector<QVector<bool>*>'
             for (int j = 0; j < m_matrix[i]->count(); i++)
                                            ^

我究竟做错了什么?

c++
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Best Answer
    Bearded Beaver
    2020-07-01T19:59:10Z2020-07-01T19:59:10Z

    在您的情况下,我不会使用向量,而是使用常规的二维数组。当程序中元素的数量不断变化时,向量很好,但据我了解,字段大小设置一次然后使用。

    class LifeMatrix
    {
    public:
        LifeMatrix() {
            m_matrix = nullptr;
            m_nx = 0;
            m_ny = 0;
        }
    
        void allocate(int nx, int ny) {
            m_nx = nx;
            m_ny = ny;
            m_matrix = new bool *[nx];
            for (int i = 0; i < nx; i++)
            {
                m_matrix[i] = new bool[ny];
            }
        }
    
        void freeMemory() {
            if (m_matrix == nullptr)
                return;
            for (int i = 0; i < m_nx; i++)
                delete [] m_matrix[i];
            delete [] m_matrix;
            m_matrix = nullptr;
        }
    
    protected:
        int m_nx;
        int m_ny;
        bool **m_matrix;
    };
    

    您还需要定义用于通过 x 和 y 访问单元格数量的函数,以及单元格中生命的 setter / getter,以便一切都是风水。

    • 1
  2. Ariox
    2020-07-01T23:10:59Z2020-07-01T23:10:59Z

    如果您需要 2D 矩阵 - 只需使用 2D 矩阵,它们有很多实现,包括仅标题。或者,您可以从 boost 中获取实现。如果你想自己实现它,最好实现这样的东西:

    template<class T>
    class Matrix{
      std::vector<T> data_;
      std::size_t size1_; //количество строк
      std::size_t size2_; //количество столбцов
    public:
      Matrix(std::size_t size1, std::size_t size2): 
        data_(size1*size2), 
        size1_(size1), 
        size2_(size2)
      {  }
    
      std::size_t size1() const {return size1_;}
      std::size_t size2() const {return size2_;}
    
      T& operator()(std::size_t i1, std::size_t i2){
        assert(i1 < size1 && i2 < size2);
        return data_[i2 + i1*size2()];
      }
      const T& operator()(std::size_t i1, std::size_t i2) const {
        assert(i1 < size1 && i2 < size2);
        return data_[i2 + i1*size2()];
      }
    };
    

    为一个向量存储一个向量是没有意义的,使用这样的构造实际上不是很方便。也可以使用 QVector,但它在按索引访问时会有额外的间接性(由于某种原因它通过双指针存储数据)。可以通过创建一个新矩阵并将旧矩阵的内容复制(移动)到其中来调整大小。boost的matrix可以提前分配内存,允许你在一定范围内自由改变大小,必要时分配内存,但是手动实现意义不大。

    如果您恰好需要一个具有不同长度字符串的向量向量,那么最好使用std::vector<std::vector<T>>,这里与 QVector 的区别在于两个间接调用。也许这对您来说并不重要,但我不认为不必要地降低性能有什么意义(但是,以及相反)。

    • 1
  3. arrowd
    2020-07-01T18:16:38Z2020-07-01T18:16:38Z

    您不是在使用向量,而是在使用指向向量的指针。因此,e.g.operator[]不是为 定义的QVector<T> *,它是为 定义的QVector<T>。

    使它m_matrix只是向量的向量:QVector<QVector<bool> >。

    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5