C++数组声明的方式

指针数组:array of pointers,即用于存储指针的数组,也就是数组元素都是指针

数组指针:a pointer to an array,即指向数组的指针

还要注意的是他们用法的区别,下面举例说明。

int* a[4] 指针数组

             表示:数组a中的元素都为int型指针    

             元素表示:*a[i]   *(a[i])是一样的,因为[]优先级高于*

int (*a)[4] 数组指针

             表示:指向数组a的指针
             元素表示:(*a)[i]   

3维

第一种:

dynamic_array.h

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H


class DynamicArray
{
public:
    DynamicArray();
    double*** allocate3DoubleArray(int x, int y, int z);
    void release3DoubleArray(double*** the_array, int x, int y, int z);
};

#endif // DYNAMICARRAY_H

dynamic_array.cpp

#include "dynamic_array.h"

DynamicArray::DynamicArray()
{

}

double*** DynamicArray::allocate3DoubleArray(int x, int y, int z)
{
    double*** the_array = new double**[x];
    for(int i(0); i < x; i++)
    {
        the_array[i] = new double*[y];

        for(int j(0); j < y; j++)
        {
            the_array[i][j] = new double[z];

            for(int k(0); k < z; k++)
            {
                the_array[i][j][k]= 0.;
            }
        }
    }
    return the_array;
}

void DynamicArray::release3DoubleArray(double*** the_array, int x, int y, int z)
{
    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            delete [] the_array[i][j];
        }
        delete [] the_array[i];
    }
    delete [] the_array;
}

第二种

    float(*a)[100][100]=new float [100][100][100];

    delete [] a;

第三种也是目前比较常用的一种:智能指针。

见:http://blog.csdn.net/philosophyatmath/article/details/50541459

vector 数组

#include <iostream>
#include <vector>
int main()
{
     std::vector< std::vector<int> > v(100, std::vector<int>(100));
     v[99][99] = 7;
     std::cout << v[99][99] << '\n';
}
#include <iostream>
#include <memory>

struct Vec3
{
    int x, y, z;
    Vec3() : x(0), y(0), z(0) { }
    Vec3(int x, int y, int z) :x(x), y(y), z(z) { }
    friend std::ostream& operator<<(std::ostream& os, Vec3& v) {
        return os << '{' << "x:" << v.x << " y:" << v.y << " z:" << v.z  << '}';
    }
};

int main()
{
    // Use the default constructor.
    std::unique_ptr<Vec3> v1(new Vec3);
    // Use the constructor that matches these arguments
    std::unique_ptr<Vec3> v2(new Vec3(0, 1, 2));
    // Create a unique_ptr to an array of 5 elements
    std::unique_ptr<Vec3[]> v3(new Vec3[5]);

    std::cout << "make_unique<Vec3>():      " << *v1 << '\n'
              << "make_unique<Vec3>(0,1,2): " << *v2 << '\n'
              << "make_unique<Vec3[]>(5):   " << '\n';
    for (int i = 0; i < 5; i++) {
        std::cout << "     " << v3[i] << '\n';
    }
}

array

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

int main ()
{
  //--这是1维数组
  array<float,5> a1={1,2,3,4,5};
  cout <<"a1="<<endl;
  for (size_t n=0; n<a1.size(); n++){
      cout << a1[n] <<'\t';
    }
  cout << endl;
  //当然也可以使用
  cout <<"a1="<<endl;
  for (size_t n=0; n<a1.size(); n++){
      cout << a1.at(n) << '\t';
    }
  cout << endl;
  //-----------------------------------------------
  //--这是2维数组,共3行2列
  array<array<float,2>,3 > a2={1.1,2,3,4,5,6};
  //-----------------------------------------------
  cout <<"a2="<<endl;
  for (size_t m=0; m<a2.size(); m++){
      for (size_t n=0; n<a2[m].size(); n++){
          cout << a2[m][n] <<'\t';
        }
      cout << endl;
    }
  //-----------------------------------------------

  return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值