Thread: Keep track of elements inside STL vector

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    13

    Keep track of elements inside STL vector

    Hi
    I would like to know what is the best way (if there is one) to keep track of an element inside an STL vector.
    Consider I have some big object 3D models, that take up a lot of space each, alligned into an STL vector and each of those models may have one or more smaller elements that refer to this model, holding proprieties such as mass, position, orientation and so on.
    How do I make sure the smaller propriety elements always have a valid reference/pointer/index to a model in the models vector? Is iteration through the vector, looking for a specific marker always necessary?
    Thanks,
    Krones

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Your discription is a little unclear. Please elaborate so we can give more specific advice.

    But generally, you need a strict system of ownership. You figure out which class's lives depend on another class, and have that other class control its lifespan. You can use smart pointers where a simple Has-a relationship is not sufficient.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    Quote Originally Posted by King Mir View Post
    Your discription is a little unclear. Please elaborate so we can give more specific advice
    The following should help to clarify. it's not actual code, but it shows what I'm trying to figure out.

    Code:
    struct Entity{
    3DModel *model;
    }
    
    std::vector<3DModel> models;
    
    void load_model(Model &m){
    models.push_back(m);
    }
    void load_entity(Entity *e, const char *modelName){
    e->model = find_model(&models, modelName);
    }
    void draw_entity(Entity *e){
    draw_model(e->model);
    }
    
    void function_a(){
    3DModel mA;
    Entity a;
    
    load_model(mA);
    load_entity(&a, mA.name);
    draw_entity(&a);
    }
    
    void function_b(){
    3DModel mB1, mB2;
    Entity b;
    
    load_model(mB1);
    load_entity(&b, mB1.name);
    load_model(mB2); //makes b.model invalid if reallocation occurs
    draw_entity(&b);
    }
    I am looking for a way to keep something like b.model valid, even if I add more members to the models vector.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could just declare b to be of type Entity&, i.e., a reference. A pointer would work as well.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    Quote Originally Posted by dwks View Post
    You could just declare b to be of type Entity&, i.e., a reference. A pointer would work as well.

    Even if b was a pointer, is b->model still valid, even if I change the vector models? Because it points to a region in the memory that may have been moved when the push_back was called.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You probably shouldn't be storing 3DModel objects in the vector. If it is big, it is probably expensive to copy, and that's what vector will do with it.

    Instead, I'd hold pointers. Then, you'd never have to "find" the model if you already had a pointer to it, and updates to it in one place would affect all places automatically.

    There are several ways to store pointers in the vector. You could use boost's ptr_vector, or you could have a vector of shared_ptrs. If you don't like either of those solutions you could use raw pointers, although I would only do that if there is a clear place to create and destroy the models.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    so a std::vector<3DModel*> would work, and I just got adding pointers to it after I load the models? Then ofc, reminding to clean it all up after I'm done with them.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You can use a boost::shared_ptr if you don't want to worry about cleaning up. You'd have to use it in the entities too.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing
    By flame82 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 02:35 PM
  2. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  3. Pointer Elements & STL Containers :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 08:13 PM
  4. mistake in tutorial re: stl vector
    By ygfperson in forum C++ Programming
    Replies: 4
    Last Post: 04-20-2002, 11:50 AM
  5. stl vector class
    By ygfperson in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2002, 12:00 AM