Accessing class variables within a vector of this class, in a function

Hi,

I just want to know if I can spare some code by accessing diffrent class variables within vector of this class in a function. My code seems to
me pretty inefficient so far.

1
2
3
4
class Automata{
public:
	vector<map<pair<int, int>, vector<double>>> trans_time_state;	
	vector<map<pair<int, int>, vector<double>>> trans_time_state_trace; 

The class contains of cause also other objects
Now I create a vector and fill it up by push_back
 
vector<Automata> automatas_ref;

and call the function. filep is my sstringstream containing my file path to write the desired data.
 
write_learned_time(&(automatas_ref), filep.str()); 

now the function itself
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
        void write_learned_time(vector<Automata> *automatas_ref, string filepath)
	{
		fstream datei;
		datei.open(filepath.c_str(), ios::out);
		if (!~datei.is_open()) { cout << "Error opening file "<< filepath; }
		typedef map<pair<int, int>, vector<double>>::const_iterator MapIterator;
		if (filepath.find("ref") != string::npos) {
			for(int k = 0; k < (*automatas_ref).size(); k++) {
				if ((*automatas_ref).at(k).num_sym > 0) {
					datei << (*automatas_ref).at(k).name;
					for(int j = 0; j < (*automatas_ref).at(k).trans_time_state.size(); j++)	{
						if ( (*automatas_ref).at(k).trans_time_state.at(j).begin() != (*automatas_ref).at(k).trans_time_state.at(j).end() )	{
							datei << "\nfrom " << j;
							for (MapIterator iter = (*automatas_ref).at(k).trans_time_state.at(j).begin(); iter != (*automatas_ref).at(k).trans_time_state.at(j).end(); iter++)	{
								datei << "\nto " << (iter->first).first <<" at " << (iter->first).second << "\nValues:";
								typedef vector<double>::const_iterator ListIterator;
								for (ListIterator list_iter = iter->second.begin(); list_iter != iter->second.end(); list_iter++)
								datei << setprecision(10) <<*list_iter << ",";
							}
						}
					}
					datei << "\n";
				}
			}
		}
		else {
			for(int k = 0; k < (*automatas_ref).size(); k++) {
				if ((*automatas_ref).at(k).num_sym > 0)  {
					datei << (*automatas_ref).at(k).name;
					for(int j = 0; j < (*automatas_ref).at(k).trans_time_state_trace.size(); j++) {
						if ( (*automatas_ref).at(k).trans_time_state_trace.at(j).begin() != (*automatas_ref).at(k).trans_time_state_trace.at(j).end() ) {
							datei << "\nfrom " << j;
							for (MapIterator iter = (*automatas_ref).at(k).trans_time_state_trace.at(j).begin(); iter != (*automatas_ref).at(k).trans_time_state_trace.at(j).end(); iter++)	{
								datei << "\nto " << (iter->first).first <<" at " << (iter->first).second << "\nValues:";
								typedef vector<double>::const_iterator ListIterator;
								for (ListIterator list_iter = iter->second.begin(); list_iter != iter->second.end(); list_iter++)
								datei << setprecision(10) <<*list_iter << ",";
							}
						}
					}
					datei << "\n";
				}
			}
		}
		
		datei.close();
	}


So the function itself is writing data either from trans_time_state or from trans_time_state_trace, depending on a "ref" containing in the filepath passed to the function. Indeed I now before calling the function which of this two should be proceed in the function. My code seems to be pretty awkward, as I have to repeat much of the code, performing the same task.

Isn't there an easier way, passing the information about weather trans_time_state, or trans_time_state_trace should be proceed in the function?
Could it be done with a pointer?

Thanks so far,
Regards, Matthew
Last edited on
You could make a function from lines 11-21 and call it with datei and appropriate (*automatas_ref).at(k).trans_time.

You could have an accessor object that has a referense to vector<Automata> and returns with member function a reference to appropriate trans_time of element k of the vector.
I've now transformed my function to a member function of the class. So I can
pass a reference to the appropriate trans_time...

I am quiet satisfied with this solution although I have to whizz through the whole vector
in the main.cpp, which makes it more bigger than it already is.
Topic archived. No new replies allowed.