280 likes | 309 Views
Learn the history, templates, and implementations of the C++ Standard Template Library (STL) with examples and explanations. Understand key concepts like namespaces, containers, iterators, and algorithms.
E N D
STLStandard Template Library • Good reference book: • The C++ Standard Library • A Tutorial and Reference • by Nicolai M. Josuttis • 1999 – Addison Wesley Longman, Inc. • ISBN: 0201379260 • My presentation will come from both books plus a small amount from other references. • www.cs.rpi.edu/projects/STL/stl/stl.html
History • 1989 – Start of standardization of C++ • 1997 – Finished, published Sept. 1998 • 750 pages, published by the International Standards Organization (ISO). • Title: “Information Technology – Programming Languages – C++” • “..it defines the exact contents and behavior of C++, ...” • Part of the standard is a standard library.
History: C++ Standard Library • core components for • I/O • strings, • containers (data structures) • algorithms (such as sort, search and merge) • support for numeric computation • support for internationalization (such as different character sets)
History: STL added • 1994 – The STL was added to the C++ standard library. • STL defines powerful, template-based, reusable components that implement many common data structures and algorithms used to process those data structures. • Makes heavy use of both function and class templates.
Templates • Templates are functions or classes that are written for one or more types not yet specified. • When you use a template, you pass the types as arguments, explicitly or implicitly.
Template Example • template <class T> • inline const T& my_max (const T& a, const T& b) • { • if(a < b) return b; • else return a; • }
exmaple : details • First line defines T as an arbitrary data type that is specified by the caller when the caller calls the function. The type is classified by class, although it does not have to be a class. You can use any data type as long as it provides the operations that the template uses. • inline is a hint to the compiler to put this function in-line since it is so short.
example: details • const before the type reference allows the function to use constant reference parameters. • T& is the type of the function. (A reference to the type T) • my_max is the name of the function being defined as a template. • const T& a (and const T& b) define two reference parameters whose value cannot be changed.
example: details • The code for the function itself is simply to return the maximum of the two parameter values as the value of the function. • Instantiated by its use (since it is a function and not a class). • Use: • int a(10), b(12); • cout << my_max(a,b) << endl; • Output: 12
MyArray class template • template <class T> • class MyArray { • public: • MyArray(int = 10); • ~MyArray( ); • void FillArray(T=2); • void PrintArray( ); • private: • T* ArrayPtr; • int size; • };
MyArray constructor • template < class T> • MyArray<T>::MyArray(int s) • { • if(s < 0) s = 10; • size = s; • ArrayPtr = new T[s]; • }
MyArray destructor • template < class T > • MyArray<T>::~MyArray( ) • { • delete [ ] ArrayPtr; • }
MyArray FillArray function • template < class T > • void MyArray<T>::FillArray(T value) • { • for(int i=0; i<size; i++) • ArrayPtr[i]=value; • }
MyArray PrintArray function • template < class T > • void MyArray<T>::PrintArray( ) • { • for(int i = 0; i<size; i++) • cout << ArrayPtr[i] << “ “; • cout << endl; • }
test MyArray • #include “MyArray.h” • int main (void) • { • MyArray<int> fred(3); • MyArray<double> suzie(5); • MyArray<int> sam; • fred.FillArray(-2); fred.PrintArray(); • suzie.FillArray(3.25); suzie.PrintArray(); • sam.FillArray(); sam.PrintArray(); • }
test output • -2 -2 -2 • 3.25 3.25 3.25 3.25 3.25 • 2 2 2 2 2 2 2 2 2 2
STL General Concepts • Namespace std • (covered before) • std::cout << std::hex << 127 << std::endl; • or • using std::cout; • using std::endl; • cout << std::hex << 127 << endl; • or • using namespace std; • cout << hex << 127 << endl;
STL General Concepts cont. • Header files • In addition to the standard ones: • #include <iostream> • There is normally a special include for each use of a STL template. • #include <string> • #include <vector> • #include <stack> • (more specifics as we describe STL components)
STLComponents • Containers • Manage collections of objects of a certain kind. • Iterators • Used to step through the elements of collections of objects. • Small but common interface for an arbitrary container type. • Algorithms • Used to process elements of collections
iterator container iterator algorithm container iterator container
STLSequence Containers • Ordered Collections. • Every element has a certain position. • Position independent of the value of the element. • Position depends on order and place of insertion. • Vector • Deque • List
STLSequence Containers Vector Deque List
STLAssociative Containers • Sorted Collection. • Actual position of an element depends on its value due to certain sorting criterion. • Order of insertion doesn't matter. • Set • Multiset • Map • Multimap
STL Associative Containers Set/Multiset Map/Multimap
STL Iterators • An object that can “iterate” over elements. May be all or part of a STL container. • An iterator represents a certain position in a container. • Operator * • Returns the element of the actual position. • Operator ++ • Lets the iterator step forward to the next element. • Most iterators also allow stepping backwards by using operator - -
(cont.) • Operator == and != • Returns whether two iterators represent the same position. • Operator = • Assigns an iterator (the position of the element to which it refers)
Iterator related member functions • begin( ) • returns an interator that represents the beginning of the elements in the container. • end( ) • returns an iterator that represents the end of the elements in the container. • The end position is behind the last element.
end ( ) begin ( )