Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

New Dynamic Array Create array with integer and string indexs dynamically

2.14/5 (7 votes)
11 May 20072 min read 1   259  
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