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.
klax33Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MafaldaConnect With a Mentor Commented:
I USE VC++6 SP5 WHAT IS YOUR ENV ?

The following code is my example with your initialization and it works fine !
Use the ANSI STL

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


//... your get from file stuff here
char max_index = (char)51 -'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;
}

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 right after
cout << matrix[0][0] << " ";
cout << matrix[1][0] << endl;
cout << matrix[1][0] << " ";
cout << matrix[1][1] << endl;



0
 
DarthNemesisCommented:
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.
0
 
klax33Author Commented:
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
MafaldaCommented:
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;
0
 
MafaldaCommented:
Do you initialize row and col to 1 ?
Are row and col defined as ints or what ?
0
 
klax33Author Commented:
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 http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20544910.html).  Thanks.
0
 
MafaldaCommented:
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;
0
 
klax33Author Commented:
I have it working now.  Thanks for your help Mafalda.
0
All Courses

From novice to tech pro — start learning today.