Creating a 2D Iterator for vector containing strings data type
Apr 23, 2015 at 6:10pm UTC
I have a 2D vector containing strings. I'd like to create a function that iterates through the array. I can't figure out how this function need to be prototyped.
My ideal function would take in the "vector, start state, and goal state" performing a recursive DFS search that outputs the path it took to get the goal state.
I just cant get past the creating the function definition.
Sample data in array
1 2 3 4 5 6 7 8
00 70 03 00 00 00 00 00 00
01 71 03 01 00 00 01 00 10
02 72 03 02 00 00 02 00 20
03 73 03 03 00 03 00 00 30
10 70 13 00 10 00 01 00 10
11 71 13 01 10 00 02 00 20
12 72 13 02 10 03 00 00 30
13 73 13 03 10 13 00 00 40
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using std::vector;
using namespace std;
string outPutFileName;
vector<vector<string> > array2D;
int rows = 32;
int columns = 9;
int main()
{
string x;
string line;
string filename;
ifstream infile;
infile.open("file.txt" );
//error check
if (infile.fail()) {
cerr << " The file you are trying to access cannot be found or opened" ;
exit(1);
}
array2D.resize(rows);
for (int i = 0; i < rows; ++i) {
array2D[i].resize(columns);
}
int row;
while (getline(infile, line)) {
istringstream streamA(line);
int col = 0;
while (streamA >> x) {
array2D[row][col] = x;
col++;
}
row++;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << array2D[i][j] << " " ;
}
cout << endl;
}
return 0;
}
Last edited on Apr 23, 2015 at 6:31pm UTC
Topic archived. No new replies allowed.