14-c - STL PDF
14-c - STL PDF
Teaching Assistants:
Renshu Gu William Kim Soumya Vasisht
L14: C++ STL CSE333, Summer 2018
2
L14: C++ STL CSE333, Summer 2018
STL Containers J
v A container is an object that stores (in memory) a
collection of other objects (elements)
§ Implemented as class templates, so hugely flexible
§ More info in C++ Primer §9.2, 11.2
3
L14: C++ STL CSE333, Summer 2018
STL Containers L
v STL containers store by value, not by reference
§ When you insert an object, the container makes a copy
§ If the container needs to rearrange objects, it makes copies
• e.g. if you sort a vector, it will make many, many copies
• e.g. if you insert into a map, that may trigger several copies
§ What if you don’t want this (disabled copy constructor or copying
is expensive)?
• You can insert a wrapper object with a pointer to the object
– We’ll learn about these “smart pointers” soon
4
L14: C++ STL CSE333, Summer 2018
STL vector
v A generic, dynamically resizable array
§ http://www.cplusplus.com/reference/stl/vector/vector/
§ Elements are store in contiguous memory locations
• Elements can be accessed using pointer arithmetic if you’d like to
• Random access is O(1) time
§ Adding/removing from the end is cheap (amortized constant
time)
§ Inserting/deleting from the middle or start is expensive (linear
time)
6
L14: C++ STL CSE333, Summer 2018
vector/Tracer Example
vectorfun.cc
#include <iostream>
#include <vector>
#include "Tracer.h"
return 0;
}
7
L14: C++ STL CSE333, Summer 2018
STL iterator
v Each container class has an associated iterator class (e.g.
vector<int>::iterator) used to iterate through
elements of the container
§ http://www.cplusplus.com/reference/std/iterator/
§ Iterator range is from begin up to end
• end is one past the last container element!
§ Some container iterators support more operations than others
• All can be incremented (++), copied, copy-constructed
• Some can be dereferenced on RHS (e.g. x = *it;)
• Some can be dereferenced on LHS (e.g. *it = x;)
• Some can be decremented (--)
• Some support random access ([], +, -, +=, -=, <, > operators)
9
L14: C++ STL CSE333, Summer 2018
iterator Example
vectoriterator.cc
#include <vector>
#include "Tracer.h"
vec.push_back(a);
vec.push_back(b);
vec.push_back(c);
void foo(void) {
// Manually identified type
std::vector<int> facts1 =
Factors(324234);
// Inferred type
auto facts2 = Factors(12321);
12
L14: C++ STL CSE333, Summer 2018
#include "Tracer.h"
vec.push_back(a);
vec.push_back(b);
vec.push_back(c);
STL Algorithms
v A set of functions to be used on ranges of elements
§ Range: any sequence that can be accessed through iterators or
pointers, like arrays or some of the containers
§ General form: algorithm(begin, end, ...);
Algorithms Example
vectoralgos.cc
#include <vector>
#include <algorithm>
#include "Tracer.h"
using namespace std;
vec.push_back(c);
vec.push_back(a);
vec.push_back(b);
cout << "sort:" << endl;
sort(vec.begin(), vec.end());
cout << "done sort!" << endl;
for_each(vec.begin(), vec.end(), &PrintOut);
return 0;
}
16
L14: C++ STL CSE333, Summer 2018
STL list
v A generic doubly-linked list
§ http://www.cplusplus.com/reference/stl/list/
§ Elements are not stored in contiguous memory locations
• Does not support random access (e.g. cannot do list[5])
§ Some operations are much more efficient than vectors
• Constant time insertion, deletion anywhere in list
• Can iterate forward or backwards
§ Has a built-in sort member function
• Doesn’t copy! Manipulates list structure instead of element values
19
L14: C++ STL CSE333, Summer 2018
list Example
listexample.cc
#include <list>
#include <algorithm>
#include "Tracer.h"
using namespace std;
lst.push_back(c);
lst.push_back(a);
lst.push_back(b);
cout << "sort:" << endl;
lst.sort();
cout << "done sort!" << endl;
for_each(lst.begin(), lst.end(), &PrintOut);
return 0;
}
20
L14: C++ STL CSE333, Summer 2018
STL map
v One of C++’s associative containers: a key/value table,
implemented as a tree
§ http://www.cplusplus.com/reference/stl/map/
§ General form: map<key_type, value_type> name;
§ Keys must be unique
• multimap allows duplicate keys
§ Efficient lookup (O(log n)) and insertion (O(log n))
• Access value via name[key]
§ Elements are type pair<key_type, value_type> and are
stored in sorted order (key is field first, value is field second)
• Key type must support less-than operator (<)
21
L14: C++ STL CSE333, Summer 2018
map Example
mapexample.cc
void PrintOut(const pair<Tracer,Tracer>& p) {
cout << "printout: [" << p.first << "," << p.second << "]" << endl;
}
table.insert(pair<Tracer,Tracer>(a, b));
table[c] = d;
table[e] = f;
cout << "table[e]:" << table[e] << endl;
it = table.find(c);
return 0;
}
22
L14: C++ STL CSE333, Summer 2018
23
L14: C++ STL CSE333, Summer 2018
Extra Exercise #1
v Using the Tracer.h/.cc files from lecture:
§ Construct a vector of lists of Tracers
• i.e. a vector container with each element being a list of
Tracers
§ Observe how many copies happen J
• Use the sort algorithm to sort the vector
• Use the list.sort() function to sort each list
24
L14: C++ STL CSE333, Summer 2018
Extra Exercise #2
v Take one of the books from HW2’s test_tree and:
§ Read in the book, split it into words (you can use your hw2)
§ For each word, insert the word into an STL map
• The key is the word, the value is an integer
• The value should keep track of how many times you’ve seen the word,
so each time you encounter the word, increment its map element
• Thus, build a histogram of word count
§ Print out the histogram in order, sorted by word count
§ Bonus: Plot the histogram on a log-log scale (use Excel, gnuplot,
etc.)
• x-axis: log(word number), y-axis: log(word count)
25