Title: C Standard Library Facilities
1C Standard Library Facilities
- Basic run-time language support
- allocation, run-time type info, ...
- C standard library
- Strings and I/O
- international character sets, localization, ...
- Containers and algorithms using containers
- vector, list, map,... traversal, sort, merge,...
- Support for numerical computation
- complex, BLAS-like operations, slices, ...
STL
2STL Components
- containers
- data structure templates
- vector, list, deque, (priority-)queue, stack
- (multi-)map, (multi-)set
- generic algorithms
- independent of containers
- iterators
- pointer-like objects used to cycle through all
the elements stored in a container
3containers, iterators, algorithms
vector ltTgt
sort
list ltTgt
find
4containers, iterators, algorithms
- include ltvectorgt include ltvector.hgt
- include ltalgorithmgtinclude ltalgo.hgt
- include ltiostreamgt include ltiostream.hgt
- using namespace std
- int ia1033,17,11,88,43,99,6,9,12,7
- int main()
- vectorltintgt x(ia,ia10)
- sort(x.begin(), x.end())
5- // grab value to search for
- int s_value
- cin gtgt s_value
- // search for an element
- vectorltintgtiterator found
- foundfind(x.begin(),x.end(),s_value)
- if(found ! x.end())
- cout ltlt "search value found!\n"
-
- else
- cout ltlt "search value not found!\n"
-
6list instead of vector
- include ltlistgt
- include ltalgorithmgt
- include ltiostreamgt
- using namespace std
- int ia1033,17,11,88,43,99,6,9,12,7
- int main()
- listltintgt x(ia,ia10)
- sort(x.begin(), x.end())
7- // grab value to search for
- int s_value
- cin gtgt s_value
- // search for an element
- vectorltintgtiterator found
- foundfind(x.begin(),x.end(),s_value)
- if(found ! x.end())
- cout ltlt "search value found!\n"
-
- else
- cout ltlt "search value not found!\n"
-
8Algorithms with built-in arrays
- include ltalgorithmgt
- include ltiostreamgt
- using namespace std
- int ia1033,17,11,88,43,99,6,9,12,7
- int main()
- int s_value
- cout ltlt "enter search value "
- cin gtgt s_value
- int found
- foundfind(ia0,ia10,s_value)
- if(found ! ia10) ...
9Containers
- Sequence Containers
- Vector
- List
- Deque
- Stack
- Queue Adaptor
- Priority_queue
- Almost Containers
- Built-in arrays, strings, valarrays, bitsets
- Associative Containers
- Map
- Multimap
- Set
- Multiset
10string Type
- include ltiostreamgt
- include ltstringgt
- include ltalgorithmgt
- using namespace std
- string s1("Hello")
- string s2("World")
- cout ltlt s1 " " s2 '\n'
- string s3(s1 " " s2)
- cout ltlt '\"'ltlt s3 ltlt "\" has " ltlt s3.length()
- ltlt " characters" ltlt endl
- replace(s3.begin(), s3.end(), 'W', 'w')
- cout ltlt s3 ltlt endl
11Constructors
- container()
- container(n) (not for ass. cont.)
- container(n,x) (not for ass. cont.)
- container(first,last)
- container(c)
- container()
12Stack, Queue, List Operations
- push_back() pop_back()
- push_front() pop_front()
- insert(p,x)
- insert(p,n,x)
- insert(p,first,last)
- erase(p)
- erase(first,last)
- clear()
13Container Summary
14Algorithms
- copy
- sort
- find
- fill
- partition
- insert, delete
- union, intersection
- accumulate
- ...
15Iterators
- The glue that makes it possible to use generic
algorithms and orthogonalize those algorithms
from the data structures - An iterator i is a generalized means of
traversing a data structure - for an array, an array index or a pointer
- Dereferencing an iterator, i, is guaranteed to
give the item, but an iterator does not obey all
pointer operations
16Iterators, Element Access
- begin()
- end()
- rbegin()
- rend()
- front()
- back()
-
- at()
- Points to first element
- Points to one-past-last element
- Points to first element of reverse seq.
- Points to one-past-last element of rev. seq.
- First element
- Last element
- Subscripting, unchecked access
- Subscripting, checked access
17Iterators (continued)
- containerltTgtiterator firstc.begin()
- containerltTgtiterator lastc.end()
- first,last)
- i
- first last
- i ! last
- i n (long jump)
- i x
- x i
18Iterator Operations
19STL Components (continued)
- function objects
- an instance of a class that defines the
parenthesis operator as a member function - to be passed as argument instead of functions
- adaptors
- container adapters provide a restricted interface
- eg. stack and queue are adaptors to vector, list
or dequeue - allocators
20Nonmodifying Sequence Operations
- for_each()
- find()
- find_if()
- find_first_of()
- adjacent_find()
- count()
- count_if()
- mismatch()
- equal()
- search()
- find_end()
- search_n()
listltPersongt all ... for_each(all.begin(),all.en
d(),Print(cout))
21Function objects
- class bThan
- public
- bThan(int x) testValue(x)
- const int testValue
- bool operator() (int val)
- return val gt testValue
-
- listltintgtiterator firstBig
- find_if(aList.begin(),aList.end(),bThan(12))
22STL Components
- containers
- data structure templates
- vector, list, deque, (priority-)queue, stack
- (multi-)map, (multi-)set
- generic algorithms
- independent of containers
- iterators
- pointer-like objects used to cycle through all
the elements stored in a container
23Future of OO
- STL is not OO, is OOP dead?
- Not all problems are best solved in OOP fashion!
- Many problems are best solved in an OOP manner.
- Know as much as you can about as many styles of
programming as you can, an use the style most
appropriate to the problem.