Thread: resizable arrays that contain structs

  1. #1
    Registered User help_seed's Avatar
    Join Date
    Nov 2009
    Posts
    13

    resizable arrays that contain structs

    I am working on a "game". The character is a firefighter and he starts at the bottom of a building (first node in linked list). At every level there is any number of fires. Each fire has a particular time it started at, and after a fire has burnt for too long the room is destroyed. So I made a struct that contains the time the fire started (initializedTime) and then I plan to have a resizable array for each node. So it would go firefighter comes to a level (node). There are x fires (however elements in array). He puts out the fire that's been going the longest (data inside struct). He moves to the next level (next node). Process repeats.
    My question is how can I have a resizable array of structs? Do I use the new operator or realloc? The fire may start again on a level, so new rooms catch on fire.

    Feel free to tell me that I'm going about this all the wrong way, or that I need to clarify my question.

    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would use a std::vector. However, the "puts out the fire that's been going the longest" suggests that a std::queue or std::priority_queue might be more appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocatable arrays in CLASSes and STRUCTS
    By simone.marras in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2009, 10:50 AM
  2. Replies: 7
    Last Post: 06-04-2008, 10:39 PM
  3. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  4. arrays in structs
    By *DEAD* in forum C Programming
    Replies: 3
    Last Post: 11-25-2006, 07:35 AM
  5. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 07:38 AM