C++多维数组的声明和使用
2015-01-15 19:04
260 查看
原贴地址:点这里。
This is another topic we get asked quite a bit.
- How do I do a 2D/3D Array?
When I also started working with multi-dimensional arrays I found it hard to find the answers I wanted too. So I'll post up some info that will hopefully help other people. I'll go over both of the
2 major methods (Vector vs Pointer).
Vector based multi-dimensional arrays
Vectors are a STL container that allow you to store pretty much anything in them. When used correctly they can be very powerful containers.
They provide an added benefit that they will automatically remove the memory they use when they go out of scope. This means that objects stored within a vector do not need to be de-allocated (but
pointers to objects do).
You can also do some interesting things with dynamic multi-dimensional arrays with vectors. For example, if you only allocate the first dimension, then use the .push_back() to add records to the 2nd
dimension it's no longer a grid, but an array with a dynamically sized 2nd dimension (much like a street of buildings each with a different amount of floors). This functionality can be achieved using pointers, but is much harder to do.
A simple 2D Array with vectors:
A 3D Array with vectors.
Pointer based multi-dimensional arrays
Pointer based multi-dimensional arrays provide you with a more raw access to the objects. The benefits can be added speed and you can
apply custom optimizations to them.
Note: There are ways you can optimize this by combining the 2 dimensions into a single dimension (HEIGHTxWIDTH). I leave the discussion of this out, as it's a more advanced topic for people already
familiar with this topic.
A simple 2D Array:
A 3D Array:
One final thing to note. When creating dynamic arrays with your own object types, you cannot overload the constructor. The ISO standard forbids this, and you must initialize the values on the objects later. All objects allocated into arrays must utilize
the default constructor.
The above code snippets should compile on both Windows and Linux with no
problems.
Recommendations
Unless your application has an extreme need to be highly optimized, and
you are quite proficient at C++ memory management, you will want to use the vector based approach. This method is a lot easier to manage, especially if you are just learning C++.
This is another topic we get asked quite a bit.
- How do I do a 2D/3D Array?
When I also started working with multi-dimensional arrays I found it hard to find the answers I wanted too. So I'll post up some info that will hopefully help other people. I'll go over both of the
2 major methods (Vector vs Pointer).
Vector based multi-dimensional arrays
Vectors are a STL container that allow you to store pretty much anything in them. When used correctly they can be very powerful containers.
They provide an added benefit that they will automatically remove the memory they use when they go out of scope. This means that objects stored within a vector do not need to be de-allocated (but
pointers to objects do).
You can also do some interesting things with dynamic multi-dimensional arrays with vectors. For example, if you only allocate the first dimension, then use the .push_back() to add records to the 2nd
dimension it's no longer a grid, but an array with a dynamically sized 2nd dimension (much like a street of buildings each with a different amount of floors). This functionality can be achieved using pointers, but is much harder to do.
A simple 2D Array with vectors:
#include <vector> using std::vector; #define HEIGHT 5 #define WIDTH 3 int main() { vector<vector<double> > array2D; // Set up sizes. (HEIGHT x WIDTH) array2D.resize(HEIGHT); for (int i = 0; i < HEIGHT; ++i) array2D[i].resize(WIDTH); // Put some values in array2D[1][2] = 6.0; array2D[3][1] = 5.5; return 0; }
A 3D Array with vectors.
#include <vector> using std::vector; #define HEIGHT 5 #define WIDTH 3 #define DEPTH 7 int main() { vector<vector<vector<double> > > array3D; // Set up sizes. (HEIGHT x WIDTH) array3D.resize(HEIGHT); for (int i = 0; i < HEIGHT; ++i) { array3D[i].resize(WIDTH); for (int j = 0; j < WIDTH; ++j) array3D[i][j].resize(DEPTH); } // Put some values in array3D[1][2][5] = 6.0; array3D[3][1][4] = 5.5; return 0; }
Pointer based multi-dimensional arrays
Pointer based multi-dimensional arrays provide you with a more raw access to the objects. The benefits can be added speed and you can
apply custom optimizations to them.
Note: There are ways you can optimize this by combining the 2 dimensions into a single dimension (HEIGHTxWIDTH). I leave the discussion of this out, as it's a more advanced topic for people already
familiar with this topic.
A simple 2D Array:
#define HEIGHT 5 #define WIDTH 3 int main() { double **p2DArray; // Allocate memory p2DArray = new double*[HEIGHT]; for (int i = 0; i < HEIGHT; ++i) p2DArray[i] = new double[WIDTH]; // Assign values p2DArray[0][0] = 3.6; p2DArray[1][2] = 4.0; // De-Allocate memory to prevent memory leak for (int i = 0; i < HEIGHT; ++i) delete [] p2DArray[i]; delete [] p2DArray; return 0; }
A 3D Array:
#define HEIGHT 5 #define WIDTH 3 #define DEPTH 7 int main() { double ***p2DArray; // Allocate memory p2DArray = new double**[HEIGHT]; for (int i = 0; i < HEIGHT; ++i) { p2DArray[i] = new double*[WIDTH]; for (int j = 0; j < WIDTH; ++j) p2DArray[i][j] = new double[DEPTH]; } // Assign values p2DArray[0][0][0] = 3.6; p2DArray[1][2][4] = 4.0; // De-Allocate memory to prevent memory leak for (int i = 0; i < HEIGHT; ++i) { for (int j = 0; j < WIDTH; ++j) delete [] p2DArray[i][j]; delete [] p2DArray[i]; } delete [] p2DArray; return 0; }
One final thing to note. When creating dynamic arrays with your own object types, you cannot overload the constructor. The ISO standard forbids this, and you must initialize the values on the objects later. All objects allocated into arrays must utilize
the default constructor.
The above code snippets should compile on both Windows and Linux with no
problems.
Recommendations
Unless your application has an extreme need to be highly optimized, and
you are quite proficient at C++ memory management, you will want to use the vector based approach. This method is a lot easier to manage, especially if you are just learning C++.
相关文章推荐
- VB中如何声明及使用多维数组,多层数组及动态数组
- c++ 多维数组 的使用
- C++中关于复杂的多维数组声明的总结
- 使用c++进行多维数组遍历的方法
- Java & C++ 数组声明和使用语法对比
- VB中如何声明及使用多维数组,多层数组及动态数组
- C#语言多维数组的声明与使用
- C++中的动态多维数组
- 使用 ASP.NET 创建 XML Web services 不支持多维数组
- C++中函数指针数组的使用
- C++ 为什么要同时使用*和&符号?以及什么场合使用这种声明方式?
- C++中动态数组CArray的使用
- Java编程那些事儿50—多维数组使用示例2
- c++中全局变量的声明与使用
- C++中的动态多维数组
- 在C++中如何将ActiveX控件所使用的BSTR数据转换成CHAR字符数组?
- [转载]c/c++指针与数组的使用与区别
- [C/C++]数组的引用、多维数组
- C++中函数指针数组的使用
- C++ 用new 动态创建多维数组