Post on 19-Jun-2015
description
Oct 20, 2009
STL Containers
Dr. Partha Pratim DasInterra Systems (India) Pvt. Ltd.
Generalizing Data Structures
19-Oct-09 2
Agenda
• STL Components– Containers– Iterators– Algorithms
19-Oct-09 3
STL Components
Why & HowWhy & How
19-Oct-09 4
STL Components
• STL is a cooperation of Components
• Key Components– Containers– Iterators– Algorithms
19-Oct-09 5
Containers
• Manage collection of objects of a kind
• Every Container Type reflect a conglomeration of requirements
• May be implemented as – Arrays– Linked Lists– Special Keys for every Element
19-Oct-09 6
Iterators
• Used to step through elements of collections of objects
• Collections may be – Containers or
– Subsets of Containers
• Offers a small yet common interface for any arbitrary container type
• This works independent of the Internal Structure• Example: Iterator steps to the next element.
19-Oct-09 7
Algorithms
• Used to process elements of collections• Examples:
– Search– Sort– Modify– Use
• Algorithms use Iterators• Algorithms can call user-defined functions
(functors)
19-Oct-09 8
Concept of STL
• Separation of Data and Operations– Data Managed by Container Classes– Operations Defined by Configurable
Algorithms– Iterators Glue between these Components
• Contradicts OOP – through separation
19-Oct-09 9
Basic Model
• Algorithmssort, find, search, copy, …
• Containers vector, list, map, hash_map, …
99
iterators
• Separation of concerns– Algorithms manipulate
data, but don’t know about containers
– Containers store data, but don’t know about algorithms
– Algorithms and containers interact through iterators
• Each container has its own iterator types
19-Oct-09 10
Containers
Basic StuffBasic Stuff
19-Oct-09 11
Types of Containers
• Sequence Containers– Ordered Collection– Every element has a certain position– Position depends on time and place of Insertion– Position is independent of value– Predefined Sequence Containers are:
• vector
• deque
• list
19-Oct-09 12
Types of Containers
• Associative Containers– Sorted Collection– Position of an element depends on its value and
a certain sorting criterion– Order of Insertion is immaterial– Predefined Associative Containers are:
• set• multiset• map• multimap
19-Oct-09 13
Types of Containers
• Container Adaptors– Meets special needs– Implemented using fundamental containers– Predefined Container Adaptors are:
• stack
• queue
• Priority queue
19-Oct-09 14
Common Containers Abilities
• Constructor– Default: ContType c– Copy: ContType c1(c2)– Initialize with a Range: ContType c(beg, end)
• Destructor: ~ContType c
19-Oct-09 15
Common Containers Abilities
• Size Operations– c.size(): Actual number of elements– c.empty(): Whether the container is empty– c.max_size(): Maximum number of elements
possible
19-Oct-09 16
Common Containers Abilities
• Comparison Operators:– Operators: ==, !=, <, <=, >, >=– Both containers must have the same type– Two containers are equal if their elements are
equal and have the same order– To check if a container is less than another, a
lexicographic comparison is done
19-Oct-09 17
Common Containers Abilities
• Assignments and swap()– c1 = c2:
• Copy all elements of the source container and remove all old elements of the destination container
• Linear Complexity - expensive
– c1.swap(c2) / swap(c1, c2)• Use when containers have the same type and source
is no longer used• Swaps some internal pointers that refer to the data
(elements, allocator, sorting criterion)• Constant Complexity
19-Oct-09 18
Common Containers Abilities
• Iterator Functions– c.begin():
• Iterator for first element
– c.end(): • Iterator for last element
– c.rbegin(): • Reverse Iterator for first element
– c.rend(): • Reverse Iterator for last element
19-Oct-09 19
Common Containers Abilities
• Manipulators– c.insert(pos, elem):
• Insert a copy of elem at pos
– c.erase(beg, end): • Remove all elements in range [beg, end)
– c.clear(): • Remove all elements – makes the container empty
19-Oct-09 20
Sequence Container: vector
• Vector models a dynamic array
• #include <vector>
• Special Operations– Element Access
• c.at(idx) / c[idx]
• c.front() / c.end()
– Insert / Remove at the back• c.push_back(elem)
• c.pop_back()
19-Oct-09 21
Sequence Container: vector
• Remove the first element with a value
std::vector<Elem> coll;
std::vector<Elem>::iterator pos;
pos = find(coll.begin(), coll.end(), val);if (pos != coll.end()) {
coll.erase(pos);}
19-Oct-09 22
Sequence Container: deque (“deck”)
• Similar to Vector– Manages elements with Dynamic Array
– Provides Random Access
– Almost same interface as Vector
– Deque is open at both ends.
• #include <deque>• Special Operations
– Insert / Remove• c.push_back(elem) / c.pop_back()
• c.push_front(elem) / c.pop_front()
19-Oct-09 23
Sequence Container: list
• Manages elements in a doubly linked list
• #include <list>
• Element Access– front() / back()
• Insert / Remove– push_ / pop_ at both ends
• Splice / Sort / Merge / Reverse
19-Oct-09 24
Sequence Container: strings
• C++ string classes as containers– basic_string<>– string– wstring
19-Oct-09 25
Sequence Container: Ordinary Array
• Has static or dynamic size
• Not STL Containers– No size()– No empty()
19-Oct-09 26
Associative Container: set / multiset
• Sorts the elements according to a sorting criterion
• Sets do not allow duplicates
• Multisets allow duplicates
• #include <set>
19-Oct-09 27
Associative Container: map / multimap
• Manage key/value pairs as elements
• Sorts the elements according to a sorting criterion that is used for the actual key
• Maps do not allow duplicates
• Multimaps allow duplicates
• #include <map>
19-Oct-09 28
Container Adapter: stack
• #include <stack>
• Core Operations– push()– top()– pop()
19-Oct-09 29
Container Adapter: queue
• #include <queue>
• Core Operations– push()– front()– pop()– back()
19-Oct-09 30
Container Adapter: priority queue
• priority_queue implements a queue from which elements are read according to their priority
• #include <queue>
• Core Operations– push()– top()– pop()
19-Oct-09 31
Iterators
Basic StuffBasic Stuff
19-Oct-09 32
Iterator Interface
• Can iterate (navigate) over elements• All or part of the elements in an STL
container• Fundamental Operations
– operator*– operator++– operator==– operator!=– operator=
19-Oct-09 33
Container Interface for Iterators
• Fundamental Container Operations for Iterators– begin()– end()
19-Oct-09 34
Basic Iterator Model• A pair of iterators define a sequence
– The beginning (points to the first element – if any)– The end (points to the one-beyond-the-last element)
3434
…
begin: end:
• An iterator is a type that supports the “iterator operations”• ++ Go to next element
• * Get value
• == Does this iterator point to the same element as that iterator?
• Some iterators support more operations (e.g. --, +, and [ ])
19-Oct-09 35
Use of Iterators std::list<char> coll;
std::list<char>::const_iterator pos;
for(pos = coll.begin(); pos != coll.end(); ++pos)
{
cout << *pos << ‘ ‘;
}
19-Oct-09 36
Iterator Categories
• Bidirectional Iterators
• Random Access Iterators
19-Oct-09 37
Iterator Adaptors
• Insert Iterators
• Stream Iterators
• Reverse Iterators
19-Oct-09 38
Algorithms
Basic StuffBasic Stuff
19-Oct-09 39
Algorithm Overview
• STL provides several standard algorithms• Algorithms are not member functions of
containers• Algorithms are global functions that operate with
iterators• Algorithms can operate on various types• Algorithms do not belong to Object Oriented
Paradigm, they belong to Generic Programming Paradigm
• #include <algorithm>
19-Oct-09 40
References
• The C++ Standard Library: A Tutorial and Reference
– Nicolai M. Josuttis
• Effective STL– Scott Meyers
• C++ Template Metaprogramming– Abrahams & Gurtovoy
• C++ Templates: The Complete Guide – David Vandevoorde & Nicolai M. Josuttis
19-Oct-09 41
Thank You