Free Educational Resource Center for teachers and students. Includes Interviews,
Sourcecode, Free Software, Research Papers, Articles, Tutorials and much more..
     R E S E A R C H A C T I V I T Y . C O M
Our Fellow Research Center for Ph.D Schollars
Home About Submit & Earn Archives: C And C + + Programming » Dev Packages » Interviews » Php Mysql Programming » Windows Programming
search
C And C Plus Plus Programming > implementing associative arrays in c plus plus

Implementing associative arrays in C/C++



This article demonstrates using string/char* indexes in regular C array, e.g. myArray["age"]=10;

We usually use numaric indexes in arrays with C and C++. But there can be another way of using C / C++ arrays like in PHP, e.g.



//create arrays
Array <std::string> info; //create array of string
Array <float> extra; //create array of float

//add data
info["name"] = "John";
info["title"] = "RAD C++ Programmer";

extra["age"] = 29;
extra["salary"] = 5000;

//use/display the data
cout << info["name"] << " earns $" << extra["salary"];



Isn’t it great ? I hope you would say offcourse!

To do this we will be using C++ tempaltes programming.

Now what you have to do is, download the header file called ctia.h here and include in your program. Better is put it in your compiler’s include folder. I will not be forcing you to learn templates programming first, but just you a GO to use my premade class.


A sample program would be:


#include <ctia.h>

int main() {

Array <std::string> info;

Array <float> extra;


info["name"] = "John";
info["title"] = "RAD C++ Programmer";

extra["age"] = 29;
extra["salary"] = 5000;

cout << info["name"] << " earns $" << extra["salary"];

system("PAUSE");

return EXIT_SUCCESS;
}