vector<*C> and vector<C>

This is a discussion on vector<*C> and vector<C> within the C++ Programming forums, part of the General Programming Boards category; Hello, I have writen a lot of functions working on containers of objects. Now I've decided it's better not to ...

  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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Katy, Texas
    Posts
    2,309
    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
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Are you talking about not wanting to change your code from object.variable to object->variable?

    Todd

  8. #8
    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

  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
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    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 11:30 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    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

  13. #13
    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

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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);

    ?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    I'm not very familiar with the boost library and not keen on mixing that in now.
    You could write your own pointer container classes.

    I was wondering if there is any more "standard" solution?
    If there was, Stroustrup would have mentioned it when answering the FAQ on Why are the standard containers so slow?

    Why won't this work:
    It would (assuming the vector does not outlive the scope of i), but code that uses vi[index] would need to be changed to *(vi[index]).
    Last edited by laserlight; 01-04-2008 at 11:45 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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