STL Common tools for C++
STL Standard Template Library (STL) Templated code for common data structures, algorithms
Sequence Containers Sequence (indexed) container: every object in the container has a specific position Three predefined sequence containers: vector deque list
Vector Container class proving dynamic array Declaration includes type: #include <vector> … vector<int> intList; vector<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} 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
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
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
Big Differences Assignment does copy of contents of vector Will not deep copy pointers Make sure to pass by reference to avoid copy
Iterators Iterator Pointer like object Know how to traverse collection Standard way to access elements of any collection
Iterators Every collection also defines an iterator myIterator is an iterator over a vector of chars
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();
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
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
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
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
Iterator Movement Doing arithmetic with iterator moves it:
Iterator Access Use dereference operator to access: and -> 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 Preincrement/decrement more efficient than post
Iterator Based Loop Print the current item
Auto C++11 supports auto type: This is a bad use…
Auto C++11 supports auto type: Good use : shorten obvious but complex types
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
Iterators as Indexes Vector.insert(iterator, value) Uses iterator as location:
Iterators as Indexes vector.erase(iterator) Removes item at given location Advanced iterator to next item