2-D Vectors and Output?

This is a discussion on 2-D Vectors and Output? within the C++ Programming forums, part of the General Programming Boards category; Hi there - first post here on CProgramming, and until now I've managed to find solutions to my coding problems ...

  1. #1
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70

    2-D Vectors and Output?

    Hi there - first post here on CProgramming, and until now I've managed to find solutions to my coding problems online or by thinking through it more. But here, I'm stumped:

    My goal is to make, essentially, a 2-d grid of spaces or slots or something. At the moment, they are all filled with an integer called "label" that increments by one before every time it's put into a slot, so (hypothetically) space 0,0 is 1, space 0, 1 is 2, space 0, 2 is 3...etc.

    Previously I tried using a 2d array for this but that failed pretty soundly since I wanted to define the length and width of it AFTER defining the array itself, and the compiler was not too agreeable on that.

    Anyway, here's the source:
    http://www.polarhome.com:823/~nazca/code/mygrid.cpp

    I realize I really -should- be using an iterator for this, but...I don't think this is my issue at the moment. If it is, please be kind enough to let me know so I can take a whack at that.

    Thank you!

    EDIT:
    Oh wow, I suck at teh C++. I found at least one super-obvious problem after some more frustrated debugging...This post can probably be ignored - I would delete it, but I don't see an option...

    EDIT2:
    Yep...it appears pretty much all of my problems have been solved: everything is running pretty much ok.
    An example of CardboardAnalyst?
    Last edited by Nazca; 04-26-2005 at 06:34 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    One thing I see is that in the constructor you shouldn't clear the row inside the second while loop, it should be outside the loop either before the loop or after the push_back.

    Those while loops are perfect candidates to be for loops.

    Now that you are using vectors, you don't need to keep track of the length, just use the length() function of the vector. This might have helped you diagnose the problem above.

  3. #3
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    That was my problem (the first thing you mentioned).

    The reason I made those otherwise-for loops into WHILE loops is that for some reason, previously, when I used for() the program refused to output information from base[x][y]. I didn't understand why this was, but switched to while() because, quite frankly, it worked.

    I'll try switching it back for simplicity's sake now.
    Thanks. ^^

    EDIT: Yes -- when I go back to for() loops, the program won't display the output in the for loop and crashes after entering the coords to display...presumably because the vector values weren't assigned inside the for() loop with x & y? Strange indeed.
    Last edited by Nazca; 04-26-2005 at 06:42 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    for (int x = 0; x < lenx; x++) should work.

  5. #5
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70

    Aye carumba...

    Silly mistakes always make me feel silliest. Thank you again - it works fine when I swap around the parameters, and of course the code is cleaner.

    I have yet another question that is somewhat related to the original topic, though - rather than clutter the board, I figured I'd just add it on here.

    Tile typical;
    row.push_back(typical);
    ~Tile typical;

    I've alread set up my vectors to accept Tile and it works just fine: the problem is that I can't figure out how to delete the Tile "typical" (the ~Tile you see there is about one of the 50 combinations I tried). I am creating and recreating a Tile to update a static counter so they all have unique id #s, to fill a motive...

    If you define something by
    Object instance,
    How do you delete it?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    It is automatically deleted when it goes out of scope. All variables created on the stack (Object instance;) are cleaned up automatically, you don't have to do anything.

    When you add it to the vector, a copy is made in the vector and the one you created is cleaned up when it goes out of scope. The copy in the vector is deleted when the vector goes out of scope, or when the entry is removed from the vector due to a call to erase or clear.

  7. #7
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    Oh...so the business of deleting it is completely unnecessary! That's quite dandy, actually.

    I (again) feel silly saying thank you (again), but it seems right.
    Thanks!

    Hopefully no more questions for a while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incrementing Vectors of type int
    By dnguyen1022 in forum C++ Programming
    Replies: 5
    Last Post: 01-12-2009, 10:00 AM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. C programing
    By flame82 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 02:35 PM
  4. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  5. extreme frustration with vectors
    By n3v in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2006, 08:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21