I’m in a situation where I cannot use a vector because I use &element[x] then add more items so the pointer is invalidated. The problem is that std::list does not seem to overload the operator [] nor provide an at() method. Therefore the only way I see I could simulate at() is by using an iterator. Is there however a better way to do this?
I’m in a situation where I cannot use a vector because I use &element[x]
Share
You should reconsider your design probably.
Trying to emulate
operator[]oratforstd::listwould lead to performance disaster: these operations would take O(N) and not O(1) time asstd::list::iteratoris Bidirectional iterator, not Random Access iterator. So if you now iterate through container and call[]oratfor each element, it would lead to O(N*N) instead of O(N).That’s why these operation are not provided by
std::list.