zoukankan      html  css  js  c++  java
  • C++创建m*n数组

    今天刷题发现一道动态规划题目要用到二维数组,但必须是根据参数来控制数组规模。查看博客发现这几类定义方法:

    • 使用指针
        int** a = new int*[rows];
        for(int i = 0; i < rows; i++)
        {
            a[i] = new int[columns];
        }
        //释放空间
        for(int i = 0; i < rows; i++)
        {
            delete []a[i];
        }
    

    1、new分配数组对象格式为:

    new int[n]; //new+类型+[] 且未初始化
    new int[n](); //初始化为0

    2、new之后一定要delete

    delete p; //删除对象
    delete []p; //删除数组

    • 使用vector
    vector<vector<int>> b(rows, vector<int>(columns));
    
    vector<vector<int>> b2(rows);
    for(int i = 0; i < rows; i++)
    {
        b2[i].resize(columns);
    }
    
    • 使用malloc
    int** c = (int**)malloc(sizeof(int)*rows);
    for(int i = 0; i < columns; i++)
    {
        c[i] = new int[columns];
    }
    
    • 特殊
      今天刷题看到一种这样的定义:
      bool dp[m][n] = {0};竟然也是可以用的

    总结

    1.vector创建数组在定义后面加()补充数组的size
    2.new和创建二维数组需两步且都需定义**a
    3.注意区分**a,(*a)等区别

  • 相关阅读:
    logistics regression
    dir 以及 attrib
    python 爬取有道翻译
    spss 逐步回归
    JQuery传值给.ashx乱码
    字符串转换成json的三种方式
    启动数据库SQL Server Service Broker
    ASP.NET两种缓存方式
    VS安装控件后报错
    sql server中有不是全数字的字符串
  • 原文地址:https://www.cnblogs.com/leflew/p/12430141.html
Copyright © 2011-2022 走看看