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.