STL What is STL Standard Templates Library Templates
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 Iterators Algorithms Search Associative Vector Map List Set 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 <iostream> #include <vector> #include <string> using namespace std; main() { vector<string> 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 <iostream> #include <vector> #include <string> using namespace std; main() { vector<string> v; //initialization omitted Output Sunday Monday Tuesday // for(int i = 0; i < v. size(); i++) { // cout << v[i] << endl; // } vector<string>: : iterator iter; for (iter = v. begin(); iter != v. end(); ++iter){ cout << *iter << endl; } }
Iterators • Represent a location within collection v. begin() (v. begin())++ v v. end() cout<<*(v. end()); //ERROR • 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
Example: const iterators vector<string> > v; … vector<string> >: : iterator iter; for (iter = v. begin(); iter != v. end(); ++iter) { cout << (*iter). size() << endl; // ok (*iter). push_back(“Hello”); //ok } vector<string> >: : 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<string> v; v. push_back(“Sunday”); … vector<string>: : iterator iter; for (iter = v. begin(); iter != v. end(); ++iter){ cout << *iter << endl; } list<string> v; v. push_back(“Sunday”); … list<string>: : 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 <iostream> #include <map> #include <string> using namespace std; int main() { map<string, int> freq; // word->frequency map; //--- Read words from input stream while (cin >> word) { freq[word]++; } //--- Write the count and the word. map<string, int>: : const_iterator iter; for (iter=freq. begin(); iter != freq. end(); ++iter) { cout << iter->second << " " << iter->first << endl; } return 0; }//end main Source: http: //www. fredosaurus. com/notes-cpp/stl-containers/map-ex-wordfreq. html
#include <iostream> #include <vector> #include <algorithm> using namespace std; Algorithms int main() { vector<int> 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<int>: : iterator p = find( v. begin(), v. end(), 1 ); if(p==v. end()) cout<<"not foundn"; else v. erase(p); for (int i=0; i<v. size(); ++i) { cout << v[i] << ' '; } cout<<endl; return 0; }
Algorithms #include <algorithm> #include <iostream> using namespace std; void print(int i) { cout << i << ' '; } bool More. Than 1(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], More. Than 1)); return 0; }
- Slides: 13