> And even then you should profile the code to see which data structure is faster. With today's hardware
> a vector may surprise you even when adding and removing elements anywhere in the vector.
Yes. Some simple benchmarks:
http://www.cplusplus.com/forum/beginner/206320/#msg976016
There may be compelling reasons for using
std::list<> even if its performance may not be the best:
1. Modifiers of
std::list<> do not invalidate iterators, references or pointers to the other elements.
2. Elements can be transferred from one
std::list<T,A> to another
std::list<T,A> (same T, compatible A) without actually moving or copying the elements, and without invalidating iterators, references or pointers to the transferred elements.
http://en.cppreference.com/w/cpp/container/list/splice
3.
std::list<> provides 'strong-exception-guarantees' for modifiers; if an exception is thrown during a standard-library operation on the list (say, the copy constructor of the value_type throws), the list rolls back to the state in which it was before the operation started. In contrast,
std::vector<> provides only the 'basic-exception-guarantee' for some modifiers; if an exception is thrown during these standard-library operation on the vector, the vector remains in a 'good state' and no resources are leaked.
http://stroustrup.com/3rd_safe.pdf