Thread: putting primitive non pointer data types on stl vector

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    putting primitive non pointer data types on stl vector

    i have a struct

    struct plane {
    long int x, y, z;
    };
    I have vector of planes
    std::vector<plane> PlaneVector;

    Do I do
    PlaneVector.resize(currentnumplanes)
    or
    PlaneVector.push_back(Plane()) (even though I provided no constructor)
    or something else? I don't see why there are different ways to add stuff to a vector

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    well doing push_back(Plane()) works just fine so I don't see the point in resize i guess msdn will tell me

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Resize allows you to insert many elements in one call; albeit they must be the same.

    PlaneVec.resize(32, plane(5,4,5));
    // Inserts 32 plane objects all constructed with x=5 y=4 z=5

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>well doing push_back(Plane()) works just fine so I don't see the point in resize
    push_back() and resize() do different things. resize() tells the vector to allocate enough space for N items so that multiple calls to push_back() are more efficient. push_back() allocates more space if needed, but doesn't if not needed, and then inserts a new item at the end.

    To recap, resize() doesn't create N items, it only sets aside space for those items, push_back() actually adds them. So unless you really need exact performance, don't even worry about resize(), just use push_back().
    *Cela*

  6. #6
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    To recap, resize() doesn't create N items, it only sets aside space for those items, push_back() actually adds them. So unless you really need exact performance, don't even worry about resize(), just use push_back().
    so if resize doesn't actually create N items, then that means push_back actually does create the items, which must mean it calls the constructor and resize doesn't, correct? kind of like new vs malloc

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >then that means push_back actually does create the items
    No, push_back() takes an already existing item and adds it to the vector.

    OBJ o;
    v.push_back(o);

    Or you can call the constructor directly and push_back() takes the newly created object

    v.push_back(OBJ());

    But push_back() doesn't call the constructor at all.
    *Cela*

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Cela
    >>well doing push_back(Plane()) works just fine so I don't see the point in resize
    push_back() and resize() do different things. resize() tells the vector to allocate enough space for N items so that multiple calls to push_back() are more efficient. push_back() allocates more space if needed, but doesn't if not needed, and then inserts a new item at the end.

    To recap, resize() doesn't create N items, it only sets aside space for those items, push_back() actually adds them. So unless you really need exact performance, don't even worry about resize(), just use push_back().
    I disagree... unless i'm missing something. Try this:
    Code:
    std::vector<int> intlist;
    intlist.resize(8, 5);
    for (std::vector<int>::iterator i = intlist.begin(); i < intlist.end(); ++i)
    	std::cout << *i;

  9. #9
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    well it outputs 5 8 times as i suspected it would, im just using push back because it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  5. mistake in tutorial re: stl vector
    By ygfperson in forum C++ Programming
    Replies: 4
    Last Post: 04-20-2002, 11:50 AM