Thread: vector<*C> and vector<C>

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    8

    vector<*C> and vector<C>

    Hello,

    I have writen a lot of functions working on containers of objects. Now I've decided it's better not to copy the objects in the containers, but to use conteiners of pointers.

    Is there some solution in which I do not have to wewrite all my functions?

    Thanks,
    Angy

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    vector<*C> and vector<C>
    I think you mean vector<C*> instead of vector<*C>.

    Now I've decided it's better not to copy the objects in the containers, but to use conteiners of pointers.
    On what basis did you make your decision?

    Is there some solution in which I do not have to wewrite all my functions?
    I do not think so. Once you use a vector of pointers you have to think about memory management. Things like boost::ptr_vector may be able to help, but there will still be quite some re-writing to do, methinks.
    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

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    vector<some_object *> my_pointers_to_several_objects ;

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Perhaps you could try a vector of reference but I'm not even sure it exists (never done it or seen it). It's worth a try. I don't think this would work because of the container's processing of the elements but try and see if it works =)

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Of course I meant vector<C*> it was a typing mistake, sorry.

    Vectors of references do not exist. The reason is that a reference is just an alternative name for an object, so that makes no sense.

    Any other Ideas?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I have writen a lot of functions working on containers of objects. Now I've decided it's better not to copy the objects in the containers, but to use conteiners of pointers.
    None of the objects in the containers need to be copied if you pass the vector to the functions as a reference.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Quote Originally Posted by anon View Post
    None of the objects in the containers need to be copied if you pass the vector to the functions as a reference.
    But objects are copied on insertion into containers

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Are you talking about not wanting to change your code from object.variable to object->variable?

    Todd

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Quote Originally Posted by Todd Burch View Post
    Are you talking about not wanting to change your code from object.variable to object->variable?

    Todd
    you can put it that way

    I do not want to go to every function and change

    my_vector_of_obgects[i]

    to

    *(my_vector_of_pointers_to_obgects[i])

    whenever a single object from a container is passed to another function

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Angelina View Post
    you can put it that way

    I do not want to go to every function and change

    my_vector_of_obgects[i]

    to

    *(my_vector_of_pointers_to_obgects[i])

    whenever a single object from a container is passed to another function
    From what I see boost::ptr_vector is designed to allow that kind of syntax (i.e., you do not need to change the code to use dereference pointers).

    I suspect if you do as ANON suggested, and change your functions to pass as a reference, that would satisfy your objective.
    If Angelina is indeed working with large objects for which copying is expensive, the overhead of having to copy on insertion may be too much. Of course, this assumes that some profiling was done, which is why I asked for the basis for the decision to change to use containers of pointers.
    Last edited by laserlight; 01-04-2008 at 12:30 PM.
    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

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Quote Originally Posted by laserlight View Post
    From what I see boost::ptr_vector is designed to allow that kind of syntax (i.e., you do not need to change the code to use dereference pointers).


    If Angelina is indeed working with large objects for which copying is expensive, the overhead of having to copy on insertion may be too much. Of course, this assumes that some profiling was done, which is why I asked for the basis for the decision to change to use containers of pointers.
    I'm not very familiar with the boost library and not keen on mixing that in now.

    I was wondering if there is any more "standard" solution?

    Yes, my objects are too big to be OK to copy

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Angelina View Post
    I'm not very familiar with the boost library and not keen on mixing that in now.

    I was wondering if there is any more "standard" solution?
    Boost. People who won't use it are masochists.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    I suspect if you do as ANON suggested, and change your functions to pass as a reference, that would satisfy your objective.

    I did what you don't want to do a couple weeks ago - it was a pain. The DOT notation is easier to read. I may try ANON's suggestion as well.

    Todd

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Quote Originally Posted by Todd Burch View Post
    I suspect if you do as ANON suggested, and change your functions to pass as a reference, that would satisfy your objective.

    I did what you don't want to do a couple weeks ago - it was a pain. The DOT notation is easier to read. I may try ANON's suggestion as well.

    Todd
    I think there is a missunderstanding here:

    The problem is not that I am not passing the container as reference to functions, but that I do not want to copy on insertion and thus use containers of pointers now.

    E.g.

    vector<int> vi;
    int i;
    vi.push_back(i);
    // here i is copied into vi
    // &vi[0] is not the address of i

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    OK, I understand. (I'm not a C++ expert, if you haven't figured that out yet).

    Why won't this work:

    vector<int *> vi;
    int i;
    vi.push_back(&i);

    ?

Popular pages Recent additions subscribe to a feed