Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C++
Article

New Dynamic Array Create array with integer and string indexs dynamically

Rate me:
Please Sign up or sign in to vote.
2.14/5 (7 votes)
11 May 20072 min read 37.4K   10   6
Article that shows how to create array of any type with int and string index

Introduction

This article shows how to create array dynamically with int and string index .

Background

This article assumes you are familiar with Link List and operator overloading in C++.because it is totally build on this knowledge. subscript operaotor is overloaded so we can create array dynamically and to know whether this subscript operator is on Left hand side ot right hand side we have created another class name ArrayItem that is maintaining link list and we have overloaded int,char* and double type etc.. so we can identify this is right hand side value . the version of subscript operator with const keyword that we used for right hand side value was not working. so i use different style mention above. i have made different function so this class could look useful others can also add function to this class an let me know to update this article.

Using the code

NewDynamicArray<int> NDA ;
 NDA["asd"]=1;   //will create new index
 int i=NDA["asd"]; //rhs value
 NDA[1]=3;       //will create new index
 printf("%d",(int)NDA["murtza"]); //rhs value
 NDA[1]=2; //will overwrite existing index
 i=NDA[1];  //rhs value

 while(NDA.MoveNext()) //will print all value
     printf("%s",(int)NDA.Ite->value);
 NDA.ReSetIterator(); //will movbe iterator to point to first value

 NewDynamicArray<char*> NDa;
 NDa["age"]="twenty one"; //will create new index
 NDa["dob"]="07-aug-1985"; // will create new index
 NDa["age"]="08-aug-1985";  // overwrites existing one
 printf("%s",(char*)NDa["dob"]);
>These are the functions of NewDynamicArray Class .
void Insert(const int index,const Type& T); //

this function inserts the value in array .if index exist already than overwrites existing value.this inserts function takes index of type int;

void Insert(char* index,const Type& T);

this function inserts the value in array .if index exist already than overwrites existing value.this inserts function takes index of type string(char*);

bool IsIndexExist(int index);

This Function Checks whether given index exist already or not .this function takes index of type int;

bool IsIndexExist(char* index);

This Function Checks whether given index exist already or not .this function takes index of type int;

Type& Get(const int index);

This functions get the value of given index .this is functions is for index of type int.

Type& Get(const char* index);

This functions get the value of given index .this is functions is for index of type string.

bool IsValueExist(Type value);

This Function Checks whether given Value exist already or not .

void RemoveByIndex(char* index);

This Function Remove Value by Index of type string.

void RemoveByIndex(int index);

This Function Remove Value by Index of type itn.

void RemoveByValue(Type value);

This Function Remove Value by input Value.

bool MoveNext();

This function is to iterate the link list so we can access value.

void ReSetIterator();

this function is to reset iterator .so iterator can point to first value.

This class can be very useful any many ways

1)Index of any type string or int can be created .

2)Value can be retrieve easily and flexibly.

3)User can also include their own functions.

4)This is template class any data type can be use


.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Pakistan Pakistan
Ali, Murtaza Tahir

http://murtazadharis.blogspot.com/

Comments and Discussions

 
QuestionI Can't includes newdynamicarray.h Pin
Omid Rashvand13-Nov-07 22:32
Omid Rashvand13-Nov-07 22:32 
AnswerRe: I Can't includes newdynamicarray.h Pin
murtaza dhari19-Nov-07 19:12
murtaza dhari19-Nov-07 19:12 
GeneralWhy is this better than Pin
Jim Crafton11-May-07 5:40
Jim Crafton11-May-07 5:40 
GeneralRe: Why is this better than Pin
murtaza dhari11-May-07 8:43
murtaza dhari11-May-07 8:43 
In This STL Map class u cannot use two different type of indexes that i have used.int and string .
NDA["foo"]=10;
NDA[10]=10;

If all the tree become pen and all ocean and many more other ocean become ink even then the ALLAH praise could not be written completely. (AL-Quran)

GeneralSuggestion Pin
Hans Dietrich11-May-07 4:53
mentorHans Dietrich11-May-07 4:53 
GeneralIt looks really sexy, believe me ! Pin
Kochise11-May-07 4:47
Kochise11-May-07 4:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.