Presentation is loading. Please wait.

Presentation is loading. Please wait.

STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.

Similar presentations


Presentation on theme: "STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language."— Presentation transcript:

1 STL CSSE 250 Susan Reeder

2 What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language support Language support Diagnostics Diagnostics General utilities General utilities Strings Strings Locales Locales Standard template library (containers, iterators, algorithms, numerics) Standard template library (containers, iterators, algorithms, numerics) Input/output Input/output

3 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.

4 What do you need to know? List of what is available (published in many sources) How to use it Understand generic programming Understand generic programming Understand templates Understand templates Cross-platform Standard Templates Cross-platform Standard Templates

5 Three “parts” of STL Conceptually, the STL encompasses three separate algorithmic problem solvers. Containers Containers Algorithms Algorithms Iterators Iterators

6 Containers A way that stored data is organized in memory Array, Stack, Queue, Linked list, Binary tree Array, Stack, Queue, Linked list, Binary tree Implemented by template classes to be customizable All have common management member functions Insert(), erase(), begin(), etc. Insert(), erase(), begin(), etc. Also have individual member functions

7 Algorithms Behaviors or functionality applied to containers to process their contents in various ways. Sort, copy, search, merge 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)

8 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.

9 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.

10 Iterator Concepts Input iterator Only guarantees read access Only guarantees read access Output iterator Only guarantees write access Only guarantees write access Forward iterator Supports input/output Supports input/output Can be constant or mutable Can be constant or mutable Supports “forward” motion through the container (increment) Supports “forward” motion through the container (increment)

11 Iterator Concepts, cont. Bidirectional iterator Supports input/output Supports input/output Can be constant or mutable Can be constant or mutable Supports increment or decrement Supports increment or decrement Random Access iterator Supports input/output Supports input/output Allows random movement through container Allows random movement through container

12 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.

13 Latest Language Updates Using namespace Casting changes RTTI (Run-Time Type Information)

14 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

15 Defining namespaces Encapsulate your declarations within a namespace block namespace your_namespace_name{ int ivalue; int ivalue; class my_class {….}; class my_class {….}; // more declarations; // more declarations;}

16 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++; ….}

17 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

18 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 compiler will internally provide name, but the identifiers are only available within the defining file simply leave out the name in the definition simply leave out the name in the definition

19 Casting Dynamic casting used to convert a base class pointer or reference to a derived class pointer or reference – checked at run time used to convert a base class pointer or reference to a derived class pointer or reference – checked at run time dynamic_cast (objectToCast); dynamic_cast (objectToCast); Static casting used to convert between types that are not in the same class hierarchy – checked at compile time used to convert between types that are not in the same class hierarchy – checked at compile time static_cast (objectToCast); static_cast (objectToCast);

20 RTTI Run-Time Type Information must include 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

21 Standard C++ Library C++ Language Support the basic types the basic types dynamic memory allocation dynamic memory allocation exception processing exception processing Diagnostic Tools assertions & error codes assertions & error codes General C/C++ Utilities components used by other elements components used by other elements

22 Standard C++ Library Strings components for manipulating sequences of characters – type char, w_char (UNICODE) or a type defined in a program 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 numeric, monetary and date/time formatting and parsing STL (Standard Template Library) containers, algorithms, iterators containers, algorithms, iterators

23 Standard C++ Library Advanced Numerical Computation seminumerical operations and components for complex number types, numeric arrays, and generalized numeric algorithms seminumerical operations and components for complex number types, numeric arrays, and generalized numeric algorithmsInput/Output components for iostreams components for iostreams Standard C Library incorporates the Standard C Library incorporates the Standard C Library

24 Standard C++ Library Headers algorithmcmath bitsetcomplex cassertcsetjmp cctypecsignal cerrnocstdarg cfloatcstddef ciso646cstdio climitscstdlib clocalecstring

25 ctimeiosfwd cwchariostream cwctypeistream dequeiterator exceptionlimits fstreamlist functionallocale iomanipmap iosmemory

26 newstring numeric strstream ?? ostreamtypeinfo queueutility setvalarray sstreamvector stack stdexcept streambuf

27 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

28 Rules for using STL Create objects to use with STL containers Make sure objects include A copy constructor A copy constructor An assignment operator, = An assignment operator, = An equality comparison operator, == An equality comparison operator, == A less than comparison operator, < A less than comparison operator, < Or can use existing types

29 An example vector 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 // v[0] == 7, v[1] == 10, v[2] = 17 reverse(v.begin(), v.end()); // v[0] == 17, v[1] == 10, v[2] = 7 // v[0] == 17, v[1] == 10, v[2] = 7

30 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


Download ppt "STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language."

Similar presentations


Ads by Google