Download presentation
Presentation is loading. Please wait.
1
STL Common tools for C++
2
STL Standard Template Library (STL)
Templated code for common data structures, algorithms
3
Sequence Containers Sequence (indexed) container: every object in the container has a specific position Three predefined sequence containers: vector deque list
4
Vector Container class proving dynamic array
Declaration includes type: #include <vector> … vector<int> intList; vector<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} vector<int> v; //empty vector<int> v2(10); //10 default ints vector<int> v3(10, 99); //10 copies of 99 vector<int> v4 {1,2,2}; //3 named elements
6
Fundamental Ops Add item to end: v.push_back(T) Get size v.size()
Access element v[index] – unsafe/no bounds check v.at(index) – blow up if out of bounds
7
Array Replacement Most of the time should use vector not array
Knows own size Much more capable Speed is the same When used correctly and compiler optimizes Unoptimized Optimized
8
Big Differences Assignment does copy of contents of vector
Will not deep copy pointers Make sure to pass by reference to avoid copy
9
Iterators Iterator Pointer like object Know how to traverse collection
Standard way to access elements of any collection
10
Iterators Every collection also defines an iterator
myIterator is an iterator over a vector of chars
11
Iterator Position Asking container for:
.begin() : iterator pointing to first element .end() : iterator pointing just after last element //get location of first element of container vector<char>::iterator myIt = container.begin(); //get location just past last element vector<char>::iterator endIt = container.end();
12
Reminder - Pointer Arithmetic
Adding 1 to pointer moves it one element: Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 0x110 0x109 0x108 0x107 x 5 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100
13
Reminder - Pointer Arithmetic
Adding 1 to pointer moves it one element: Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 0x110 0x109 0x108 0x107 x 5 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100
14
Reminder - Pointer Arithmetic
Adding 1 to pointer moves it one element: Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 x 2.5 0x110 0x109 0x108 0x107 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100
15
Reminder - Pointer Arithmetic
Adding 1 to pointer moves it one element: Size of element determines size of move Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 x 2.5 0x110 0x109 0x108 0x107 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100
16
Iterator Movement Doing arithmetic with iterator moves it:
17
Iterator Access Use dereference operator to access:
and -> to access members of what iterator points to:
18
Iterator Based Loop Start at begining
19
Iterator Based Loop Go up to end
20
Iterator Based Loop Step by one position
Preincrement/decrement more efficient than post
21
Iterator Based Loop Print the current item
22
Auto C++11 supports auto type: This is a bad use…
23
Auto C++11 supports auto type:
Good use : shorten obvious but complex types
24
Going Backwards Can start at (end() – 1) and -- to begin() OR use
Reverse Iterator Start at rbegin() End at rend() Count FORWARD to go back
25
Iterators as Indexes Vector.insert(iterator, value)
Uses iterator as location:
26
Iterators as Indexes vector.erase(iterator)
Removes item at given location Advanced iterator to next item
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.