Click here to Skip to main content
11,638,173 members (76,496 online)
Click here to Skip to main content

New Dynamic Array Create array with integer and string indexs dynamically

, 11 May 2007 26.2K 222 10
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

murtaza dhari
Software Developer
Pakistan Pakistan
Ali, Murtaza Tahir

http://murtazadharis.blogspot.com/

You may also be interested in...

Comments and Discussions

 
QuestionI Can't includes newdynamicarray.h Pin
Omid Rashvand13-Nov-07 22:32
memberOmid Rashvand13-Nov-07 22:32 
AnswerRe: I Can't includes newdynamicarray.h Pin
murtaza dhari19-Nov-07 19:12
membermurtaza dhari19-Nov-07 19:12 
GeneralWhy is this better than Pin
Jim Crafton11-May-07 5:40
memberJim Crafton11-May-07 5:40 
GeneralRe: Why is this better than Pin
murtaza dhari11-May-07 8:43
membermurtaza dhari11-May-07 8:43 
GeneralSuggestion Pin
Hans Dietrich11-May-07 4:53
mvpHans Dietrich11-May-07 4:53 
GeneralIt looks really sexy, believe me ! Pin
Kochise11-May-07 4:47
memberKochise11-May-07 4:47 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 11 May 2007
Article Copyright 2007 by murtaza dhari
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid