C++数组声明的方式
2016-04-21 09:18
375 查看
指针数组:array of pointers,即用于存储指针的数组,也就是数组元素都是指针
数组指针:a pointer to an array,即指向数组的指针
还要注意的是他们用法的区别,下面举例说明。
int* a[4] 指针数组
int (*a)[4] 数组指针
数组指针: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;
第三种也是目前比较常用的一种:智能指针。
见:/article/10626331.htmlvector 数组
#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 <<'\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] <<'\t'; } cout << endl; } //----------------------------------------------- return 0; }
相关文章推荐
- c++第四次实验——贪财的富翁
- C++走向远洋——37(工资类,2)
- C++上机实验4项目1-利用循环求和
- C++中函数调用时的三种参数传递方式
- c++实验4-百钱白鸡问题
- C++实验4—【项目1:利用循环求和】求1000以内所有偶数的和
- C++课堂实验4
- 用对象数组操作长方柱类
- 用对象数组操作长方柱类
- 用对象数组操作长方柱类
- 用对象数组操作长方柱类
- 用对象数组操作长方柱类
- c++上机实验四——分数的累积求和
- C++实验4-项目7穷举
- C++实验4——项目3:乘法口诀表
- C++实验4—输出星号图
- c++上机实验4项目1-利用循环求和
- C++实验4-循环求和
- c++第四次实验——分数的累加
- C++实验4-输出星号图