I'm not aware of a way to implement a multi-dimensional associative array with std::map. I'm not sure how you went from needing an array of rows/columns to multi-dimensional arrays with string keys. I'm having a hard time imagining how a multi-dimensional array accessed by string keys would be possible.
Sorry kempofighter, I think I got lazy in my terminology... thus .. the confusion.
Im trying to learn C but I still think in php, which is a problem.
for instance, Id like to do this .. in C
1 2 3 4 5 6 7 8 9 10
while (fetching rows from the DB) {
array[1]['username'] = "papa smurf"; // literally
// so it would be :
array[row][column]=data;
}
Maybe its easier than I think and I'm over complicating it thru lack of knowledge.
I think I could do this if I knew how to set up the array in the first place. There is a variable number of rows, and columns, and the data variable can be numbers, or strings.
ps .. anyone know how to make a tab in the code on this forum?
Your first idea got me thinking ... I was trying to determine if there was a char that would not come up in any data result set ... I couldnt come up with one, so I had started to think about a possible string, like a tag. "[divider]" or something like that.
But I was convinced with the power of c++ there should be a way to do it in an array, which it appears you have given me.
thanks so much to everyone who replied!
When I get this project done, ill let you guys see what you helped me create. I think its a great idea in concept, it has to do with sql + php + apache but for speed I decided to build it in c++
Do any of you guys ever work together on projects, like open source stuff, or anything like that?
Depending on exactly how you will be using it, the pseudo-2D map might be more efficient. Since C++ strings allow the null char, you can use that as a seperator.
I have seen 2d maps implemented as Hammurabi suggested, so I don't know what I was thinking yesterday. I guess I had a brain fart late in the day. As he suggested, the keys for each map could be different types if you need them to be. In that case you'd want to implement a 2D array.
If both keys are strings, then think about it like this. if you had a string key that is a person's name, you would probably do a 1D array and a key could be "steve johnson" or "melissa hendricks". You wouldn't do a 2D array where the first key is the first name and the second key is the last name. If you had strings representing state and city, you could have "san diego, ca" as a key rather than having one key be the city and the second key being the state or vice versa. In the latter case, an argument could certainly be made that it could be better to separate the city and state strings as separate keys. You could have a multimap of states since there are numerous cities per state. That way each state key could be associated with numerous city maps where the value might be a structure containg stats or metrics about that city. You could dream up lots of scenarios that might change your mind about how to organize the data. Yesterday, I wasn't sure what your requirements were so I was having a hard time thinking of a scenario using 2D arrays with string keys. Also in that latter example is a bit more complicated because it involved multimaps. In the case of map/multipmap they are templates so the value type can be whatever you want. You could even have a map where the value is a vector object or a pointer to some user defined type. It doesn't have to be another map.