c++ - Move pointer to object from one list to another without destroying object -
consider code:
transferptr(node *moveme, list<node*> &lista, list<node*> &listb) { // how remove moveme a? listb.push_back(moveme); }
assume pointer moveme
(pointer node, or object) in lista
, we're given pointer, not iterator. we're moving moveme
out of lista
, listb
.
how remove moveme
lista
?
by using list splice function can avoid memory allocation list node. using remove() have search list anyway.
list<node*>::iterator = std::find(lista.begin(), lista.end(), moveme); if (i != lista.end()) { listb.splice(listb.end(), lista, i); }
if pointer can exist more 1 time in list you'll need use following, stl implementation may not efficiently you'd hope.
list<node*>::iterator = std::remove(lista.begin(), lista.end(), moveme); if (i != lista.end()) { listb.splice(listb.end(), lista, i, lista.end()); }
Comments
Post a Comment