STL
What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the same on lots of data types Sorting etc. This is what STL provides Before implementing a template, check STL
STL in a glance STL Containers Sequence Vector List Associative Map Set IteratorsAlgorithms Search Sort (A non-exhaustive list)
STL Containers Manage a collection of elements Different implementations –Different APIs map provides access by key, vector doesn’t –Different operation complexities
Example: using a vector #include using namespace std; main() { vector v; v.push_back(“Sunday”); v.push_back(“Monday”); v.push_back(“Tuesday”); for(int i = 0; i < v.size(); i++) { cout << v[i] << endl; } Output Sunday Monday Tuesday
Example: iterators #include using namespace std; main() { vector v; //initialization omitted // for(int i = 0; i < v.size(); i++) { // cout << v[i] << endl; // } vector ::iterator iter; for (iter = v.begin(); iter != v.end(); ++iter){ cout << *iter << endl; } Output Sunday Monday Tuesday
Iterators Represent a location within collection Operators: * get the collection’s element at the represented location ==, != compare locations’ identity ++ move to the next position -- move to the previous position +=, -= jump several positions forward/backward Container may invalidate iterators if modified Use const_iterator to protect referenced element from accidental modification v.begin()(v.begin())++ v v.end() cout<<*(v.end()); //ERROR
Example: const iterators vector > v; … vector >::iterator iter; for (iter = v.begin(); iter != v.end(); ++iter) { cout << (*iter).size() << endl; // ok (*iter).push_back(“Hello”); //ok } vector >::const_iterator iter; for (iter = v.begin(); iter != v.end(); ++iter){ cout << (*iter).size() << endl; // ok (*iter).push_back(“Hello”); //error }
vector vs. list vector v; v.push_back(“Sunday”); … vector ::iterator iter; for (iter = v.begin(); iter != v.end(); ++iter){ cout << *iter << endl; } list v; v.push_back(“Sunday”); … list ::iterator iter; for (iter = v.begin(); iter != v.end(); ++iter){ cout << *iter << endl; }
vector vs. list (cont.) vector can do everything list can –And then some operator[] Why have list at all? –Operations have different cost –Which one is best depends on application
Example: map // Word frequencies -- using map #include using namespace std; int main() { map freq; // word->frequency map; //--- Read words from input stream while (cin >> word) { freq[word]++; } //--- Write the count and the word. map ::const_iterator iter; for (iter=freq.begin(); iter != freq.end(); ++iter) { cout second first << endl; } return 0; }//end main Source:
Algorithms #include using namespace std; int main() { vector v; for (int i=1; i<=7; ++i) { v.push_back(i); } for (int i=0; i<v.size(); ++i) { cout << v[i] << ' '; } cout<<endl; vector ::iterator p = find( v.begin(), v.end(), 1 ); if(p==v.end()) cout<<"not found\n"; else v.erase(p); for (int i=0; i<v.size(); ++i) { cout << v[i] << ' '; } cout<<endl; return 0; }
Algorithms #include using namespace std; void print(int i) { cout << i << ' '; } bool MoreThan1(int i) { return i > 1; } int main() { int a[5] = {5, 4, 1, 3, 2}; sort(&a[0], &a[5]); for_each(&a[0], &a[5], print); cout << '\n' << *( find_if(&a[2], &a[5], MoreThan1)); return 0; }