Transcript STL

STL
CSSE 250
Susan Reeder
What is the STL?
Standard Template Library
Standard C++ Library is an extensible
framework which contains components for







Language support
Diagnostics
General utilities
Strings
Locales
Standard template library (containers, iterators,
algorithms, numerics)
Input/output
Origins of STL
With the popularity of C/C++, many
companies found profit in providing
libraries of routines designed to handle the
storage and processing of data.
ANSI/ISO added a standard for these
libraries – the STL
The STL brings a mature set of generic
containers and algorithms to the C++
language that didn’t exist before.
What do you need to know?
List of what is available (published in many
sources)
How to use it



Understand generic programming
Understand templates
Cross-platform Standard Templates
Three “parts” of STL
Conceptually, the STL encompasses three
separate algorithmic problem solvers.



Containers
Algorithms
Iterators
Containers
A way that stored data is organized in
memory

Array, Stack, Queue, Linked list, Binary tree
Implemented by template classes to be
customizable
All have common management member
functions

Insert(), erase(), begin(), etc.
Also have individual member functions
Algorithms
Behaviors or functionality applied to
containers to process their contents in
various ways.

Sort, copy, search, merge
Template functions – not part of a class,
but a stand-alone function
Can be used on containers other than STL
(e.g. common arrays)
Iterators
Once you have a container and an
algorithm, an iterator will allow the two to
interact.
Iterator is a generalized pointer that points
to elements within a container.
Iterators are a key part of the STL because
they connect algorithms with containers.
Iterators, cont.
Iterators are objects that point to other objects.
As objects, they have data members and
member functions.
Iterators are central to generic programming
because they are an interface between
containers and algorithms; algorithms typically
take iterators as arguments, so a container need
only supply a way to access its elements using
iterators.
Iterator Concepts
Input iterator

Only guarantees read access
Output iterator

Only guarantees write access
Forward iterator



Supports input/output
Can be constant or mutable
Supports “forward” motion through the
container (increment)
Iterator Concepts, cont.
Bidirectional iterator



Supports input/output
Can be constant or mutable
Supports increment or decrement
Random Access iterator


Supports input/output
Allows random movement through container
Iterator Concepts, cont.
Most algorithms are expressed not in
terms of a single iterator, but in terms of a
range of iterators (first, last).
A class needs to provide access for an
iterator – either by returning one or making
the iterator class a friend.
Latest Language Updates
Using namespace
Casting changes
RTTI (Run-Time Type Information)
Namespace
Namespaces control scope or identifier
visibility
Tightest scope is local – those identifiers
declared within a function
Next up would be class scope
Highest level is program or workspace
scope
Defining namespaces
Encapsulate your declarations within a
namespace block
namespace your_namespace_name{
int ivalue;
class my_class {….};
// more declarations;
}
How to use
Any code statements within
your_namespace_name have direct access to
the namespace’s declarations
Code statements outside of
your_namespace_name must use qualifying
syntax
int main()
{ your_namespace_name::ivalue++;
….
}
How to use
Using statement removes the need to qualify,
but allows global access to everything in the
listed namespace.
using namespace your_namespace_name;
the Selective using statement is somewhere
between fully qualified and global access
using your_namespace_name::ivalue;
each allows access to ivalue without another
qualifying syntax
Renaming
Long namespace names help with scope,
but can be unwieldy.
namespace YNN =
your_namespace_name;
Can also have unnamed namespaces


compiler will internally provide name, but the
identifiers are only available within the
defining file
simply leave out the name in the definition
Casting
Dynamic casting


used to convert a base class pointer or
reference to a derived class pointer or
reference – checked at run time
dynamic_cast <castType>(objectToCast);
Static casting


used to convert between types that are not in
the same class hierarchy – checked at
compile time
static_cast<castType>(objectToCast);
RTTI
Run-Time Type Information
must include <typeinfo> library
use the typeid operator to get the type of
any object
can be used for debugging or simply
verifying the objects in use at any time as
a useful piece of run-time logic control
Standard C++ Library
C++ Language Support



the basic types
dynamic memory allocation
exception processing
Diagnostic Tools

assertions & error codes
General C/C++ Utilities

components used by other elements
Standard C++ Library
Strings

components for manipulating sequences of
characters – type char, w_char (UNICODE) or
a type defined in a program
Cultural Formatting Support

numeric, monetary and date/time formatting
and parsing
STL (Standard Template Library)

containers, algorithms, iterators
Standard C++ Library
Advanced Numerical Computation

seminumerical operations and components
for complex number types, numeric arrays,
and generalized numeric algorithms
Input/Output

components for iostreams
Standard C Library

incorporates the Standard C Library
Standard C++ Library Headers
algorithm
bitset
cassert
cctype
cerrno
cfloat
ciso646
climits
clocale
cmath
complex
csetjmp
csignal
cstdarg
cstddef
cstdio
cstdlib
cstring
Standard C++ Library Headers
ctime
cwchar
cwctype
deque
exception
fstream
functional
iomanip
ios
iosfwd
iostream
istream
iterator
limits
list
locale
map
memory
Standard C++ Library Headers
new
numeric
ostream
queue
set
sstream
stack
stdexcept
streambuf
string
strstream ??
typeinfo
utility
valarray
vector
Standard Template Library
Containers, algorithms, iterators
Individual STL libraries and “glue”
components are designed to work together
in useful ways to produce the kind of
larger and more specialized algorithms
needed in today’s applications
Rules for using STL
Create objects to use with STL containers
Make sure objects include




A copy constructor
An assignment operator, =
An equality comparison operator, ==
A less than comparison operator, <
Or can use existing types
An example
vector<int> v(3); // declares vector size 3
v[0] = 7;
v[1] = v[0] + 3;
v[2] = v[0] + v[1];

// v[0] == 7, v[1] == 10, v[2] = 17
reverse(v.begin(), v.end());

// v[0] == 17, v[1] == 10, v[2] = 7
Final word
Possible to write code and never need to
actually create own data structures
Most languages do not provide these
types of libraries
Sometimes using the STL bloats your
program and makes it inefficient
Good to know how to write your own, then
there are options