Topic: v.push_back(v[0]) okay for vector?


Author: Bill Dimm <billd@gim.net>
Date: 1997/01/25
Raw View
The standard library implementations that I have seen
handle insertions that cause the capacity of a
vector to increase by copying the elements into the new
memory, inserting the new element(s), then destroying
the elements in the old memory.  This allows things like:
  v.push_back(v[0]);
  v.insert(v.end(),v[0]);
without having problems with the reference returned by v[0] becoming
invalid before the copy takes place.

I don't see anything in the Dec96 draft that says this is required.
Did the ANSI committee intentionally leave this unspecified (if so,
why?), or was it an accidental omission?  Also, one of the responses
to my previous post asking why vector::resize uses pass by value
said it was so you could do: v.resize(100,v[0]) without having
problems with a stale reference.  If vector::insert were required
to have the behavior I described in the first paragraph above, there
would be no need for pass by value in vector::resize to avoid stale
references (since vector::resize is defined to behave
like vector::insert).



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]