Introduction to dynamic two dimensional arrays in C++
来源:互联网 发布:windows linux子系统 编辑:程序博客网 时间:2024/05/08 08:20
Introduction
A dynamic array is an array data structure that can be resized and which allows elements to be added or removed.
There are many ways of creating two dimensional dynamic arrays in C++.
1. Pointer to pointer
First, we will allocate memory for an array which contains a set of pointers. Next, we will allocate memory for eacharray which is pointed by the pointers. The deallocation of memory is done in the reverse order of memory allocation.
int **dynamicArray = 0;//memory allocated for elements of rows.dynamicArray = new int *[ROWS] ;//memory allocated for elements of each column.for( int i = 0 ; i < ROWS ; i++ )dynamicArray[i] = new int[COLUMNS];//free the allocated memoryfor( int i = 0 ; i < ROWS ; i++ )delete [] dynamicArray[i] ;delete [] dynamicArray ;
The above code is for integer values. We can use the template to operate with generic types. In the below example, for the memory allocation, theAllocateDynamicArray
function templates are used, and to free the memory,FreeDynamicArray
is used.
template <typename T> T **AllocateDynamicArray( int nRows, int nCols){ T **dynamicArray; dynamicArray = new T*[nRows]; for( int i = 0 ; i < nRows ; i++ ) dynamicArray[i] = new T [nCols]; return dynamicArray;}template <typename T>void FreeDynamicArray(T** dArray){ delete [] *dArray; delete [] dArray;}int main(){ int **my2dArr = AllocateDynamicArray<int>(4,4); my2dArr[0][0]=5; my2dArr[2][2]=8; cout << my2dArr[0][0] << my2dArr[0][1] << endl; cout << my2dArr[1][1] << my2dArr[2][2]<< endl; FreeDynamicArray<int>(my2dArr); return 0;}
2. Vector of vector
The above code can be done in one line of code by using a vector of vector, i.e., a vector containing anarray of vectors.
vector<vector<T> > dynamicArray(ROWS, vector<T>(COLUMNS));#include <vector>using namespace std;#define ROWS 4#define COLUMNS 4//vector<int > vec(5, 7); create a vector with //5 elements and each element will have the value 7.vector<vector<int> > dynamicArray(ROWS, vector<int>(COLUMNS));for(int i = 0;i < dynamicArray.size();++i){ for(int j = 0;j < dynamicArray[i].size();++j) { dynamicArray[i][j] = i*j; }} for(int i = 0;i < dynamicArray.size();++i){ for(int j = 0;j < dynamicArray[i].size();++j) { cout << dynamicArray[i][j] << endl; }}
3.Vector wrapper class
We have created a wrapper class DynamicArray
with avector<vector<T> > dArray
data member. Then, we pass the number of rows and columns as arguments in the constructor.
template <typename T>class DynamicArray{public: DynamicArray(){}; DynamicArray(int rows, int cols): dArray(rows, vector<T>(cols)){} vector<T> & operator[](int i) { return dArray[i]; } const vector<T> & operator[] (int i) const { return dArray[i]; } void resize(int rows, int cols)//resize the two dimentional array . { dArray.resize(rows); for(int i = 0;i < rows;++i) dArray[i].resize(cols); }private: vector<vector<T> > dArray; };void Matrix(int x, int y){ DynamicArray<int> my2dArr(x, y); my2dArr[0][0] = -1; my2dArr[0][1] = 5; cout << my2dArr[0][0] << endl; cout << my2dArr[0][1] << endl;}int main(){ Matrix(2,2); return 0;}
- Introduction to dynamic two dimensional arrays in C++
- Two-dimensional arrays as arrays of arrays
- Recipe 4.8. Transposing Two-Dimensional Arrays
- The two-dimensional pointer operation in C++
- An Introduction to Interactive Programming in Python - Week two
- 动态二维数组——The dynamic two-dimensional array
- Ruby array of two dimensional arrays, search/lookup?
- Is it possible to get the difference from two dynamic arrays regardless of their order?
- Keep to the Grain in Dimensional Modeling
- An Introduction to Dynamic Management Views and Functions in SQL Server 2005
- Introduction to Programming in C/C with Vim
- Two Dimensional TInt Array in Symbian(二维数组)
- Introduction to Algorithm ( chapter 15: Dynamic Programming)
- Introduction to Programming in C/C++ with Vim
- Introduction to Programming in C/C++ with Vim
- An Introduction to XML Data Binding in C++(转载)
- Introduction to AspectDNG in Details.
- Introduction to SDET in Microsoft
- 杭电2031
- GPF简介
- cocos2dx提供的调试时打印日志的方法
- Setup Spring MVC Development Environment: Eclipse Juno with Tomcat 7
- OBJECT-C 类别
- Introduction to dynamic two dimensional arrays in C++
- IOS QQ第三方登陆
- linux驱动 结构体中的逗号
- 如何在外网中访问自己在另一个局域网中的某个机器(SSH为例)
- 进度条Dialog线程
- C++string str="abc";sizeof(str)
- F460破解解惑
- ACRush 楼天城回忆录
- 《TCP/IP协议详解》ARP与RARP(第四章与第五章)