Standard Template Library

Reviews
Standard Template Library  Programming paradigm: generic programming   the decomposition of programs into components which may be developed separately and combined arbitrarily, subject only to well-defined interfaces. lots of orthogonality Components    Containers Iterators Algorithms Example 1 // Simple STL vector program from Ira Pohl, UC Santa Cruz #include #include using namespace std; int main () { vector v(100); // 100 is vector's size for (int i = 0; i < 100; ++i) v[i] = i; for (vector::iterator p = v.begin(); p != v.end(); ++p) cout << *p << '\t'; cout << endl; } Example 2 #include #include // list container #include // for accumulate using namespace std; // Using the list container int main() { double w[4] = { 0.9, 0.8, 88, -99.99 }; list z; for (int i = 0; i < 4; ++i) z.push_front(w[i]); print(z); z.sort(); print(z); cout << "sum is " << accumulate(z.begin(), z.end(), 0.0) << endl; } Containers  Sequential – ordered by sequence    Vector – dynamic array List – linked list Deque – double-ended queue Sets – stores unique keys Multisets – allows multiple copies of key Maps – associates unique keys and values Multimaps – allows multiple copies of key  Associative – use keys for ordering     Common container interfaces       Constructors Element access Element insertion Element deletion Destructor Iterators Useful definitions and members       CON::iterator CON::value_type c.begin() c.end() c.size() c.empty() Useful sequence members      c.insert(w_it, v) – inserts v before w_it c.insert(w_it,v,n) – inserts n copies of v before w_it c.insert(w_it,b_it,e_it) – inserts b_it to e_it before w_it c.erase(w_it) – erases element at w_it c.erase(b_it,e_it) – erases b_it to e_it Useful Associative Container Definitions, Members    ASSOC::key_type c.find(k) – returns iterator to element with key k or c.end() c.count(k) – returns number of elements with key k Example 3 - Map //Associative Containers - looking up ages #include #include #include using namespace std; int main() { map > name_age; name_age["Pohl,Laura"] = 12; name_age["Dolsberry,Betty"] = 39; name_age["Pohl,Tanya"] = 14; cout << "Laura is " << name_age["Pohl,Laura"] << " years old." << endl; } Example 4 - Multiset #include #include //used for both set and multiset #include using namespace std; enum vegetables { broccoli, tomato, carrot, lettuce, beet, radish, potato}; int main() { vector my_diet(100); vector::iterator pos; vegetables veg; multiset > v_food; multiset >::iterator vpos; for (pos = my_diet.begin(); pos != my_diet.end(); ++pos) { *pos = static_cast(rand() % 7); v_food.insert(*pos); } for (veg = broccoli; veg <= potato; ++veg) cout << v_food.count(veg) << endl; } Example 5- Container Adaptors //Adapt a stack from a vector #include #include #include #include using namespace std; int main() { stack > str_stack; string quote[3] = { “Let’s all visit\n", “Greensburg\n",“Bill Watts\n" }; for (int i = 0; i < 3; ++i) str_stack.push(quote[i]); while (!str_stack.empty()) { cout << str_stack.top(); str_stack.pop(); } } Algorithms     Sorting Nonmutating sequence algorithms Mutating sequence algorithms Numerical algorithms Example 6 - Quicksort //Using sort() from STL #include #include using namespace std; const int N = 5; int main() { int d[N], i, *e = d + N; for (i = 0; i < N; ++i) d[i] = rand(); sort(d, e); for (i = 0; i < N; ++i) cout << d[i] << '\t'; } Other useful sort-related algorithms    binary_search(b,e,t) – true if t is found in b to e min_element(b,e) – returns iterator for minimum element in b to e includes(b1,e1,b2,e2) – returns true if second sequence is subset of first Example 7 - find //Use of the find function #include #include using namespace std; int main() { string words[5] = { "my", "hop", "mop", "hope", "cope"}; string* where; where = find(words, words + 5, "hop"); cout << *++where << endl; //mop sort(words, words + 5); where = find(words, words + 5, "hop"); cout << *++where << endl; //hope } Useful non-mutating sequence algorithms    for_each(b,e,f) – applies function f to each value in b to e count_if(b,e,p,n) – returns in n the count of elements that make predicate p true equal(b1,e1,b2) – returns true if sequences match Useful mutating sequence algorithms   replace(b1,e1,t1,t2) – replaces t1 by t2 if t1 occurs in range b1 to e1 replace_if(b1,e1,p,t2) – replaces elements in b1 to e1 that satisfy predicate p with t2 Function objects  Classes/structs that have operator() defined template struct notZero : public unary_function { bool operator()(Number x) const { return x != (Number) 0; }

Related docs
The Standard Template Library
Views: 8  |  Downloads: 2
Standard Template Library
Views: 16  |  Downloads: 1
Standard Template Library
Views: 15  |  Downloads: 1
The Standard Template Library
Views: 14  |  Downloads: 0
Standard Template Library
Views: 19  |  Downloads: 1
Standard Template Library
Views: 5  |  Downloads: 0
Standard Template Library
Views: 28  |  Downloads: 0
Standard Template Library
Views: 191  |  Downloads: 2
The Standard Template Library
Views: 0  |  Downloads: 0
MCSTL The Multi-Standard Template Library
Views: 4  |  Downloads: 0
STANDARD TEMPLATE
Views: 27  |  Downloads: 1
The C++ Standard Template Library
Views: 18  |  Downloads: 2
Other docs by moneu