Link to home
Start Free TrialLog in
Avatar of klax33
klax33

asked on

2D array (vector) syntax

I am trying to get the value from a 2D array of vectors, that is a vector< vector<int> > type structure.  I can assign values to the array locations using v[1][2] and v[x][y] just fine, but I cannot seem to get the value out of the location as easily as:

ofstreamObj << v[x][y];

When I step through it with the debugger, it shows the correct values of the array locations as it "passes over" the variables, but when I put v[0][0] in the Watch Box, it says "Error: Overloaded operation not found." It would make sense that if I can assign a value to a location using the notation above, I should be able to retrieve it doing the same.  Perhaps an expect can shed some light on my subject.  Thank you.
Avatar of DarthNemesis
DarthNemesis

Not sure what's going wrong with your code; I just tried a test program and everything worked fine:

#include <vector.h>
#include <fstream.h>
int main()
{
        // initialize 2D vector to 3 elements, each with
        // initial value vector<int>(3)
        vector< vector<int> > v(3,vector<int>(3));

        ofstream OutFile("test.txt");
        for (int j=0; j<3; j++) {
                for (int i=0; i<3; i++) {
                        v[i][j] = (i+1)*(j+1);
                        OutFile << v[i][j];
                }
                OutFile << endl;
        }
        OutFile.close();
        return 0;
}

If that program doesn't work for you, then your vector class must be outdated. It works on Borland C++ Builder 4.0.
Avatar of klax33

ASKER

Thank you for your comment.  Your code works correct when I tested it.  I am thinking my assignment and printing code is wrong.  

// matrix is global at the top
// row and col are declared to be 1 and 1

// set an index
matrix[row][col] = 1;

// this says 1 is at 1,1
cout << matrix[row][col] << " " << row << "," << col << endl;

// this shows the matrix is all 0's exectued right after
cout << matrix[0][0] << " ";
cout << matrix[1][0] << endl;
cout << matrix[1][0] << " ";
cout << matrix[1][1] << endl;

I am very confused as to why this is the case.  Perhaps someone can help me.  Thank you.
1)Better use the portable standart ANSI STL by changing to

#include <vector>
#include <fstream>
using namespace std;

2)

making changes in 1 and running your code shows the right result ! (I use VC++6 SP5 on Win 2000 Pro)

vector< vector<int> > matrix(3,vector<int>(3));
int col = 1;
int row = 1;
matrix[row][col] = 1;

// this says 1 is at 1,1
cout << matrix[row][col] << " " << row << "," << col << endl;

// this shows the matrix is 0,0,0,1
cout << matrix[0][0] << " ";
cout << matrix[1][0] << endl;
cout << matrix[1][0] << " ";
cout << matrix[1][1] << endl;
Do you initialize row and col to 1 ?
Are row and col defined as ints or what ?
Avatar of klax33

ASKER

My array declaration code is as follows:

cout << "Attempting to declare a 2D array of size " << largest_index << "... ";

// declare a 2d array
char max_index = (char)largest_index -'0';

 vector< vector<int> > matrix(max_index);

for (int h = 0; h < max_index; h++)
     matrix[h].reserve(max_index);  

// set all indices to 0
for (int i = 0; i < max_index; i++)
{
     for (int j = 0; j < max_index; j++)
          matrix[i][j] = 0;
}
cout << "done." << endl;

In my case, max_index, largest_index, row, and col are all chars because I'm reading them from a file (see previous post https://www.experts-exchange.com/questions/20544910/Converting-between-char-and-int-types.html).  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Mafalda
Mafalda

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW there is a bug ... instead of

cout << matrix[0][0] << " ";
cout << matrix[1][0] << endl;
cout << matrix[1][0] << " ";
cout << matrix[1][1] << endl;

it should be

cout << matrix[0][0] << " ";
cout << matrix[0][1] << endl; // <<<----------  FIX
cout << matrix[1][0] << " ";
cout << matrix[1][1] << endl;
Avatar of klax33

ASKER

I have it working now.  Thanks for your help Mafalda.