LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   creating array in c++ with string index (https://www.linuxquestions.org/questions/programming-9/creating-array-in-c-with-string-index-536044/)

mohtasham1983 03-09-2007 12:16 PM

creating array in c++ with string index
 
I am wondering if it is possible to create an array whose index are string.

For example I want to create a 2 dimensional array which looks like this:

array["first"]["second"]=8;

then I want to make a loop to verify each elements of my array.

In PHP we have foreach, but I couldn't find the equivalent command in c++.

tuxdev 03-09-2007 06:37 PM

Arrays are Evil!

what you want is a std::map<std::string, std::map<std::string, int> > (not the space in between the last two angle brackets, it is required).

there is a for_each in the STL <functional> header. It's a bit complicated by itself, but with boost::lambda and/or boost::bind it's a win.

The_eXXe 03-11-2007 02:27 PM

Quote:

Originally Posted by mohtasham1983
I am wondering if it is possible to create an array whose index are string.

For example I want to create a 2 dimensional array which looks like this:

array["first"]["second"]=8;

then I want to make a loop to verify each elements of my array.

In PHP we have foreach, but I couldn't find the equivalent command in c++.

I am not quite sure, but i think it can be done with enum in JAVA. I think C++ has the same option. try enum

random0 03-11-2007 04:01 PM

The enum keyword is used in C++ to create enumerated constants. STL maps are the way to go on this one.


All times are GMT -5. The time now is 05:55 PM.