• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

2D vector usage


  I'm having a problem with some multidimensional vectors.  The code looks something like this:

protected:
  vector< vector<int> > m_FooArray;


  m_FooArray.clear();

  for( int i = 0; i++; i < MAX1 )
  {
    m_FooArray[i].assign( MAX2, 0 );  //  This works
  }

  m_FooArray[2][2] = 4;  //  This compiles fine, but bombs on execution

  m_FooArray is a member of a base class and used in the constructor of the derived classes, in case that matters.  Bacically I want to know what's wrong with the line that bombs and if there's an equivalent notation that I can use.  I have a large amount of data that needs to be hard coded, so the simpler the notation the better.  (Reading from files is undesirable in this case, though I may end up doing it anyway.)

  Thanks in advance.
0
Brillig
Asked:
Brillig
  • 2
  • 2
1 Solution
 
GaryFxCommented:
Your for loop has the clauses in the wrong order.  Thus i++ always returns 0, which means the loop never actually executes.  It should be for (int i=0; i < MAX1; i++)

Once you fix that, you still have a problem, though it may not show up.  The first time through the loop you execute

  m_FooArray[0].assign(MAX2,0);

The fetch of m_FooArray[0] (which returns a vector<int>, in theory) should fail in principle, because there is no such element yet.  Fetching via square brackets doesn't do range checking (unlike at), so this may not trigger any visible errors.  The simplest thing to do is to resize m_FooArray to MAX1 before enteringthe loop (or possibly remove the clear and construct m_FooArray with the right size).

Gary

0
 
BrilligAuthor Commented:
 Thanks for the help.  I can't believe I had the for loop clauses swapped.  Based on a comment I saw in another thread I'm probably going to pick up a copy of Josuttis's STL book tomorrow.  But until then, do you have a recommendation on how to construct m_FooArray with the right size if it's a member variable?  Is there a way I can do it in the header or do I need to use reserve() in the constructor?

0
 
GaryFxCommented:
You should be able to do:

vector< vector<int> > m_FooArray(MAX1);

assuming MAX1 is defined at that point.

If you did it later, you'd have to use resize(), not reserve().  The difference is that reserve() doesn't actually construct the entries, nor does it change the size() of the array.  All it does is allocate raw storage, so that you're not wasting a lot of time allocate one element at a time.  resize(), on the other hand, will change the size() of the array, and when it increases the size of the array, it will call the default constuctor on the new elements.

But the declaration above ought to work more easily.

Gary
0
 
BrilligAuthor Commented:
 Thanks.  That should fix my (current) problem.  I just got back from buying Josuttis' STL book.  Looks like there's a lot of STL stuff I need to brush up on.  Thanks again for the help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now