?
Solved

2D vector usage

Posted on 2003-03-30
4
Medium Priority
?
184 Views
Last Modified: 2010-04-01

  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
Comment
Question by:Brillig
[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
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
GaryFx earned 200 total points
ID: 8236526
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
 

Author Comment

by:Brillig
ID: 8236630
 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
 
LVL 6

Expert Comment

by:GaryFx
ID: 8241234
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
 

Author Comment

by:Brillig
ID: 8241590
 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

765 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