Title: Introduction to Effective C Programming
1Introduction to Effective C Programming
- Kwanghee Ko
- Design Laboratory
- Department of Ocean Engineering
- Massachusetts Institute of Technology
- Day 4
2Understanding 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 , -gt
operations - Point to next element
- Equality
- Allocators They are used to insulate container
implementations from details of access to memory.
3Containers
- ltvectorgt one-dimensional array
- ltlistgt doubly-linked list
- ltdequegt double-ended queue
- ltqueuegt queue
- queue, priority queue
- ltstackgt stack
- ltmapgt associative array
- Map, multimap
- ltsetgt set
- Set, multiset
- ltbitsetgt set of booleans
4General Info.
- Sequences
- ltvectorgt ltlistgt ltdequegt
- Adapters
- ltqueuegt ltstackgt
5Example Vector 1 (Use of )
- include ltiostreamgt
- include ltvectorgt
- include ltalgorithmgt
- using namespace std
- int main(void)
-
- vectorltintgt v(5)
- int i
- for(i0ilt5i)
- vi i
- cout ltlt vi ltlt endl
-
- int sum0
- for(i0ilt5i)
- sum vi cout ltlt "Sum " ltlt sum ltlt endl
6Example Vector 2 (Use of Iterators)
- include ltiostreamgt
- include ltvectorgt
- include ltalgorithmgt
- using namespace std
- int main(void)
-
- vectorltintgt v(5) int sum
- vectorltintgtiterator first v.begin()
- vectorltintgtiterator last v.end()
- sum 0
- while(first ! last)
- sum first
- cout ltlt first ltlt endl first
-
- cout ltlt "sum " ltlt sum ltlt endl
7Example Vector 3 (Stack Operation)
- include ltiostreamgt
- include ltvectorgt
- include ltalgorithmgt
- using namespace std
- int main(void)
-
- vectorltintgt v(5) int sum
- v.push_back(100)
- vectorltintgtiterator first v.begin()
- vectorltintgtiterator last v.end()
- sum 0
- while(first ! last)
- sum first
- cout ltlt first ltlt endl first
-
- cout ltlt "sum " ltlt sum ltlt endl
8Example Vector 3 (Various Operations)
- vectorltintgt 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() //
9Example List (Use of Iterators)
- No subscripting operator
- Bidirectional iterator
- Additional operations
- splice, merge, front, push_front, pop_front
- sort, unique
10Example List (Use of Iterators)
- include ltiostreamgt
- include ltlistgt
- include ltalgorithmgt
- using namespace std
- int main(void)
-
- listltintgt v(5) int sum
- listltintgtiterator first v.begin()
- listltintgtiterator last v.end()
- sum 0
- while(first ! last)
- sum first
- cout ltlt first ltlt endl first
-
- cout ltlt "sum " ltlt sum ltlt endl
11Example List of List 1
include ltiostreamgt include ltlistgt using
namespace std typedef listltintgt List typedef
listltListgt ListofList void print_list(const
List list1, int id) int main(void)
ListofList listoflist1 for(int i0ilt3i)
List list1 for(int
j0jlt4j) list1.push_back(i4
j) listoflist1.push_back(list1)
ListofListiterator it
listoflist1.begin() ListofListiterator ie
listoflist1.end() for(int j1it ! ie
it,j) const List list1 it
print_list(list1,j) return 0
12Example List of List 2
void print_list(const List list1, int id)
cout ltlt "list " ltlt id ltlt " "
Listconst_iterator ils list1.begin()
Listconst_iterator ile list1.end()
while(ils ! ile) cout ltlt ils ltlt '
' cout ltlt endl
13Example Stack
include ltiostreamgt include ltstringgt include
ltstackgt using namespace std int main(void)
stackltstringgt s s.push("banana")
s.push("apple") cout ltlt s.top() ltlt " "
ltlt s.size() ltlt endl s.pop() cout ltlt
s.top() ltlt " " ltlt s.size() ltlt endl
s.pop() return 0
14Example Queue Priority_Queue
include ltiostreamgt include ltstringgt include
ltqueuegt using namespace std int main(void)
queueltstringgt s s.push("banana")
s.push("apple") cout ltlt s.front() ltlt " "
ltlt s.size() ltlt endl s.pop() cout ltlt
s.front() ltlt " " ltlt s.size() ltlt endl
s.pop() priority_queueltintgt k
k.push(56) k.push(2) k.push(100)
k.push(5) cout ltlt k.top() ltlt endl
k.pop() cout ltlt k.top() ltlt endl
return 0
15Associative 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.
16map
- 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.
17Example map
include ltiostreamgt include ltstringgt include
ltmapgt using namespace std int main(void)
mapltstring,intgt salary salary"Steve"
2000 salary"Tom" 3000
salary"John" 4500 salary"Neal"
3000 salary"Jane" 4000 int
total 0 typedef mapltstring,intgtconst_it
erator CI for(CI p salary.begin()
p!salary.end() p) total
p-gtsecond cout ltlt p-gtfirst ltlt '\t'
ltlt p-gtsecond ltlt '\n' cout ltlt
"-----------------\n total \t" ltlt total ltlt endl
cout ltlt "Tom's salary " ltlt salary"Tom" ltlt
endl cout ltlt "Jane's salary " ltlt
salary.find("Jane")-gtsecond ltlt endl return
0
18Algorithms
- 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.
19Algorithms
- Example for function objects
bool less_than_7(int v) return v lt 7 Void
f(listltintgt c) listltintgtconst_iterator p
find_if(c.begin(),c.end(),less_than_7)
20Algorithms
- Find, for_each, count, copy, search, sort, binary
search, etc.