cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
customized vector: o(1) deletion
customized vector: o(1) deletion
Oct 31, 2015 at 12:56pm UTC
icegood
(63)
Is it somewhere implemented customized vectior container with o(1) deletion operation and invalidation of only of last and end iterators?
Last edited on
Oct 31, 2015 at 12:56pm UTC
Oct 31, 2015 at 1:04pm UTC
icegood
(63)
std::swap( container.back(), *iterator );
container.pop_back()
is a solution
Oct 31, 2015 at 4:05pm UTC
Peter87
(11238)
In this situation I think it's better to
move
the object instead of using swap.
1
2
*iterator = std::move(container.back()); container.pop_back();
Oct 31, 2015 at 8:44pm UTC
cire
(8284)
In C++11, std::swap already takes advantage of move semantics.
resize
is probably more appropriate than
pop_back
, though. Do we really want to generate a copy if we're worried about performance?
Oct 31, 2015 at 9:48pm UTC
Peter87
(11238)
In C++11, std::swap already takes advantage of move semantics.
Yes, but swap might do more work because it will move a value to the soon-to-be-popped last element.
Oct 31, 2015 at 10:27pm UTC
helios
(17575)
Do we really want to generate a copy if we're worried about performance?
The STL pop*() functions are named somewhat wrong, since they don't return anything. pop_back() is equivalent to resize(size() - 1).
Oct 31, 2015 at 10:52pm UTC
cire
(8284)
I've been spending too much time with Python lately, I guess. ;)
Topic archived. No new replies allowed.