How do I access a vector from a different function?

I create a 2D vector in 'void initialLoader()' and need to access it from 'void emulator()'.

Im having trouble with:
if (transitions.at(i) == row[i][1] && currentState == row[i][0])



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
int initialLoader()
{
	vector < vector < string > > row;
	ifstream file("example.txt");
	string line;			// Current line being read in
	int rows = 0;
	
	while ( getline(file, line) && line != "<stop>" )
	{
		vector < string > column;
		string value;
		istringstream iss(line,istringstream::in);
		rows++;
		
		while (iss >> value)
		{	column.push_back(value);	}
		row.push_back(column);
	 }
	 
	 for (int i=0; i<rows; i++)
	 {
		 for (int j=0; j<3; j++)
		 {	cout << row[i][j] << "\t";	}
		 cout << endl;
	 }
	 cout << "# of rows: " << rows << endl;
	 return rows;
}

void emulator ()
{
	vector < string > alphabet;
	ifstream input("input.txt");
	string transitions;		// Current line being read in
	int counter = 0;
	char startState = 'A';
	char currentState = startState;

	//while (more input)
	int rows = initialLoader();
	
	while ( getline(input, transitions) && transitions != "<next>" )
	{
		alphabet.push_back(transitions);
		cout << transitions << endl;
		counter++;
		
		for (unsigned i = 0; i < transitions.length(); i++)
		{
			for (int j = 0; j < rows; j++)
			{
				if (transitions.at(i) == row[i][1] && currentState == row[i][0])
				{
					//currentState = row[i][2];
					cout << "zero" << endl; break;}
				else if (transitions.at(i) == '1')
				{ cout << "one" << endl; break;}
			}
		}
		cout << endl;
}
Make it a global variable or pass it as a parameter.
@chenqi07:
He would have to pass the vector as a parameter by reference if he has to do some changes on it.
Topic archived. No new replies allowed.