int
s, double
s, string
s,
etc. already. We could choose something more exotic, like
ofstream
s, even! For example, imagine a situation
where we'd like a program that works as follows:
// Get number of files, call it N
int N;
cout << "Enter number of files: ";
cin >> N;
// Create an array of N ofstreams
ofstream* FILE = new ofstream[N];
Now we have to consider the following problem:
ofstream
objects, but how do we tell
each ofstream
object what filename they should use?
ofstream
object as in:
ofstream OUT("temp.txt");
However, when we create arrays of objects using new
, we cannot
initialize the objects. So, there is no way to give a filename to each
object in the array while it's being created using new
.
open()
function later. For example, we
can create a file "temp.txt" as follows:
ofstream OUT;
OUT.open("temp.txt");
cout << "Please enter " << N << " filenames: ";
for(int j = 0; j < N; j++)
{
string s;
cin >> s;
FILE[j].open(s);
}
Having done this, we can write whatever we want to all the files,
like this:
for(int k = 0; k < N; k++)
FILE[k] << "Hello file number " << k << "!!!" << endl;
You can take a look at this complete program
that writes a sentence to a whole list of files.
But what type of object can I use to store the 6 homework grades that correspond to a given student?
Answer: An array of 6 int
s, of course!
Remember, we create an array of
6 objects of type T like this:
|
Each object in my array of students is itself an array of
int s, i.e. each object is an int* .
|
So, if the type of object that gets stored in the array is
int* , we create our array like this:
|
array[i]
is an object of type
int*
. So wherever you see
a[i]
just think of it as being a normal old
pointer:
int** a = new int*[6];
for(int i = 0; i < 6; i++)
a[i] = new int[6];
What gets created and how you index elements in it are depicted
in this diagram:
This is how you can create "multi-dimensional" arrays in C++. You just have arrays of arrays! Okay, now let's read the grade information and store it in our 2-dimensional array:
ifstream IN("grades.txt");
for(int s = 0; s < 6; s++)
for(int g = 0; g < 6; g++)
IN >> a[s][g];
With this, I can get the grade for student stu
on homework assignment hwa
as
array[stu][hwa]
. This
program is a simple one that reads the grade info and allows
the user to ask questions about grades.
This version makes nice use of functions,
and gives you an idea of how we pass multi-dimensional arrays
around for this sort of thing.
Here are three extensions
of the functionality of this program you might like to consider:
new
live on until the end of your program, or until deleted with the
delete []
command. With mutli-dimensional arrays,
you have to remember to delete each array you created. That
means, if we refer back to the grades problem from above, that all
of the arrays pointed to by the elements of the array
grade
must be deleted before we can delete the array
grade
itself.
for(int s = 0; s < 6; s++)
delete [] a[s];
delete [] a;
~/$ ./ex1 There are 24 people. How many teams would you like? (make it evenly divide n) 3 Team 0: Mike Dan Chris Joni Christy Seung-Geol Cathy Susan Team 1: Gavin Nate Paul Adina Jeff Carl Karen Eric Team 2: Phong Betty Madeline Marianne Don Shirley Tim Steve