Introduction to C++ STL

6,239 views

Published on

Introduction to C++ STL

Slides revision 2 for 2010 NTNU CSIE WInter Training

Published in: Technology, Education

Introduction to C++ STL

  1. 1. Introduction to C++ STL 2010 NTNU CSIE Winter Training S.C. Chen / Dannvix [at] gmail [dot] com 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  2. 2. Brief ‣ Object-Oriented Programming ‣ C++ Standard Template Library ‣ Generic Programming Implementation ‣ Software Engineering ‣ Template ‣ “Don’t remake wheels by yourself.” ‣ Easy to use, Easy to extend. 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  3. 3. Generic Programming ‣ Data Types ‣ Copy ‘n’ Paste? ‣ “I don’t want to reproduce similar snippets.” ‣ “How about keeping just one formal type of code?” ‣ C++ Template ‣ template <class T> class my_container 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  4. 4. STL Components ‣ Container Classes ‣ Storage your data ‣ stack, queue, vector, map ... ‣ Iterator Classes ‣ Algorithm Classes ‣ frequently used algorithms ‣ sort, find, binary search, next_permutation ... ‣ Utilities 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  5. 5. STL Containers ‣ Sequences ‣ vector, list, deque ... ‣ Associative Containers ‣ set, map ... ‣ Container Adaptors ‣ Based on deque by default ‣ stack, queue, priority queue ... 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  6. 6. vector ‣ random access ([] operator) like an array ‣ add / remove an element in constant time at the first or last of vector ‣ add / remove an element in linear time in the middle of vector ‣ automatic memory management ‣ needn’t specify quantity of elements 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  7. 7. vector (operations) #include <vector> int main() { vector<int> v; v.push_back(10); print(v); v.push_back(20); print(v); cout << v.size() << “ “ << v.capacity() << endl; v.pop_back(); print(v); cout << v.size() << “ “ << v.capacity() << endl; } /* Output: 10 10 20 2 2 10 1 2 */ 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  8. 8. list ‣ double-linked list ‣ predecessor, successor ‣ add / remove an element in linear time ‣ alternative choice, single-linked list ‣ slist 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  9. 9. list (operations) #include <list> int main() { list <int> la, lb; la.push_back(0), la.push_back(1), la.push_back(3); lb.push_back(4), lb.push_front(2); la.sort(); lb.sort(); la.merge(lb); print(la); } /* Output: 4 3 2 1 0 */ 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  10. 10. deque ‣ Double-Ended Queue ‣ usually pronounced [deck] instead of [de-cue] ‣ random access ‣ slower than vector ‣ add / remove an element in constant time at the first or last position of deque ‣ add / remove an element in amortized constant time in the middle of deque 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  11. 11. deque (cont.) ‣ dynamic segmented array implementation ‣ never copy of any element when adding / removing elements at the first or last position ‣ vector is more efficient than deque ‣ sort a deque ‣ copy a deque to vector, sort the vector, and then copy the vector back to the deque 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  12. 12. deque (operations) #include <deque> int main() { deque<int> dq; dq.push_back(3); dq.push_front(1); dq.insert(de.begin() + 1, 2); dq[2] = 0; } /* Output: 1 2 0 */ 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  13. 13. set ‣ Sorted Associative Container ‣ Compare function ‣ Unique ‣ There are no two same elements. ‣ add / remove a sorted range in linear time. 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  14. 14. set (operations) #include <set> struct ltstr { bool operator() (const char* s1, const char* s2) const { return (strcmp(s1, s2) < 0); } } int main() { const char* str[6] = {“isomer”, “ephemeral”, “prosaic”, “nugatory”, “artichoke”, “serif”}; set<const char*, ltstr> s(str, str + 6); print(s); } /* Output: artichoke ephemeral isomer nugatory prosaic serif */ 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  15. 15. map ‣ Sorted Associative Container ‣ Pair Associative Container ‣ Unique Associate Container ‣ Dictionary 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  16. 16. map (operations) #include <map> int main() { map<string, int> grade; grade[“Mark”] = 95; grade[“Edward”] = 87; grade[“Louise”] = 66; grade[“Allen”] = 76; print(grade); cout << grade[“Allen”] << end; } /* Output: Allen, 76 Edward, 87 Louise, 66 Mark, 95 76 */ 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  17. 17. stack ‣ last in first out, LIFO ‣ unable to access elements except the top of stack ‣ you cannot traversal a stack ‣ implementation based-on deque by default 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  18. 18. stack (operations) #include <stack> int main() { stack<int> s; s.push(8); s.push(5); s.push(6); cout << s.top() << endl; s.pop(); cout << s.top() << endl; } /* Output: 6 5 */ 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  19. 19. queue ‣ fast in first out, FIFO ‣ unable to access elements except the top of queue ‣ you cannot traversal a queue ‣ implementation based-on deque by default 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  20. 20. queue (operations) #include <queue> int main() { queue<int> q; q.push(8); q.push(5); q.push(6); cout << q.top() << endl; q.pop(); cout << q.top() << endl; } /* Output: 8 5 */ 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  21. 21. priority queue ‣ fast in first out, FIFO ‣ top is always the biggest in the queue ‣ unable to access elements except the top of queue ‣ you cannot traversal a queue ‣ implementation based-on deque by default 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  22. 22. priority queue (operations) #include <queue> int main() { priority_queue< int, vector, greater<int> > pq; int ary[6] = {1, 4, 2, 8, 5, 7}; for (int i = 0; i < 6; pq.push(ary[i++])); while (!pq.empty()) { cout << pq.top() << “ “; pq.pop(); } } /* Output: 1 2 4 5 7 8 */ 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  23. 23. STL Iterator ‣ Every STL container holds a nested iterator class ‣ STL iterator adaptors 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  24. 24. STL Iterator (operations) #include <vector> #include <iterator> #include <algorithm> int main() { vector<int> v; int ary[6] = {1, 4, 2, 8, 5, 7}; for (int i = 0; i < 6; v.push_back(ary[i++])); for (vector<int>::iterator it = v.begin(); it != v.end(); it++) cout << (*it) << “ “; cout << endl; copy (v.begin(), v.end(), ostream_iterator<int>(cout, “ “)); } /* Output: 1 4 2 8 5 7 1 4 2 8 5 7 */ 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  25. 25. STL Algorithms ‣ Linear Search ‣ find, find_if, find_first_of ... ‣ Subsequence Matching ‣ search, find_end ... ‣ Counting Elements ‣ count, count_if ‣ for_each ‣ Comparing Two Ranges ‣ equal, mismatch ... 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  26. 26. STL Algorithms (cont.) ‣ Copy Ranges ‣ copy, copy_backward ‣ Swapping Elements ‣ swap, swap_ranges ... ‣ Replacing Elements ‣ replace, replace_if, replace_copy ... ‣ Permuting Elements ‣ reverse, rotate, next_permutation ... 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  27. 27. STL Algorithms (cont.) ‣ Random Shuffling and Sampling ‣ random_shuffle ... ‣ Generalized Numeric Algorithms ‣ inner_product, adjacent_difference ... ‣ Sorting Ranges ‣ sort, stable_sort, is_sorted, nth_element ... ‣ Operation in Sorted Ranges ‣ binary_search, merge, equal_range ... 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  28. 28. Demonstration ‣ “Show me the power!” ‣ “I got the power” - Jim Carrey ‣ UVa Prob. 10222 - Decode the Mad man ‣ http://uva.onlinejudge.org/external/102/10222.html ‣ switch implementation ‣ map implementation ‣ string + find + iterator implementation 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  29. 29. UVa Prob. 10222 (switch) #include <stdio.h> char decode (char c) { switch (c) { case ‘3’: return ‘1’; case ‘e’: return ‘q’; ... /* many many lines */ case ‘/’: return ‘,’; default: return c; } } int main() { char c; while (c = getchar()) putchar(decode(c)); return 0; } 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  30. 30. UVa Prob. 10222 (map) #include <iostream> #include <map> using namespace std; void init(map<char, char> &m) { m[‘3’] = ‘1’, m[‘e’] = ‘q’ ... m[‘/’] = ‘,’; } int main() { map<char, char> dict; init(dict); char c; while (cin >> c) if (dict[c] != ‘ ‘) cout << dict[c]; else cout << c; return 0; } 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com
  31. 31. UVa Prob. 10222 (string + find + iterator) #include <iostream> #include <string> #include <algorithm> #include <iterator> using namespace std; string origin = "3edc4rfv5tgb6yhn7ujm8ik,9ol.0p;/-['=] "; string layout = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,9ol0p[ "; char decode (char c) { return c == 'n' ? c : layout.at(origin.find(c)); } int main() { cin >> noskipws; transform(istream_iterator<char>(cin), istream_iterator<char(), ostream_iterator<char>(cout), decode); return 0; } 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com 31
  32. 32. Reference ‣ Generic Programming and the STL ‣ 9780201309560 ‣ Matthew H. Austern ‣ Addision-Wesely Professional ‣ STL ‣ 9575667670 ‣ ‣ Gotop 2010 NTNU CSIE Winter Traning S.C. Chen / Dannvix [at] gmail [dot] com

×