Array of Pointers can be used to do fast sorting.
For eg. If you want to sort a unsorted array of integer. You generally can use all sorts of algorithms on the int array. But it would be less efficient as the runtime head for moving the values of int array will be more.
Now other alternative is to sort by using array of pointers. Say, now, you create array to int pointers (int*) and let each one of them point to the corresponding elements of the integer array. Now when you need to sort them, just re-locate the pointers in correct order, which would be faster.
Read: http://www.gidforums.com/t-10801.html
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
you can use them to dynamically initialize memory (mean that at runtime u decide the number of elements of the array)
for both 2d And 1d
arshad115
Junior Poster in Training
65 posts since Nov 2008
Reputation Points: 7
Solved Threads: 2
You can use them e.g. for a table of char-strings:
#include <iostream>
using namespace std;
int main(void)
{
/* Declare the '2D Array' */
char ** ptr = new char * [5];
ptr[0] = new char[20];
ptr[1] = new char[20];
ptr[2] = new char[20];
ptr[3] = new char[20];
ptr[4] = new char[20];
/* Put some data in the array */
ptr[0] = "Hello ";
ptr[1] = "wond";
ptr[2] = "er";
ptr[3] = "ful";
ptr[4] = " world !!!";
/* Print the array on the screen */
for(int i = 0; i < 5; i++)
cout << ptr[i];
cout << endl;
/* Cleanup */
delete[] ptr;
/* Wait for the user to press ENTER */
cin.get();
/* Tell the Operating System that everything went well */
return 0;
}
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243