?
Solved

2D array (vector) syntax

Posted on 2003-03-10
8
Medium Priority
?
235 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:klax33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:DarthNemesis
ID: 8108066
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
 

Author Comment

by:klax33
ID: 8108279
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
 
LVL 6

Expert Comment

by:Mafalda
ID: 8108754
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Mafalda
ID: 8108761
Do you initialize row and col to 1 ?
Are row and col defined as ints or what ?
0
 

Author Comment

by:klax33
ID: 8111293
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
 
LVL 6

Accepted Solution

by:
Mafalda earned 200 total points
ID: 8113301
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
 
LVL 6

Expert Comment

by:Mafalda
ID: 8113312
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
 

Author Comment

by:klax33
ID: 8117057
I have it working now.  Thanks for your help Mafalda.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question