2 Dimentional Maps : Passing values from a map to a function.

The difficulty I am having is to do how to access data from a 2d map of definition map<string, map<string, Data_class> > ; Particularly how to access every instance of a specific attribute of Data_class. Eg. Attribute = number or number2 (see code for clarification).
I think the code below better describes the problem I am having.

Even a suggestion no matter how partial would help.

Thank you to anyone who is able to help me. Your help is GREATLY appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//Appropriate #include and using directives

class Data_class
{
    double number;
    int number2;

public:
    double onumber() const
    {
        return number;
    }

    double inumber(double in)
    {
        number = in;
    }


    int onumber2() const
    {
        return number2;
    }

    int inumber2(double in)
    {
        number2 = in;
    }
};


typedef map<string, Data_class>                                 d1_map;
typedef map<string, map<string, Data_class> >                   d2_map;

class Map_class
{
public:
    d2_map Data_map;
};

//The function I am having trouble with
std::string access_to_map(d2_map& in) //Or however the parameters should be declared
{
         //Do something with all (number or number2). "All" as in every instance
         //of a and b for (number or number2). "Do something" for eg calculate
         //average of all (number or number2).
         
         //return something of type double or int;
}

int main()
{
    Map_class obj;

//Example data
    obj.Data_map["qwer"]["qwer"].inumber(1);
    obj.Data_map["qwer"]["rewq"].inumber(2);
    obj.Data_map["qwer2"]["ter"].inumber(5);
    obj.Data_map["qwer2"]["ret"].inumber2(3);
    obj.Data_map["qwer2"]["1324"].inumber2(4);


//Data_map[a][b].Data_class;    Assume a and b are instances of the map

//Again the problematic function
    access_to_map(//number or number2 )
    {
         //Do something with all (number or number2). "All" as in every instance
         //of a and b for (number or number2). "Do something" for eg calculate
         //average of all (number or number2).
    }
    return 0;
}

I don't understand what you're asking. Could you rephrase it in clearer terms?

Also, may I suggest this?
typedef std::map<std::pair<std::string,std::string>,Data_class> d2_map;
Your method copies big structures every time the outer map is updated. This one however possibly introduces a lot of data redundancy, but that's why it's merely a suggestion. Which is best depends on what you're doing. There are, of course, ways to get the best out of both worlds.
Thank you for your constructive criticism Helois .
I will look into your suggestions, I am certain I will find it useful.

For now , to clarify my description of the problem I am having. I have written a functional but hacky and very non-generic code. You will see what I mean by non-generic.
It works, but only for predefined class attribute names predefined inside the function.
I would like to know how to write it to be more generic for any map of type map<string, map<string,class> > that might have calculable attributes inside class.

Note: I have renamed access_to_map to avg for clarity purposes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Appropriate #include and using directives

class Data_class
{
    double number;
    int number2;

public:
    double onumber() const      //Accessor function to private data "number"
    {
        return number;
    }

    double inumber(double in)   //Function used to new data to the class
    {
        number = in;
    }


    int onumber2() const        //Accessor function to private data "number"
    {
        return number2;
    }

    int inumber2(double in)    //Function used to new data to the class
    {
        number2 = in;
    }
};


typedef map<string, Data_class>                                 d1_map;
typedef map<string, map<string, Data_class> >                   d2_map;

typedef map<string, Data_class>::const_iterator                 d1_map_const_it;
typedef map<string, map<string, Data_class> >::const_iterator   d2_map_const_it;


class Map_class
{
public:
    d2_map Data_map;
    double avg(bool in);
};
double Map_class::avg(bool in)
{
    double avg = 0;     //avg is used to accumulate the average
    size_t count = 1;   //count is used as part of equation in calculating the average
    if (in)
    {
        for (d2_map_const_it it3 = Data_map.begin(); it3 != Data_map.end() ; ++it3)     //Same as below, just with onumber() instead of onumber2()
        {
            cout << it3->first << "\t" << endl;
            for (d1_map_const_it it4 = it3->second.begin(); it4 != it3->second.end() ; ++it4)
            {
                avg += ((it4->second.onumber() - avg)/count);       //The equation that prevents overflow of avg
                count++;

            }
        }
    }
    else
    {
        for (d2_map_const_it it3 = Data_map.begin(); it3 != Data_map.end() ; ++it3)	//Same as above, just with onumber2() instead of onumber()
        {
            cout << it3->first << "\t" << endl;
            for (d1_map_const_it it4 = it3->second.begin(); it4 != it3->second.end() ; ++it4)
            {
                avg += ((it4->second.onumber2() - avg)/count);       //The equation that prevents overflow of avg
                count++;

            }
        }
    }
    cout << "avg" << avg << endl;
}

int main()
{
    Map_class obj;

//Sample data
    obj.Data_map["qwer"]["qwer"].inumber(1);
    obj.Data_map["qwer"]["rewq"].inumber(2);
    obj.Data_map["qwer2"]["ter"].inumber(5);

    obj.Data_map["qwer2"]["ret"].inumber2(3);
    obj.Data_map["qwer2"]["1324"].inumber2(4);

//Key to understanding what I am trying to say
    obj.avg(0);	// 0 is passed as an argument to calculate the average of every number2 (Not how I would the arguments to be passed , see below for further description)

    return 0;
}


How I really want the function "avg" to work, is for me to pass "number" or "number2"(or any other attribute name that might be in Data_class) as an argument in order for it to calculate the average of the appropriate attribute of the class defined above.

To reiterate , I would like to know how to write it so that I only need to enter the name of the attribute (contained in Data_class) into the argument list of "avg for it to calculate the average of every instance of that attribute.

For simplicity purposes let us assume that every possible attribute is calculable.

Again , Thank you for your time.
I do not know if this is acceptable forum etiquette . Let me know if it is not.

Bump...
Topic archived. No new replies allowed.