J
Jakob Bieling
Hi,
I want to move an element from a std::list to the end of the same list.
To get this done, I thought I'd just do something like:
std::list <int> lst;
lst.push_back (0);
lst.push_back (1);
lst.push_back (2);
lst.splice (lst.end (), lst, lst.begin (), ++ lst.begin ());
This does change the list from [0, 1, 2] to [1, 2, 0]. The problem is,
that any iterators I have pointing to '0' are now invalid. Are there ways to
get this done without invalidating my iterators? Theoretically, it should be
possible (if I would reimplement a list container myself), shouldn't it?
Does the Standard provide methods to achieve this?
Now for some more background information: The application is a server
and the list holds all connected clients. Whenever some action has been
taken, the corrensponding element in the list is moved[*] to the end of the
list. This way, the first element will always be the client that has
responded (or has been served) _least_ recently. Along with the time, I can
easily timeout on any client. I just examine the first element in the
"client list", wait at most some time T (which I can calculate now) and if
nothing happened with this client during that time, I know it has timed out.
As it stands, splice would be perfect. But I also manage another list with
iterators into the "client list". That list is a "job list" and holds
iterators to clients, that have pending jobs (pending, because a request is
already being processed for that client and requests can only be satisfied
in the same order they arrive). And this is the problem. When moving a
client to the end of the "client list", the "job list" could then contain
invalid iterators.
One solution would be, to store the actual client structures somewhere
else (for example yet another list) and have both the "client list" and the
"job list" just contain iterators into that "store list". This would solve
it all, really, but I am not quite happy with this solution. It feels
somewhat clumsy. I'd appreciate any more thoughts on this!
[*] (I do not want to actually move any memory, but instead just
rearrange the next/prev node-links.. but I just realize that I am not even
sure whether splice may deep-copy or not ..)
Thanks for your help!
I want to move an element from a std::list to the end of the same list.
To get this done, I thought I'd just do something like:
std::list <int> lst;
lst.push_back (0);
lst.push_back (1);
lst.push_back (2);
lst.splice (lst.end (), lst, lst.begin (), ++ lst.begin ());
This does change the list from [0, 1, 2] to [1, 2, 0]. The problem is,
that any iterators I have pointing to '0' are now invalid. Are there ways to
get this done without invalidating my iterators? Theoretically, it should be
possible (if I would reimplement a list container myself), shouldn't it?
Does the Standard provide methods to achieve this?
Now for some more background information: The application is a server
and the list holds all connected clients. Whenever some action has been
taken, the corrensponding element in the list is moved[*] to the end of the
list. This way, the first element will always be the client that has
responded (or has been served) _least_ recently. Along with the time, I can
easily timeout on any client. I just examine the first element in the
"client list", wait at most some time T (which I can calculate now) and if
nothing happened with this client during that time, I know it has timed out.
As it stands, splice would be perfect. But I also manage another list with
iterators into the "client list". That list is a "job list" and holds
iterators to clients, that have pending jobs (pending, because a request is
already being processed for that client and requests can only be satisfied
in the same order they arrive). And this is the problem. When moving a
client to the end of the "client list", the "job list" could then contain
invalid iterators.
One solution would be, to store the actual client structures somewhere
else (for example yet another list) and have both the "client list" and the
"job list" just contain iterators into that "store list". This would solve
it all, really, but I am not quite happy with this solution. It feels
somewhat clumsy. I'd appreciate any more thoughts on this!
[*] (I do not want to actually move any memory, but instead just
rearrange the next/prev node-links.. but I just realize that I am not even
sure whether splice may deep-copy or not ..)
Thanks for your help!