Title: E81 CSE 532S: Advanced MultiParadigm Software Development
1E81 CSE 532S Advanced Multi-Paradigm Software
Development
STL Containers
Christopher Gill, Haijun Qiu, Zhao
Xing Department of Computer Science and
Engineering Washington University, St.
Louis cdgill_at_cse.wustl.edu
2Assigned Reading
- STL Containers
- Austern, Chapters 5 and 9
- optional Austern, Chapter 16
3Review C Standard Template Library
- The STL is a collection of related software
elements - Iterators (define ranges)
- Variables used to give flexible access to the
values in a container - Algorithms (operate over ranges)
- Functions that use iterators to access values in
containers - Perform computations that modify values, or
creates new ones - Function objects (modify algorithms operations)
- Encapsulate a function as an object
- Containers (the subject of todays lecture)
- Data structures store item ranges according to
specific organization - STL implementation requires advanced C features
- Function/class templates, typedefs, traits,
associated types - An example of, and a framework for, generic
programming
4Intro to C STL Containers
- Goals
- See how STL containers are implemented
- See how differences in implementation affect use
- Well cover several kinds
- Focus on template concepts
- And how each containers concept relates to its
implementation - Example to the left prints
- v0 is 1
- v1 is 2
- v2 is 4
- include ltiostreamgt
- include ltvectorgt
- using namespace std
- int main (int, char )
- vectorltintgt v
-
- // This would be asking for trouble....
- // v0 1 v1 2 v2 4
- // ... but this works fine...
- v.push_back (1)
- v.push_back (2)
- v.push_back (4)
- // ... and now this is ok ...
- for (size_t s 0 s lt v.size() s)
- cout ltlt "v" ltlt s ltlt " is "
- ltlt vs ltlt endl
-
5Example STL Container from a C Array
template ltclass T, size_t Ngt struct block
typedef T value_type //
type traits typedef value_type pointer
typedef const value_type const_pointer
typedef value_type reference typedef
const value_type const_reference typedef
size_t size_type typedef pointer
iterator // iterator types
typedef const_pointer const_iterator
From Matthew H. Austern, Generic Programming and
the STL, pp. 61
6Array-based STL Container, Continued
template ltclass T, size_t Ngt struct block
iterator begin() return data //
iterator accessors iterator end() return
data N const_iterator begin() const
return data const_iterator end() const
return data N reference operator
(size_type n) // container interface
return datan const_reference operator
(size_type n) const return datan
size_type size() const return N T data
N // contained
elements
7Array-based STL Container, Continued
template ltclass T, size_t Ngt struct block ...
// reverse iterator accessors // ri(i)
(i-1) (ri.base()-1) typedef
reverse_iteratorltiteratorgt reverse_iterator
typedef reverse_iteratorltconst_iteratorgt
const_reverse_iterator iterator rbegin()
return reverse_iterator(end())
iterator rend() return
reverse_iterator(begin()) const_iterator
rbegin() const return const_reverse_iterat
or(end()) const_iterator rend() const
return const_reverse_iterator(begin()) ...
8Requirements for all Containers
- Contain instances of a parameterized type
- Contained sets of instances cannot overlap
- An instance cannot belong to more than one
container - Creation or duplication of a range copies
instances (by value) - A container owns its instances (nested lifetimes)
- Containers are designed to work with iterators
- Declare appropriate iterator types (traits)
- Provide iterators through accessor/factory
methods - Containers provide an interface to the instances
- Functions to add/remove instances (e.g.,
push_back()) - Operators on the containers to allow access to
instances - E.g., random access containers like vector, deque
provide - Interfaces must obey the principle of least
surprise - E.g., list and slist dont have since cant
guarantee O(1)
9Containers Must Also Work with Iterators
- Declare appropriate iterator types (traits)
- typedef pointer iterator
- typedef const_pointer const_iterator
- Provide iterator accessor/factory methods
- iterator begin() return data
- iterator end() return dataN
- const_iterator begin() const return data
- const_iterator end() const return dataN
- iterator rbegin() return reverse_iterator(end())
- iterator rend() return reverse_iterator(begin()
) - const_iterator rbegin() const
- return const_reverse_iterator(end())
- const_iterator rend() const
- return const_reverse_iterator(begin())
10Hierarchy of STL Container Concepts
From Matthew H. Austern, Generic Programming
and the STL
Container
Forward Container
Associative
Container
Sequence
Reversible Container
Simple Associative Container
Pair Associative container
Front Insertion Sequence
Back Insertion Sequence
Random Access Container
Unique Associative Container
Multiple Associative Container
Sorted Associative Container
Hashed Associative Container
11Containers Can Model Multiple Concepts
Container
- Notice containers can have multiple
classifications - Useful to look at differences between data
structures! - Back vs. front insertion
- Forward vs. reversible vs. random access
- More general concepts higher in the hierarchy
- More specific concepts appear farther down
Forward Container
slist
Sequence
list
Reversible Container
Front Insertion Sequence
Back Insertion Sequence
Random Access Container
deque
vector
refined by
models
12Container Top of its Concept Hierarchy
Container
- Valid Expressions Complexity
- Copy constructor X(a) linear
- Copy constructor X b(a) linear
- Assignment operator b a linear
- Destructor a.X() linear
- Beginning of range a.begin() constant
- End of range a.end() constant
- Size a.size() container-dependent
- Maximum size a.max_size() constant
- Empty a.empty()
constant - Swap a.swap(b)
linear
13Forward Container
Container
deque
vector
Forward Container
slist
list
map
set
multimap
multiset
- Additional Expressions Complexity
- Equality a b linear
- Inequality a ! b
linear - Less than a lt b
linear - Greater than a gt b
linear - Less than or equal a lt b
linear - Greater than or equal a gt b
linear
14Reversible Container
Container
list
Forward Container
vector
Reversible Container
deque
- Additional Expressions Complexity
- Beginning of reverse range a.rbegin()
constant - End of reverse range a.rend() constant
15Random Access Container
Container
Forward Container
vector
Reversible Container
deque
Random Access Container
- Additional Expressions Complexity
- Element access an constant
16Sequence Containers
A linear range of elements NO special rule on
ordering elements. Can add or delete an element
at any point in that range.
S.insert(p,x)
p iterator pointing to an element
S.erase(p)
x a new element
17Sequence Containers
Container
vector
deque
Forward Container
list
slist
Sequence
- Additional Expressions Complexity
- Fill constructor X(n,t) linear
- Fill constructor X(n) linear
- Default constructor X()
linear - Range constructor X(i,j) linear
- Insert a.insert(p,t)
sequence-dependent - Fill insert a.insert(p,n,t) linear
- Range insert a.insert(p,i,j) linear
- Erase a.erase(p)
sequence-dependent - Range erase a.erase(p,q) linear
- Front a.front()
constant
18Front Insertion Sequence Containers
Container
slist
Forward Container
list
Sequence
deque
Front Insertion Sequence
- Additional Expressions Complexity
- Push front a.push_front(t)
constant - Pop front a.pop_front(t)
constant
19Back Insertion Sequence Containers
Container
list
Forward Container
vector
Sequence
deque
Back Insertion Sequence
- Additional Expressions Complexity
- Back a.back()
constant - Push back a.push_back(t)
constant - Pop back a.pop_back(t)
constant
20Two Questions to Ask About
Insertion and Deletion
What are the effects of insertion/deletion on a
sequences other elements and iterators?
What is the time complexity of insert and erase?
21Effects of Insertion/Deletion on Lists
- time complexity per node inserted /deleted is
constant - existing nodes are not moved
- iterators to existing nodes remain valid
22Effects of Insertion/Deletion on Vectors
- time complexity per node inserted/deleted is
linear in size - existing nodes may be moved
- iterators to existing nodes may become invalid
23Other Forms of Insert and Erase
Insert or erase an entire range
Examples
V.insert(V.begin(),L.begin(),L.end())
V(L.begin(), L.end())
Insert or erase at the front or back
front(), push_front(), pop_front()
Examples
back(), push_back(), pop_back()
24Insertion vs. Overwrite Semantics
25Associative Containers
Elements are always ordered according to some
rule, usually to provide faster lookup
An element is associated with a key of a type
defined by the container.
a. Different insert usage than Sequence (need not
specify location)
C.insert(x) //single element insertion
C.insert(first, last) //insert range of elements
b. Other member functions
C.find(key)
C.count(key)
26Refinements of Associative Container
Are the value_type and key the same?
Simple associative container yes, same instance
Pair associative container no, different
instances (and possibly types)
Can multiple elements have the same key?
Unique associative container no
Multiple associative container yes
How are elements organized by the container?
Sorted associative container
Hashed associative container (currently
non-standard)
27Associative Container Concepts
Container
refined by
models
Forward Container
Associative
Container
multiset
Pair Associative container
Simple Associative Container
set
multimap
map
Multiple Associative Container
hash_multiset
Unique Associative Container
hash_set
hash_multimap
Hashed Associative Container
Sorted Associative Container
hash_map
28Associative Container
hash_set
hash_multiset
multiset
multimap
set
map
hash_map
hash_multimap
- Additional Expressions Complexity
- Default constructor X () constant
- Default constructor X a constant
- Find a.find(k) logarithmic
- Count a.count(k)
O(log(size())count(k)) - Equal range a.equal_range(k))
logarithmic - Erase key a.erase(k)
O(log(size())count(k)) - Erase element a.erase(p) constant
- Erase range a.erase(p,q)
O(log(size())count(k))
29Unique Associative Container
hash_set
set
map
hash_map
- Additional Expressions Complexity
- Range constructor X a(i,j) linear
- Insert element a.insert(t)
logarithmic - Insert range a.insert(i,j)
O(Nlog(size()N))
30Multiple Associative Container
hash_multiset
multiset
multimap
hash_multimap
- Additional Expressions Complexity
- Range constructor X a(i,j) linear
- Insert element a.insert(t)
logarithmic - Insert range a.insert(i,j)
O(Nlog(size()N))
31Sorted Associative Container
multimap
map
multiset
set
- Additional Expressions Complexity
- Default constructors X () X a constant
- Default constructor with comparator X a(c)
constant - Range constructor X(i,j)
O(NlogN) - Range constructor w/ comparator X
a(i,j,c) O(NlogN) - Key comparison a.key_comp() constant
- Value comparison a.value_comp() constant
- Lower bound a.lower_bound(k)
logarithmic - Upper bound a.upper_bound(k)
logarithmic - Equal range a.equal_range(k)
logarithmic - Insert with hint a.insert(p,t)
logarithmic
32(Hashed Associative Container)
hash_set
hash_multiset
hash_map
hash_multimap
- Additional Expressions Complexity
- Default constructors X () X a
constant - Default constructor with bucket count X a(n)
constant - Default constructor with hash function X a(n,h)
constant - Default constructor with key equality X a(n,h,k)
constant - Range constructor X a(i,j) linear
- Range constructor with bucket count X a(i,j,n)
linear - Range constructor w/ hash fxn X a(i,j,n,h)
linear - Range constructor w/ key eq X a(i,j,n,h,k)
linear - Hash function a.hash_funct()
constant - Key equality a.key_eq()
constant - Bucket count a.bucket_count()
constant - Resize a.resize()
linear
33An Associated Concept Allocator
- Notation
- T,U Any types
- X Allocator whose
- value type is T
- Y Corresponding
- Allocator whose
- value type is U
- a,b Objects of type X
- Y Object of type Y
- T Object of type T
- p,q Objects of type
- Xpointer
- N Value of type
- Xsize_type
- Valid Expressions
- Default constructor X a
- Copy constructor X b(a)
- Generalized copy constructor X a(y)
- Comparison ab
- Allocate a.allocate(n)
- Allocate with hint a.allocate(n, q)
- Deallocate a.deallocate(p, n)
- Maximum size a.max_size()
- Construct a.construct(p, t)
- Destroy a.destroy(p)
- Address a.address(r)
34Questions for Discussion
- STL algorithms operate on ranges of elements.
Where are those elements stored? - What are some frequently used containers?
- What concepts do they model?
- What are the elements of a container concept?
- What are the key differences between fixed sized
containers and those with variable sizes?