Introduction to Standard Template Library STL Wenguang Wang
Introduction to Standard Template Library (STL) Wenguang Wang and Yanping Zhao March 20, 2000 1
What Is STL? • A new C++ Library, ANSI standard • Support complex data structures – Vector (array), list, string, hash, set, map, queue, stack, priority queue. . . • Support various algorithms – Searching, sorting, heap, merging, copying, transforming… 2
Why STL was developed? • Some basic data structures are used everyday in your programs • Programming them from the scratch is time consuming and error-prone • Most of operations on these structures can be standardized 3
Why we use STL? • • More reliable and efficient implementations Automatic memory management Constructing complex data structures easily Saving programming time and efforts 4
Where are STL and resources? • GNU g++ on skorpio, ultra*… • Visual C++ • Wenguang Wang’s home page – http: //www. cs. usask. ca/grads/wew 036/stl • Standard Template Library Programmer’s Guide – http: //www. sgi. com/Technology/STL/ 5
Fundamental Elements in STL? • Containers (data structures) – vector dynamic size – list doubly linked list – string, hash • Algorithms – sorting, heap manipulation • Iterators (pointers) – A bridge to connect algorithms and containers – Example 6
How to Use STL? (example) • On Unix – GNU g++ 2. 7. 0 and later version • On Windows – Visual C++ 5. 0 and later version – not fully supported even in Visual C++ 6. 0 • no hash table • no rope • more? • Example 7
Why I am here? • • I learned STL two weeks ago I never heard of STL before After a five minutes STL tour, I can use it ! I found that STL helped me a lot in programming • I want to share it with you 8
Vector -- Advantages • Data type – basic type: integer, double, pointer, … – user-defined structures and classes • Dynamic size 9
int *array = malloc(size*sizeof(int)); //allocate int *temp = realloc(array, new_size*sizeof(int)); if (temp) array = temp; //reallocate How to get the size of the array? free(array); // deallocate Count Estimate Count = 0; while(not end) count++; #define SIZE vector<int> vec; vec. push_back(i); 2000 10
Vector -- Operations • Property operations – [ ] , size, empty • Iterator operations size=8 0 1 2 3 4 5 6 7 begin v[3] end – begin, end • Manipulation operations – push_back, pop_back, insert, erase 11
#include <vector> #include <iostream> typedef vector<int> Vec. Int; void main(){ Vec. Int v. Int; Vec. Int: : iterator it; v. Int. push_back(0); v. Int. push_back(1); for (it=v. Int. begin(); it!=v. Int. end(); it++) cout << *it << endl; for (int i=0; i<v. Int. size(); i++) cout << v. Int[i] << endl; it = v. Int. insert(v. Int. begin(), 2); v. Int. erase(it); } 12
List -- Operations • Property operations – size, empty – back, front • Iterator operations – begin, end • Manipulation operations – push_back, pop_back, push_front, pop_front – insert, erase 13
#include <list> #include <iostream> typedef list<int> List. Int; void main(){ List. Int l. Int; List. Int: : iterator it; // stack l. Int. push_front(0); l. Int. push_front(1); cout << l. Int. front() << endl; l. Int. pop_front(); // queue l. Int. push_back(2); l. Int. push_back(3); cout << l. Int. front() << endl; l. Int. pop_front(); // iterator for (it=l. Int. begin(); it!=l. Int. end(); it++) cout << *it << endl; } 14
Algorithm -- Heap • Data structure: vector or C++ array • make_heap 4 1 7 5 3 6 8 9 9 5 8 4 3 6 7 1 • pop_heap 8 5 7 4 3 6 1 9 • push_heap 9 8 7 5 3 6 1 4 15
#include <vector> #include <algorithm> #include <iostream> typedef vector<int> Vec. Int; void main(){ Vec. Int v. Int(4); int i; v. Int[0]=7; v. Int[1]=4; v. Int[2]=9; v. Int[3]=1; make_heap(v. Int. begin(), v. Int. end()); v. Int. push_back(3); push_heap(v. Int. begin(), v. Int. end()); pop_heap(v. Int. begin(), v. Int. end()); cout << v. Int. back() << endl; v. Int. pop_back(); } 16
User-defined Data Type struct Event { long timestamp; int type; }; typedef Event* PEvent; typedef vector<Event> Vec. Event; typedef list<PEvent> List. PEvent; 17
User-defined Data Type (cont. ) page class Page { page int page_num; int modify_flag; page int reference_cnt; }; typedef list<Page> List. Page; typedef List. Page* PList. Page; typedef vector<PList. Page> Vec. PList. Page; What is the structure of Vec. PList. Page? 18
Iterator • Similar to pointer – *it • Iterator for list (bidirectional iterator) – ++it, it++, --it, it-- • Iterator for vector (random access iterator) – ++it, it++, --it, it-– it+n, it. Begin-it. End 19
More details in: http: //www. cs. usask. ca/grads/wew 036/stl 20
21
#include <vector> #include <algorithm> void main(void) { vector<int> v. Int; vector<int>: : iterator begin, end; begin = v. Int. begin(); end = v. Int. end(); sort(begin, end); } 22
#include <iostream> //not iostream. h! #include <cstdio> #include <cstdlib> #include <vector> #include <list> #include <set> #include <map> #include <string> #include <algorithm> #ifdef _MSC_VER // for Visual C++ using namespace std; #endif 23
- Slides: 23