The C++ Standard Template Library
The C++ Standard Template Library
The C++ Standard Template Library
Douglas C. Schmidt
Professor Department of EECS
[email protected] Vanderbilt University
www.dre.vanderbilt.edu/∼schmidt/ (615) 343-8197
• What is STL?
• Generic Programming: Why Use STL?
• Overview of STL concepts & features
– e.g., helper class & function templates, containers, iterators,
generic algorithms, function objects, adaptors
• A Complete STL Example
• References for More Information on STL
Vanderbilt University 1
The C++ STL Douglas C. Schmidt
What is STL?
Vanderbilt University 2
The C++ STL Douglas C. Schmidt
Vanderbilt University 3
The C++ STL Douglas C. Schmidt
Vanderbilt University 4
The C++ STL Douglas C. Schmidt
• Containers
– Sequential: vector, deque, list
– Associative: set, multiset, map, multimap
– Adapters: stack, queue, priority queue
• Iterators
– Input, output, forward, bidirectional, & random access
– Each container declares a trait for the type of iterator it provides
• Generic Algorithms
– Mutating, non-mutating, sorting, & numeric
Vanderbilt University 5
The C++ STL Douglas C. Schmidt
Vanderbilt University 6
The C++ STL Douglas C. Schmidt
Vanderbilt University 7
The C++ STL Douglas C. Schmidt
Vanderbilt University 8
The C++ STL Douglas C. Schmidt
Vanderbilt University 9
The C++ STL Douglas C. Schmidt
Vanderbilt University 10
The C++ STL Douglas C. Schmidt
Vanderbilt University 11
The C++ STL Douglas C. Schmidt
Vanderbilt University 12
The C++ STL Douglas C. Schmidt
Vanderbilt University 13
The C++ STL Douglas C. Schmidt
int main() {
My_Map my_map;
for (std::string a_word;
std::cin >> a_word; ) my_map[a_word]++;
std::for_each (my_map.begin(),
my_map.end(), print ());
return 0;
}
Vanderbilt University 14
The C++ STL Douglas C. Schmidt
Vanderbilt University 15
The C++ STL Douglas C. Schmidt
int main() {
const int N = 10;
int a[N] = {4, 1, 1, 1, 1, 1, 0, 5, 1, 0};
int b[N] = {4, 4, 2, 4, 2, 4, 0, 1, 5, 5};
Vanderbilt University 16
The C++ STL Douglas C. Schmidt
Vanderbilt University 17
The C++ STL Douglas C. Schmidt
Vanderbilt University 18
The C++ STL Douglas C. Schmidt
Vanderbilt University 19
The C++ STL Douglas C. Schmidt
Vanderbilt University 20
The C++ STL Douglas C. Schmidt
Vanderbilt University 21
The C++ STL Douglas C. Schmidt
Vanderbilt University 22
The C++ STL Douglas C. Schmidt
Vanderbilt University 23
The C++ STL Douglas C. Schmidt
Vanderbilt University 24
The C++ STL Douglas C. Schmidt
Vanderbilt University 25
The C++ STL Douglas C. Schmidt
Vanderbilt University 26
The C++ STL Douglas C. Schmidt
Vanderbilt University 27
The C++ STL Douglas C. Schmidt
Vanderbilt University 28
The C++ STL Douglas C. Schmidt
Vanderbilt University 29
The C++ STL Douglas C. Schmidt
Vanderbilt University 30
The C++ STL Douglas C. Schmidt
std::vector<int>::iterator i = v.begin();
std::vector<int>::iterator j = i + 2; cout << *j << " ";
i += 3; std::cout << *i << " ";
j = i - 1; std::cout << *j << " ";
j -= 2;
std::cout << *j << " ";
std::cout << v[1] << endl;
(j < i) ? std::cout << "j < i" : std::cout << "not (j < i)";
std::cout << endl;
(j > i) ? std::cout << "j > i" : std::cout << "not (j > i)";
std::cout << endl;
i = j;
i <= j && j <= i ? std::cout << "i & j equal" :
std::cout << "i & j not equal"; std::cout << endl;
Vanderbilt University 31
The C++ STL Douglas C. Schmidt
Vanderbilt University 32
The C++ STL Douglas C. Schmidt
Vanderbilt University 33
The C++ STL Douglas C. Schmidt
Vanderbilt University 34
The C++ STL Douglas C. Schmidt
Vanderbilt University 35
The C++ STL Douglas C. Schmidt
std::vector<std::string>::iterator j =
std::find (projects.begin (), projects.end (), std::string ("Lab8"));
Vanderbilt University 36
The C++ STL Douglas C. Schmidt
Vanderbilt University 37
The C++ STL Douglas C. Schmidt
std::cout << "Element " << p - A << " is out of order: "
<< *p << " > " << *(p + 1) << "." << std::endl;
Vanderbilt University 38
The C++ STL Douglas C. Schmidt
Vanderbilt University 39
The C++ STL Douglas C. Schmidt
Vanderbilt University 40
The C++ STL Douglas C. Schmidt
Vanderbilt University 41
The C++ STL Douglas C. Schmidt
int main () {
int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
int *pbegin = myints, *pend = myints + sizeof myints / sizeof *myints;
std::cout << "original array contains:";
std::copy (pbegin, pend, std::ostream_iterator<int> (std::cout, " "));
int *nend = std::remove (pbegin, pend, 20);
std::cout << "\nrange contains:";
std::copy (pbegin, nend, std::ostream_iterator<int> (std::cout, " "));
std::cout << "\ncomplete array contains:";
std::copy (pbegin, pend, std::ostream_iterator<int> (std::cout, " "));
std::cout << std::endl;
return 0;
}
Vanderbilt University 42
The C++ STL Douglas C. Schmidt
int main () {
int myints[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int *pbegin = myints;
int *pend = myints + sizeof myints / sizeof *myints;
pend = std::remove_if (pbegin, pend, is_odd ());
std::cout << "range contains:";
std::copy (pbegin, pend, std::ostream_iterator<int> (std::cout, " "));
std::cout << std::endl;
return 0;
}
Vanderbilt University 43
The C++ STL Douglas C. Schmidt
Vanderbilt University 44
The C++ STL Douglas C. Schmidt
int main() {
std::vector<float> v (5, 1); // a vector of 5 floats all initialized to 1.0.
std::partial_sum (v.begin(), v.end(), v.begin());
Vanderbilt University 45
The C++ STL Douglas C. Schmidt
int main() {
int A[] = {1, 4, 2, 8, 5, 7};
const int N = sizeof(A) / sizeof(int);
Vanderbilt University 46
The C++ STL Douglas C. Schmidt
Vanderbilt University 47
The C++ STL Douglas C. Schmidt
return 0;
}
Vanderbilt University 48
The C++ STL Douglas C. Schmidt
STL Adaptors
Vanderbilt University 49
The C++ STL Douglas C. Schmidt
Vanderbilt University 50
The C++ STL Douglas C. Schmidt
Vanderbilt University 51
The C++ STL Douglas C. Schmidt
for (; !st.empty (); st.pop ()) { for (; !q.empty (); q.pop ()) {
cout << "\nPopping: "; std::cout << "\nPopping ";
cout << st.top(); std::cout << q.front ();
} }
return 0; return 0;
} }
Vanderbilt University 52
The C++ STL Douglas C. Schmidt
struct Place {
unsigned int dist; std::string dest;
Place (const std::string dt, size_t ds) : dist(ds), dest(dt) {}
bool operator< (const Place &right) const { return dist < right.dist; }
};
int main () {
std::priority_queue <Place> pque;
pque.push (Place ("Poway", 10));
pque.push (Place ("El Cajon", 20));
pque.push (Place ("La Jolla", 3));
for (; !pque.empty (); pque.pop ()) std::cout << pque.top() << std::endl;
return 0;
}
Vanderbilt University 53
The C++ STL Douglas C. Schmidt
Vanderbilt University 54
The C++ STL Douglas C. Schmidt
Vanderbilt University 55
The C++ STL Douglas C. Schmidt
• STL has predefined functor adaptors that will change their functors
so that they can:
– Perform function composition & binding
– Allow fewer created functors
• These functors allow one to combine, transform or manipulate
functors with each other, certain values or with special functions
• STL function adapters include
– Binders (bind1st() & bind2nd()) bind one of their arguments
– Negators (not1 & not2) adapt functors by negating arguments
– Member functions (ptr fun & mem fun) allow functors to be
class members
Vanderbilt University 56
The C++ STL Douglas C. Schmidt
Vanderbilt University 57
The C++ STL Douglas C. Schmidt
Vanderbilt University 58
The C++ STL Douglas C. Schmidt
Vanderbilt University 59
The C++ STL Douglas C. Schmidt
int main() {
std::string s = "spaces in text";
std::cout << s << std::endl;
std::string::iterator new_end =
std::remove_if (s.begin (), s.end (), std::bind2nd (std::equal_to<char>(), ’ ’));
// remove_if() just moves unwanted elements to the end and returns an iterator
// to the first unwanted element since it’.s a generic algorithm & doesn’t "know"
// whether the container be changed. s.erase() *does* know this, however..
s.erase (new_end, s.end ());
std::cout << s << std::endl;
return 0;
}
Vanderbilt University 60
The C++ STL Douglas C. Schmidt
Vanderbilt University 61
The C++ STL Douglas C. Schmidt
Vanderbilt University 62
The C++ STL Douglas C. Schmidt
int main() {
std::vector<int> v1;
v1.push_back (1); v1.push_back (2); v1.push_back (3); v1.push_back (4);
std::vector<int> v2;
std::remove_copy_if (v1.begin(), v1.end(), std::back_inserter (v2),
std::bind2nd (std::greater<int> (), 3));
std::copy (v2.begin(), v2.end (),
std::ostream_iterator<int> (std::cout, "\n"));
std::vector<int> v3;
std::remove_copy_if (v1.begin(), v1.end(), std::back_inserter (v3),
std::not1 (std::bind2nd (std::greater<int> (), 3)));
std::copy (v3.begin(), v3.end (),
std::ostream_iterator<int> (std::cout, "\n"));
return 0;
}
Vanderbilt University 63
The C++ STL Douglas C. Schmidt
void showval() {
std::cout << val_ << " ";
}
bool is_prime() {
for (int i = 2; i <= (val_ / 2); i++)
if ((val_ % i) == 0)
return false;
return true;
}
private:
int val_;
};
Vanderbilt University 64
The C++ STL Douglas C. Schmidt
return 0;
}
Vanderbilt University 65
The C++ STL Douglas C. Schmidt
int main () {
std::vector<char *> v;
v.push_back ("One"); v.push_back ("Two"); v.push_back ("Three"); v.push_back ("Four");
Vanderbilt University 66
The C++ STL Douglas C. Schmidt
Vanderbilt University 67
The C++ STL Douglas C. Schmidt
Vanderbilt University 68
The C++ STL Douglas C. Schmidt
Vanderbilt University 69
The C++ STL Douglas C. Schmidt
• Goals:
– Read in a list of course names, along with the corresponding
day(s) of the week & time(s) each course meets
∗ Days of the week are read in as characters M,T,W,R,F,S,U
∗ Times are read as unsigned decimal integers in 24 hour HHMM
format, with no leading zeroes (e.g., 11:59pm should be read in
as 2359, & midnight should be read in as 0)
– Sort the list according to day of the week & then time of day
– Detect any times of overlap between courses & print them out
– Print out an ordered schedule for the week
• STL provides most of the code for the above
Vanderbilt University 70
The C++ STL Douglas C. Schmidt
Vanderbilt University 71
The C++ STL Douglas C. Schmidt
Vanderbilt University 72
The C++ STL Douglas C. Schmidt
Vanderbilt University 73
The C++ STL Douglas C. Schmidt
Vanderbilt University 74
The C++ STL Douglas C. Schmidt
Vanderbilt University 75
The C++ STL Douglas C. Schmidt
private:
int argc_;
char **argv_, **base_argv_;
int increment_;
};
Vanderbilt University 76
The C++ STL Douglas C. Schmidt
Vanderbilt University 77
The C++ STL Douglas C. Schmidt
Vanderbilt University 78
The C++ STL Douglas C. Schmidt
Vanderbilt University 79