2 dim Vectors

Hey guys,

I'm trying to create a program that reads content from a file with the following format:

Title
Year Gross Studio
List of Actors/Actresses (separated by a "," [up to 5])

I understand that vectors have to be of one type, so I'll exclude Gross and consider the year a string.

I'm thinking a vector so that future contents could be added or removed;
I'm thinking 2 dim vector with 8 columns. So I want m rows and 8 columns.

How would I crate the vector and then how would I save data into it?

For example, how would I save my first title into Vector 1, row 0 column 1;
year row 0 column 2, studio row 0 column 3...
title2 row 1 column 1....etc....

Any suggestions?
I'll get you started on the 2D array using the STL vector. The following code shows the shell of what is required:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
	int rows, columns = 8;
	cout <<	"Enter number of records" << endl;
	cin >> rows;

	vector<vector<char *>> myAry1;
	myAry1.resize(rows);
	for(int i=0; i<rows; i++) {
		myAry1[i].resize(columns);
		for(int ii=0; ii<columns; ii++) {
			myAry1[i][ii] = "text goes here";
			cout << myAry1[i][ii] << " ";
		}
		cout << endl;
	}

	return 0;
}


Note where the text is entered into the array.

If you are familiar with the vector container, you may substitute the use of the integers "i" and "ii" to move through the array in favour of the iterators that are provided as member functions of the container vector.

As for reading the file, you may need to familiarise yourself with C++ streams.
So I have to set up an array within the vector?
I thought vectors were arrays themselves, except vectors are adjustable in size.
Unless I'm misinterpreting the code you helped me out with, you had the user define my rows and columns and that kind of defeats the purpose of having a vector, from what I understand.
I'd like for my program to add a new row as needed, or detected when reading the contents from my file, and not have to be declared by the user.

Any further suggestions?
The code I provided is intended to give you a feel for the technique of constructing a 2D array using the STL vector. Yes, vector will allow you to resize as you add more elements. But the point I was trying to get across is that you want to use the following declaration:

vector<vector<char *>> myAry1;

Whether you choose to resize, as I have, or instead use the vector member function "push_back" to add each record is up to you. For instance, to add a new record to the array, you could use:

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
#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
	int rows, columns = 8;

	//Declare necessary vector objects
	vector<vector<char *>> myAry1;
	vector<char *> myVect1(columns);

	//Fill record with some text
	for(int ii=0; ii<columns; ii++) {
		myVect1[ii] = "text";
	}

	//Add record to array
	myAry1.push_back(myVect1);

	//Display contents
	vector<vector<char *>>::iterator i;
	vector<char *>::iterator ii;
	for(i = myAry1.begin(); i < myAry1.end(); i++) {
		for(ii = i->begin(); ii < i->end(); ii++) {
			cout << *ii << " ";
		}
		cout << endl;
	}
		cout << endl;

	return 0;
}
Last edited on
Ah, I'm not familiar with iterators, but I understood most of what u just said. The comments helped. Thanks for the help :)
For the moment, just consider an iterator as a type of pointer.
Topic archived. No new replies allowed.