associative array 2 deep

Id like to make an array that can contain any type of data, so I guess I would have to use the string type.

Basically its going to store results from a database.

Id like it to be like this, where x is the ROW # fetched:

array[x][column_name] = "data of some type";

I can figure out how to loop thru and fill these in once I get the structure right.

Could I do:

 
std:map <string,string> dataArray; 


What im not sure about is how to get that 2nd level that holds the actual data.

In php it would be easy:

 
$array[$row][$column] = $data;


Im sure this is simple for most of you, I appreciate the time and help!

If you want to get the whole column of the array (or vector, or whatever), just only use the first part, i.e.:

1
2
int myTable[3][3] = {{3,3,3},{2,2,2},{1,1,1}};
myTable[1]; //gets second "row" {2,2,2} 


Also, for std::map info, read:
http://www.cplusplus.com/reference/stl/map/

I'm not completely sure what you mean by
get that 2nd level that holds the actual data.
though...
Last edited on
Thanks, but im looking for how to initialize such an array, also it has to be able to handle a string as key & data.
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.
For two dimensions, can't you just concatenate the strings with a divider (a char otherwise not in the strings):

1
2
3
4
5
string s;
s = "hello";
s += "|";
s += "there";
theMap[ s ] = value;

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?
As you didn't like my last suggestion (a kind of pseudo-2D array using string concatenation), how about this.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
   map< string, map< string, string > >  data;
   data[ "hey" ][ "yo" ] = "whassup";
   cout << data[ "hey" ][ "yo" ];
}


If your first dimension is actually int, change the first string to int.

> entering tabs
Type in three or four spaces.
Or type your code in another editor and paste it in.
Ahh, thanks Hammurabi,

I think thats what Im looking for.

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.

1
2
3
4
   map< string, string > data2;
   string str = string("hey") + '\0' + "yo";
   data2[ str ] = "whatever";
   cout << data2[ str ];


But that means you can't access the c_str() of str normally. Although, since you know it's "2D", you could do something like:

1
2
3
4
// Get first dimension value.
cout << str.c_str() << '\n';
// Get second dimension value.
cout << str.c_str() + strlen( str.c_str() ) + 1 << '\n';

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.
Topic archived. No new replies allowed.