1. C version - one dimension array |
#include <stdlib.h> // for malloc, realloc, free #include <stdio.h>/* void * malloc ( size_t size ); - Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. */ /* void free ( void * ptr ); - Deallocate space in memory previously allocated using a call to malloc, - calloc or realloc is deallocated, making it available again for further allocations. */ /* void * realloc ( void * ptr, size_t size ); - Reallocate memory block. - The content of the memory block is preserved up to the lesser of the new and old sizes. - If the new size is larger, the value of the newly allocated portion is indeterminate. */ int main(){
} |
2. C version - two dimension array - pointer to pointer |
#include <stdlib.h> #include <stdio.h>#include <time.h>/* int **arr ----> |------| | int* | ---> | int int | | int* | ---> | int int | | int* | ---> | int int | |------| || || || \/ m x n 2-D array */
int main(){
}
|
3. C++ version - one dimension array |
#include <cstdlib> #include <cstdio>#include <iostream>using namespace std;// Type *array = new Type(size); // delete array; int main(){
}
|
4. C++ version - two dimension array - pointer to pointer |
#include <cstdlib> #include <cstdio>#include <ctime>#include <iostream>using namespace std;/* int **arr ----> |------| | int* | ---> | int int | | int* | ---> | int int | | int* | ---> | int int | |------| || || || \/ m x n 2-D array */
int main(){
for (int i = 0; i < m ; i++){
} delete[] arr; return 0; } |
5. C++ version - standard template library (STL) - vector - one and two dimension array |
#include <vector> #include <string>#include <iostream>using namespace std;int main(){
} |
沒有留言:
張貼留言