210 likes | 284 Views
Introduction to Effective C++ Programming. Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 4. Understanding The Standard Template Library. Containers
E N D
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 4
Understanding The Standard Template Library • Containers • A container is an object that holds other objects. You can add objects to a container and remove objects from it. • Standard Library Algorithms • Iterators and Allocators • Iterators : They provide an abstract view of data. They are an abstraction of the notion of a pointer to an element of a sequence. • The element currently pointed to : *, -> operations • Point to next element : ++ • Equality : == • Allocators : They are used to insulate container implementations from details of access to memory.
Containers • <vector> : one-dimensional array • <list> : doubly-linked list • <deque> : double-ended queue • <queue> : queue • queue, priority queue • <stack> : stack • <map> : associative array • Map, multimap • <set> : set • Set, multiset • <bitset> : set of booleans
General Info. • Sequences • <vector> <list> <deque> • Adapters • <queue> <stack>
Example : Vector 1 (Use of []) #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(void) { vector<int> v(5); int i; for(i=0;i<5;i++) { v[i] = i; cout << v[i] << endl; } int sum=0; for(i=0;i<5;i++) sum += v[i]; cout << "Sum : " << sum << endl; }
Example : Vector 2 (Use of Iterators) #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(void) { vector<int> v(5); int sum; vector<int>::iterator first = v.begin(); vector<int>::iterator last = v.end(); sum = 0; while(first != last) { sum += *first; cout << *first << endl; ++first; } cout << "sum : " << sum << endl; }
Example : Vector 3 (Stack Operation) #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(void) { vector<int> v(5); int sum; v.push_back(100); vector<int>::iterator first = v.begin(); vector<int>::iterator last = v.end(); sum = 0; while(first != last) { sum += *first; cout << *first << endl; ++first; } cout << "sum : " << sum << endl; }
Example : Vector 3 (Various Operations) vector<int> v(5); v.push_back(100); // increase the size of v by one v.pop_back(); // decrease the size by one v.back() // extract the last object. v.erase(iterator); v.insert(iterator,int); // insert int after what iterator points to v.size() // current size v.capacity() // v.max_size() //
Example : List (Use of Iterators) • No subscripting : operator[] • Bidirectional iterator • Additional operations • splice, merge, front, push_front, pop_front • sort, unique
Example : List (Use of Iterators) #include <iostream> #include <list> #include <algorithm> using namespace std; int main(void) { list<int> v(5); int sum; list<int>::iterator first = v.begin(); list<int>::iterator last = v.end(); sum = 0; while(first != last) { sum += *first; cout << *first << endl; ++first; } cout << "sum : " << sum << endl; }
Example : List of List 1 #include <iostream> #include <list> using namespace std; typedef list<int> List; typedef list<List> ListofList; void print_list(const List& list1, int id); int main(void) { ListofList listoflist1; for(int i=0;i<3;i++) { List list1; for(int j=0;j<4;j++) list1.push_back(i*4 + j); listoflist1.push_back(list1); } ListofList::iterator it = listoflist1.begin(); ListofList::iterator ie = listoflist1.end(); for(int j=1;it != ie; ++it,++j) { const List &list1 = *it; print_list(list1,j); } return 0; }
Example : List of List 2 void print_list(const List& list1, int id) { cout << "list " << id << " : "; List::const_iterator ils = list1.begin(); List::const_iterator ile = list1.end(); while(ils != ile) cout << *ils++ << ' '; cout << endl; }
Example : Stack #include <iostream> #include <string> #include <stack> using namespace std; int main(void){ stack<string> s; s.push("banana"); s.push("apple"); cout << s.top() << " " << s.size() << endl; s.pop(); cout << s.top() << " " << s.size() << endl; s.pop(); return 0; }
Example : Queue & Priority_Queue #include <iostream> #include <string> #include <queue> using namespace std; int main(void){ queue<string> s; s.push("banana"); s.push("apple"); cout << s.front() << " " << s.size() << endl; s.pop(); cout << s.front() << " " << s.size() << endl; s.pop(); priority_queue<int> k; k.push(56); k.push(2); k.push(100); k.push(5); cout << k.top() << endl; k.pop(); cout << k.top() << endl; return 0; }
Associative Containers • map : a single value is associated with each unique key. • multimap : an associative array that allows duplicate elements for a given key. • set, multiset : degenerate associative arrays in which no value is associated with a key.
map • A sequence of (key, value) pairs that provides for fast retrieval based on the key. • Each key in a map is unique. • It provides bidirectional iterators • hash_map : useful for a case that there is no need to keep the container sorted.
Example : map #include <iostream> #include <string> #include <map> using namespace std; int main(void){ map<string,int> salary; salary["Steve"] = 2000; salary["Tom"] = 3000; salary["John"] = 4500; salary["Neal"] = 3000; salary["Jane"] = 4000; int total = 0; typedef map<string,int>::const_iterator CI; for(CI p = salary.begin(); p!=salary.end(); ++p) { total += p->second; cout << p->first << '\t' << p->second << '\n'; } cout << "-----------------\n total \t" << total << endl; cout << "Tom's salary : " << salary["Tom"] << endl; cout << "Jane's salary : " << salary.find("Jane")->second << endl; return 0; }
Algorithms • STL provides algorithms to serve the most common and basic needs. • Most algorithms operate on sequences using iterators and values. • Function objects • Useful when we want the algorithms to execute code that we supply.
Algorithms • Example for function objects bool less_than_7(int v) { return v < 7; } Void f(list<int>& c) { list<int>::const_iterator p = find_if(c.begin(),c.end(),less_than_7); }
Algorithms • Find, for_each, count, copy, search, sort, binary search, etc.