Title: Programming Using the C Standard Template Library
1Programming Using the C Standard Template
Library
- Tim Smith
- October 13, 2004
2What is the STL?
- A collection of classes to make C programming
more efficient - Algorithms, Containers, and Iterators
- Function objects, Allocators, and Adapters
Function Objects
Allocators
Iterators
Containers
Algorithms
Adapters
3STL Containers
- Sequences
- Vector
- Bit_vector
- Deque
- List
- Slist
4STL Containers
- Associative Containers
- Set
- Multiset
- Map
- Multimap
- Hash_set
- Hash_map
- Not part of the C Standard
5Creating Containers
- includeltvectorgt
- includeltlistgt
- includeltmapgt
- using namespace std
- int main()
- vectorltintgt v1 //empty vector of
integers - vectorltintgt v2(100) //vector of 100
integers. - //initialized using
default integer constructor. - vectorltintgt v3(100,5) //vector of 100
integers all initialized to 5. - listltdoublegt l1 //empty list of
doubles. - listltdoublegt l2(100) //list of 100
doubles. - //initialized with
default constructor. - listltdoublegt l3(100,3.14) //list of 100
doubles all initialized to 3.14. - mapltint,doublegt m1 //empty map that
associates integers with doubles -
6Inserting and Deleting Elements
- includeltvectorgt
- includeltlistgt
- includeltmapgt
- using namespace std
- int main()
- vectorltintgt v1 //empty vector of
integers - listltdoublegt l1 //empty list of
doubles. - mapltint,doublegt m1 //empty map that
associates integers with doubles - int x 3
- double y 3.14
- v1.push_back(x) //insert a copy of x
at end of vector - v1.insert(v1.begin(),x) //insert a copy of x
before the first element - l1.push_back(y) //insert a copy of y
at end of list - l1.push_front(y) //insert a copy of y
at front of list
7Accessing Elements in Containers
- Access to an element stored in a container is
done through STL iterators most of the time - An iterator is an abstraction that provides data
access operations needed by algorithms (read,
write, next element, prev element, etc.) - STL has several different iterator types
Input
Forward
Bidirectional
Random Access
Output
8STL Iterators
- Used as arguments to algorithms
- Containers provide several iterators
- Begin, End, Reverse Begin, Reverse End
Input Operations , , !, , -gt, , No
assignment of i
Output Operations , ,
Forward Operations , , !, , -gt,
Bidirectional Operations , , !, , -gt, , --
Random Operations , , !, , -, , -gt, ,
, -, --, n, lt, lt, gt, gt
9Iterator Operations
- Input
- (read), (next element)
- Output
- (write), (next element)
- Forward
- (read/write), (next element)
- Bidirectional
- (read/write), (next element), --
(previous element) - Random Access
- (read/write), /-- (next/prev element),
(next nth element), - (previous nth element)
10Iterator Operations Example
- includeltvectorgt
- includeltlistgt
- using namespace std
- int main()
- listltintgt l(10)
- vectorltintgt v(10)
- listltintgtiterator i l.begin()
- i i i-- --i
- int x i
- i 7
- vectorltintgtiterator j v.begin()
- j j j-- --j
- j 5 j - 5
- int y j
11Iterating Through a Container
- Containers all provide begin() and end()
- begin() returns iterator pointing to first
element - end() returns iterator pointing one past the last
element - rbegin() and rend() provide to iterate through
container from end to beginning - use containerlttypegtreverse_iterator
12Iterate Through Container
- include ltiostreamgt
- include ltvectorgt
- using namespace std
- int main()
- vectorltintgt v(10,7)
- for (vectorltintgtiterator i v.begin()
- i ! v.end()
- i)
- cout ltlt i ltlt " "
-
- cout ltlt endl
13STL Algorithms
- Each algorithm examined to identify data access
operations used. - Algorithms written using the appropriate STL
iterator. - Allows one algorithm to work on multiple types of
containers that provide the appropriate iterator
type. - Most algorithms work with Forward Iterators.
14STL Algorithms
- 4 categories of algorithms
- Non-modifying For each, Find, Count, Equal,
Search, etc. - Mutating Copy, Generate, Reverse, Remove, Swap,
etc. - Sorting Related Sort, Stable sort, Binary
search, Merge, etc. - Numeric Accumulate, Inner product, Partial
sum, Adjacent difference
15Sort Elements in a Vector
- includeltiostreamgt
- includeltvectorgt
- includeltalgorithmgt
- using namespace std
- int main()
- vectorltintgt v
- for (int i 0 i lt 10 i 2)
- v.push_back(i)
- for (int i 1 i lt 10 i 2)
- v.push_back(i)
- for (vectorltintgtiterator i v.begin() i !
v.end() i) - cout ltlt i ltlt " "
- cout ltlt endl
- sort(v.begin(), v.end())
16Function Objects
- Used by some algorithms
- Applied to each element in input by For each
- Used to create new elements by Generate
- Predicates - Function Objects that return bool
- Unary and Binary predicates used to test equality
- Find If uses unary, Sort uses binary
17Example Function Objects
- class generator
- private
- int counter
- public
- generator() counter(0)
- int operator()()
- return counter
-
-
- class printer
- public
- int operator()(int val)
- cout ltlt val ltlt " "
-
-
18- int main()
- vectorltintgt v(10)
- generator gen
- printer prn
- for_each(v.begin(), v.end(), prn) //Print the
uninitialized vector - cout ltlt endl
- generate(v.begin(), v.end(), gen)
//Initialize the vector - for_each(v.begin(), v.end(), prn) //Print the
vector again - cout ltlt endl
-
19Predicate Object Example
- includeltiostreamgt
- includeltlistgt
- includeltalgorithmgt
- includeltfunctionalgt
- using namespace std
- struct is_zero public unary_functionltint, boolgt
- bool operator()(int x) return x 0
-
- int main()
- listltintgt l
- l.push_back(-1) l.push_back(0)
l.push_back(1) l.push_back(-3) - listltintgtiterator result find_if(l.begin(),
l.end(), -
bind2nd(greaterltintgt(), 0)) - if (result ! l.end())
- cout ltlt result ltlt endl
20STL Summary
- STL allows programmers to focus on application
specific issues - Provides collections of algorithms, containers,
iterators, and other objects - Programmers dont reinvent the wheel
21STL Resources
- http//www.sgi.com/tech/stl/ SGIs STL reference
documentation - Musser, Derge, Saini. STL Tutorial and Reference
Guide. C Programming with the Standard Template
Library, 2nd Edition. Addison-Wesley, 2001. - Josuttis.The C Standard Library A Tutorial and
Reference. Addison-Wesley, 1999. - Stroustrup. The C Programming Language, 3rd
Edition. Addison-Wesley, 1997.