STL List
Sequence Containers Sequence (indexed) container: Store elements in defined order (0, 1, 2…) Three predefined sequence containers: vector deque list
List std::list Manages a linked list Doubly linked – allows for backward navigation
List Container class proving dynamic array #include <list> Declaration includes type: list<int> intList; list<string> stringList;
Constructors Construction options: () : empty (size) : filled with size copies of 0 or default constructor (size, value) : filled with size copies of value {list} list<int> v; //empty list<int> v2(10); //10 default ints list<int> v3(10, 99); //10 copies of 99 list<int> v4 {1, 2, 3}; //3 defined elements
Fundamental Ops Add item to end: list.push_back(T) OR list.push_front(T) Get size list.size()
No Random Access!!! No .at(i) or [i]
Iterators Iterator Pointer like object Know how to traverse collection Standard way to access elements of any collection
Iterators List.begin() is iterator at first item List.end() is one step past end
Iterator Movement ++ and – to advance
Iterator Access Use dereference operator to access: And assign:
Iterator Access Use -> to access members of what iterator points to:
Iterator Based Loop Start at begining
Iterator Based Loop Go up to end
Iterator Based Loop Step by one position
Iterator Based Loop Print the current item
Copies Assignment does deep copy of contents
Pass By Ref Pass by ref to avoid copy:
Comparison std::list Linked List std::vector Array List
Comparison List: Pro Faster at front and back Can have advantage with large elements Can splice easily
Comparison Vector: Pro Random access is fast Array generally performs better than pointers for similar tasks
Comparison Small items at end https://baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html
Comparison Linear Search https://baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html
Comparison Small items at front https://baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html
Comparison Remove a large item from middle https://baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html