multi-dimensional vectors

I have a little text file and I can read it into an array with the code below. I'd like to read them into a multi-dimensional vector (or vector of vectors if you will). Not many tutorials on multidimensional vectors. The don't understand the pushback, clear, popback commands. I've tried to do them to no avail.
This is NOT homework, just trying to teach myself the language.
My questions are: How can I read the elements into a multi dimensional-vector? and if you decide to write any code, could you please comment the vector commands so I can see how they are working?

Thank you very much, I appreciate it.

1
2
3
4
5
6
7
8
9
10
11
void readmap(){
   int gamemap[12][10];
   int rms;
   ifstream thefile ("room.dat");
   for (int y=0; y<12; y++){
         for (int x=0;x<10;x++){thefile >> rms;
         gamemap[y] [x]= rms;
         }
    }
    thefile.close();
}


and the text (room.dat) file is:

1
2
3
4
5
6
7
8
9
10
11
12
13
1 2 -1 -1 6 -1 4 3 -1 -1      
10 -1 -1 -1 0 -1 -1 -1 -1 12    
-1 -1 -1 -1 -1 0 -1 -1 11 -1   
-1 -1 -1 0 -1 -1 -1 -1 -1 -1     
-1 -1 0 -1 -1 -1 -1 -1 -1 -1      
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1      
0 -1 8 -1 9 -1 7 -1 -1 -1         
-1 -1 6 -1 -1 -1 -1 -1 -1 -1      
-1 -1 -1 -1 -1 -1 6 -1 -1 -1      
6 -1 -1 -1 -1 -1 -1 -1 -1 -1      
-1 -1 -1 -1 1 -1 -1 -1 -1 -1      
-1 -1 -1 -1 -1 -1 -1 -1 -1 2      
-1 -1 -1 -1 -1 -1 -1 -1 1 -1      
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

int main(){
	ifstream inp("room.dat");
	
	vector<vector<int>> vect;
	string s;	
	
	while(getline(inp,s)){ //get 1 line each time
		
		int i;
		vector<int> v;
		stringstream ss;
		ss << s;
		
		while(ss>>i){
			v.push_back(i); // v contains 1 line of ints
		}
		vect.push_back(v); // vect contains 2D ints
		
	}
	
	for(auto x: vect){  // print in screen
		for(auto y: x){
			cout << y << " ";
		}
		cout << "\n";
	}
	
return 0;
}
Thank you anup30, I was expecting a few for/next loops, but this works just fine, AND, you've given me something else to study on, that "auto" in your print loop.
another method, without stringstream
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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main(){	

	vector<int> v;
	vector<vector<int>> vect;	
	
	ifstream in("in.txt");
	char c;	
	
	while(1){
		
		in>> std::noskipws >> c; //noskipws : to input whispaces.
	
		if(c == '\n'){
			vect.push_back(v); 
			v.clear();
			continue;
		} 
		else{
			in.unget();		
			int i;			
			in>> std::skipws >> i;
			v.push_back(i);			
		}
		
		if(in.eof()){
			vect.push_back(v);
			break;
		}
		
	}

	
	for(int i=0; i<vect.size(); i++){
		for(int j=0; j<vect[i].size(); j++){
			cout << vect[i][j] << " ";
		}
		cout << "\n";
	}

	
return 0;
}


if in.txt is this:
12 13 6 -8


0
3 3
2 1 0 6
7 5


notice that, lines are of different size.
then the size of vect[0] is 4.
vect[1] and vect[2] are zero sized.
and vect[3]'s size is 1.
Topic archived. No new replies allowed.