qt 中 动态 创建 对象 二维数组 记录

qt 中 动态 创建 对象 二维数组

二维数组指针
Coin * coins[4][4];
别人的方法
#ifndef DATACONFIG_H
#define DATACONFIG_H

#include <QObject>
#include <QMap>
#include <QVector>

class dataConfig : public QObject
{
    Q_OBJECT
public:
    explicit dataConfig(QObject *parent = 0);

public:

    QMap<int, QVector< QVector<int> > >mData;



signals:

public slots:
};

#endif // DATACONFIG_H
QVector< QVector<int>> v;

dataconfig

#include "dataconfig.h"
#include <QDebug>
dataConfig::dataConfig(QObject *parent) : QObject(parent)
{

     int array1[4][4] = {{1, 1, 1, 1},
                        {1, 1, 0, 1},
                        {1, 0, 0, 0},
                        {1, 1, 0, 1} } ;

     QVector< QVector<int>> v;
     for(int i = 0 ; i < 4;i++)
     {
         QVector<int>v1;
         for(int j = 0 ; j < 4;j++)
         {

            v1.push_back(array1[i][j]);
         }
         v.push_back(v1);
     }

     mData.insert(1,v);


     int array2[4][4] = { {1, 0, 1, 1},
                          {0, 0, 1, 1},
                          {1, 1, 0, 0},
                          {1, 1, 0, 1}} ;

     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array2[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(2,v);



     int array3[4][4] = {  {0, 0, 0, 0},
                           {0, 1, 1, 0},
                           {0, 1, 1, 0},
                           {0, 0, 0, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array3[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(3,v);


     int array4[4][4] = {   {0, 1, 1, 1},
                            {1, 0, 0, 1},
                            {1, 0, 1, 1},
                            {1, 1, 1, 1}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array4[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(4,v);


     int array5[4][4] = {  {1, 0, 0, 1},
                           {0, 0, 0, 0},
                           {0, 0, 0, 0},
                           {1, 0, 0, 1}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array5[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(5,v);


     int array6[4][4] = {   {1, 0, 0, 1},
                            {0, 1, 1, 0},
                            {0, 1, 1, 0},
                            {1, 0, 0, 1}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array6[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(6,v);


     int array7[4][4] = {   {0, 1, 1, 1},
                            {1, 0, 1, 1},
                            {1, 1, 0, 1},
                            {1, 1, 1, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array7[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(7,v);

     int array8[4][4] = {  {0, 1, 0, 1},
                           {1, 0, 0, 0},
                           {0, 0, 0, 1},
                           {1, 0, 1, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array8[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(8,v);

     int array9[4][4] = {   {1, 0, 1, 0},
                            {1, 0, 1, 0},
                            {0, 0, 1, 0},
                            {1, 0, 0, 1}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array9[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(9,v);



     int array10[4][4] = {  {1, 0, 1, 1},
                            {1, 1, 0, 0},
                            {0, 0, 1, 1},
                            {1, 1, 0, 1}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array10[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(10,v);


     int array11[4][4] = {  {0, 1, 1, 0},
                            {1, 0, 0, 1},
                            {1, 0, 0, 1},
                            {0, 1, 1, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array11[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(11,v);

     int array12[4][4] = {  {0, 1, 1, 0},
                            {0, 0, 0, 0},
                            {1, 1, 1, 1},
                            {0, 0, 0, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array12[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(12,v);


     int array13[4][4] = {    {0, 1, 1, 0},
                              {0, 0, 0, 0},
                              {0, 0, 0, 0},
                              {0, 1, 1, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array13[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(13,v);

     int array14[4][4] = {    {1, 0, 1, 1},
                              {0, 1, 0, 1},
                              {1, 0, 1, 0},
                              {1, 1, 0, 1}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array14[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(14,v);


     int array15[4][4] = {   {0, 1, 0, 1},
                             {1, 0, 0, 0},
                             {1, 0, 0, 0},
                             {0, 1, 0, 1}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array15[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(15,v);


     int array16[4][4] = {   {0, 1, 1, 0},
                             {1, 1, 1, 1},
                             {1, 1, 1, 1},
                             {0, 1, 1, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array16[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(16,v);

     int array17[4][4] = {  {0, 1, 1, 1},
                            {0, 1, 0, 0},
                            {0, 0, 1, 0},
                            {1, 1, 1, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array17[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(17,v);


     int array18[4][4] = { {0, 0, 0, 1},
                           {0, 0, 1, 0},
                           {0, 1, 0, 0},
                           {1, 0, 0, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array18[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(18,v);

     int array19[4][4] = {   {0, 1, 0, 0},
                             {0, 1, 1, 0},
                             {0, 0, 1, 1},
                             {0, 0, 0, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array19[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(19,v);

     int array20[4][4] = {  {0, 0, 0, 0},
                            {0, 0, 0, 0},
                            {0, 0, 0, 0},
                            {0, 0, 0, 0}} ;
     v.clear();
     for(int i = 0 ; i < 4;i++)
     {
          QVector<int>v1;
          for(int j = 0 ; j < 4;j++)
          {
             v1.push_back(array20[i][j]);
          }
          v.push_back(v1);
     }

     mData.insert(20,v);


     //测试数据
//    for( QMap<int, QVector< QVector<int> > >::iterator it = mData.begin();it != mData.end();it++ )
//    {
//         for(QVector< QVector<int> >::iterator it2 = (*it).begin(); it2!= (*it).end();it2++)
//         {
//            for(QVector<int>::iterator it3 = (*it2).begin(); it3 != (*it2).end(); it3++ )
//            {
//                qDebug() << *it3 ;
//            }
//         }
//         qDebug() << endl;
//    }


}
2222
int **p;
block::block(int row ,int col,int boomNum )
{
    //动态创建二维数组,用于保存块信息
    p = new int *[row];
    for(int i = 0;i<row;i++)
        p[i] = new int [col];
    //初始化数组,全部赋值为零
    for(int i =0;i<row;i++)
        for(int j = 0;j<col;j++)
            p[i][j] = 0;
    //产生随机数炸弹,将对应块赋值为99
    srand((unsigned)time(0));
    for(int i =1;i<=boomNum;i++)
    {
         int num = rand()%(row*col)-1;
         int rowBlock = num / col;
         int colBlock = num % col;
         if( p[rowBlock][colBlock] == 99)
         {
          i--;
          continue;
         }
         else
         p[rowBlock][colBlock] = 99;

    }
    //为除炸弹块外赋值,表明周围八位的炸弹数
    for(int i =0;i<row;i++)
        for(int j = 0;j<col;j++)
        {
            if(p[i][j]!=99)
            {
            char blockBoomNum = 0;
            for(int blockRow = i -1;blockRow<=i +1 ;blockRow++)
            {
                for(int blockCol = j -1;blockCol<=j + 1;blockCol++)
                {
                    //处理特殊情况,比如第一排、第一列等
                    if(blockRow<0||blockCol<0||blockRow>=row||blockCol>=col)
                    continue;
                    //开始检测周围雷数
                    if(p[blockRow][blockCol]==99)
                      blockBoomNum ++;
                }
            }
            p[i][j]=blockBoomNum;
            }

        }
    //初始化完成
    //初始化私有成员
    this->row = row;
    this->col = col;
    this->boomNum = boomNum;
    this->timeNum = 0;
    this->redFlagNum = boomNum;
}


block::~block()
{
   for(int i =0;i<row;i++)
   {
    delete[]  p[i];
   }
   delete [] p;
}
C++ 动态对象数组的知识总结

本文章向大家介绍C++ 动态对象数组的知识总结,主要包括C++ 动态对象数组的知识总结使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
默认构造函数
首先,我们来看一下是什么是默认构造函数,如下面一段代码:

#include<iostream>
#include<math.h>
using namespace std;
class Point
{
    double x, y;
public:
    void print();
    Point(double a=0,double b=1){x=a;y=b;}  //默认构造函数
    Point(double a){x=1;y=1;} //构造函数
    Point(){} //默认构造函数

};
void Point::print()
{
    cout << x << " " << y << endl;
}

所以,可以知道默认构造函数总得来说是可以在对象生成的时候,不需要传入参数,对应情况就是:

Point point;   
Point *point=new Point;

非默认构造函数就是必须要传参。关于默认构造函数有以下知识点:

程序没有定义任何构造函数,则有默认的构造函数Point(){},里面的成员的值没有初始化
定义默认构造函数有两种方式,如上述代码展示的,一是定义一个无参的构造函数,二是定义所有参数都有默认值的构造函数
注意:一个类只能有一个默认构造函数!也就是说上述两种方式不能同时出现,一般选择 Point(); 这种形式的默认构造函数

带默认构造函数的动态数组
我们来看一下如果一个类带默认构造函数,如下代码是生成静态和动态数组的:

#include<iostream>
#include<math.h>
using namespace std;
class Point
{
    double x, y;
public:
    void print();

};
void Point::print()
{
    cout << x << " " << y << endl;
}
int main() {
    //静态对象数组
    Point pp[5];      
    for (int i = 0; i<5; i++)
        pp[i].print();
    //动态对象数组
    Point *p = new Point[5]; //这里面5也可以是变量
    for (int i = 0; i<5; i++)
            p[i].print();
    //别忘记delete相应的空间
}

可以看出来,如果一个类带默认构造函数的话,那么生成的时候就不需要传入参数了,方便了不少。

没有默认构造函数的动态数组
如果一个类没有默认构造函数呢,这种情况就需要传入参数了,如下代码:

#include<iostream>
#include<math.h>
using namespace std;
class Point
{
    double x, y;
public:
    Point(double a, double b);
    void print();
};
Point::Point(double a, double b)
{
    x = a;
    y = b;
}
void Point::print()
{
    cout << x << " " << y << endl;
}
int main()
{
    Point *p[5];//静态指针数组
    for (int i = 0; i < 5; i++) {
        p[i] = new Point(i, i);    //其中你可以用自己的方式来初始化对象,例如从键盘接收值等
    }
    for (int i = 0; i < 5; i++) {
        p[i]->print();
    }
    Point **pp = new Point *[5];  //动态指针数组,5也可以是变量
    for (int i = 0; i < 5; i++) {
        pp[i] = new Point(i, i);
    }
    for (int i = 0; i < 5; i++) {
        pp[i]->print();
    }
    //别忘了delete相应的空间
    return 0;
}

静态指针数组是定义了一些的Point指针,通过new的方式来使每个指针指向对象。动态指针数组是分配固定大小的连续空间,里面的每个元素类型是Point *,然后把这连续空间的首地址给pp,pp是指向Point *类型的指针。

注意是 new Point *[5]而不是 new (Point *)[5],后者编译器会报错
new多维数组

int *p=new int[2];
int *p=new int[2][3];  //错误
int (*p)[3]=new int[2][3];  //正确
int (*p)[4][5]=new int[2][4][5]; //正确

为啥第二行就错误呢?其实我们可以这样理解:

int §[3]其中p是指针,这是毋庸置疑的,那这个指针指向什么类的数据呢?我们改一下写法,编程int[3] §,是不是就很简洁明了呢?p指向的类型是int[3]。
new int[2][3]返回的2个int[3]类型的数组的首地址,赋值给p。
同理,第四行p指向的类型是int[4][5],new int[3][4][5]返回的是3个int[4][5]类型的数组的首地址,赋值给p。
针对第一行的情况,如果p的一开始地址为1000,假设int占用4个字节,那么执行p++以后,p的地址为1000+4,第三行的情况,如果p的一开始地址为1000,进行p++以后,p的请值为1000+43,以此类推,第四行p++以后p的值为1000+44*5。

方法1

QT 的动态二维数组创建方法
记得要delete,不然容易出现一堆野指针的bug;

int **a;
int row = 2;//行数
int col = 3;//列数
a = new int*[row]
for(int i = 0 ;i < row ;i++)
{
	a[i] = new int[col]
}

2用QVector容器

int row = 2;
int col = 3;
QVector<QVector<int>> qv1(row);
for(int i = 2;i < row;i++)
{
	qu1[i].resize(col);
}
//如果在头文件中声明行数不确定
QVector<QVector<int>> qv2;
qv2.resize(row);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值