1 Replies - 3440 Views - Last Post: 07 February 2009 - 07:55 PM Rate Topic: -----

#1 Euraty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-February 09

Resizing two dimensional vectors?

Post icon  Posted 07 February 2009 - 06:51 PM

Hi, so I am having some trouble with two dimensional vectors. I am new to this forum, so I don't know if I am supposed to post my code, so I will post it at the end. So, for my program, I am trying to input a txt file that looks like this....

.....xx........
....x..xx......
....x....xxxx..
...x........xxx
..x...*........
...x........xx.
...x..xxx...x.x
...x..x..x.x...
...xxxx...x....
...............

Into a two dimensional vector of chars. So what I was thinking was that I should use the getline function to input each line into a string. Then, I would input the characters of the string into the vector. Now, my problem is how I would make more rows, as this would only input the first row. I don't know if I can use the pushback function or the resize function, an explanation on how to edit each dimension of the vector would be greatly appreciated. Anyways, this was my best attempt at the program, but when I compile it, it doesn't display anything and returns some obscure number (also, I am using code::blocks if that is any help)...

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
ifstream infile("C:/Documents and Settings/Stefan Schembor/My Documents/Softmore Programming/picture.txt");
if (!infile)
{
cout<<"File could not be opened."<<endl;
exit(1);
}
vector<vector<char> >picture(1); //one by one vector
string line;
int rows=0;
while(getline(infile, line))
{
picture.resize(line.size()); //resize vector horizontally to fit the string
for(int x=0; x<picture.size(); x++)
picture[rows][x]=line[x]; //input string into array
for(int x=0; x<picture.size(); x++)
picture[x].resize(picture[x].size()+1); //resize vector vertically by one
rows++;
}

//display picture

for(int y=0; y<picture[y].size(); y++)
{
for(int x=0; x<picture.size(); x++)
cout<<picture[y][x];
cout<<endl;
}
return 0;
}

If I posted this in the wrong section, or did this wrong, please tell me so that I can do it right next time. Thanks for your help! :D

Is This A Good Question/Topic? 0
  • +

Replies To: Resizing two dimensional vectors?

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 4884
  • View blog
  • Posts: 11,280
  • Joined: 16-October 07

Re: Resizing two dimensional vectors?

Posted 07 February 2009 - 07:55 PM

Your add line code looks a little wonky. Your size seems sideways or something...

Make functions to control the logic of what you're doing.

Here's an example:
#include <iostream>
#include <vector>

using namespace std;

typedef vector<vector<char> > PictureType;

void show(PictureType &picture) {
	for(int y=0; y<picture.size(); y++) {
		for(int x=0; x<picture[y].size(); x++) {
			cout<<picture[y][x];
		}
		cout<<endl;
	}
}

void addLine(PictureType &picture, const string &line) {
	vector<char> newRow(line.size());
	for(int x=0; x<line.size(); x++) {
		newRow[x] = line[x];
	}
	picture.push_back(newRow);
}

int main() {
	PictureType picture;
	addLine(picture, ".....xx........");
	addLine(picture, "....x..xx......");
	addLine(picture, "....x....xxxx..");
	addLine(picture, "...x........xxx");
	addLine(picture, "..x...*........");
	show(picture);

	return 0;
}



Also, this kind of thing really lends to OOP. A class is even better:
class Picture {
private:
	vector<vector<char> > grid;

public:
	void setValue(int row, string line);
	void setValue(int row, int col, char ch);
	char getValue(int row, int col) const;
	void addRow(string line);
	int getRows() const;
	int getCols() const;
	void display() const;
}



Hope this helps.
Was This Post Helpful? 1

Page 1 of 1