Presentation is loading. Please wait.

Presentation is loading. Please wait.

STL List.

Similar presentations


Presentation on theme: "STL List."— Presentation transcript:

1 STL List

2 Sequence Containers Sequence (indexed) container: Store elements in defined order (0, 1, 2…) Three predefined sequence containers: vector deque list

3 List std::list Manages a linked list
Doubly linked – allows for backward navigation

4 List Container class proving dynamic array #include <list>
Declaration includes type: list<int> intList; list<string> stringList;

5 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

6 Fundamental Ops Add item to end: list.push_back(T)
OR list.push_front(T) Get size list.size()

7 No Random Access!!! No .at(i) or [i]

8 Iterators Iterator Pointer like object Know how to traverse collection
Standard way to access elements of any collection

9 Iterators List.begin() is iterator at first item
List.end() is one step past end

10 Iterator Movement ++ and – to advance

11 Iterator Access Use dereference operator to access: And assign:

12 Iterator Access Use -> to access members of what iterator points to:

13 Iterator Based Loop Start at begining

14 Iterator Based Loop Go up to end

15 Iterator Based Loop Step by one position

16 Iterator Based Loop Print the current item

17 Copies Assignment does deep copy of contents

18 Pass By Ref Pass by ref to avoid copy:

19 Comparison std::list Linked List std::vector Array List

20 Comparison List: Pro Faster at front and back
Can have advantage with large elements Can splice easily

21 Comparison Vector: Pro Random access is fast
Array generally performs better than pointers for similar tasks

22 Comparison Small items at end

23 Comparison Linear Search

24 Comparison Small items at front

25 Comparison Remove a large item from middle


Download ppt "STL List."

Similar presentations


Ads by Google