The Standard Template Library (STL) is a software library partially included in the C++ Standard Library. In Computer science, a library is a collection of Subroutines used to develop Software. C++ (" C Plus Plus " ˌsiːˌplʌsˈplʌs is a general-purpose Programming language. In C++, the Standard Library is a collection of classes and functions, which are written in the Core language. It provides containers, iterators, algorithms, and functors. In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects In Computer science, an iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific In Mathematics, Computing, Linguistics and related subjects an algorithm is a sequence of finite instructions often used for Calculation A function object, also called a functor or functional, is a Computer programming construct allowing an object to be invoked or called as if More specifically, the C++ Standard Library is based on the STL published by SGI. Silicon Graphics Inc (commonly initialised to SGI, historically sometimes referred to as Silicon Graphics Computer Systems or SGCS) is a company Both include some features not found in the other. SGI's STL is rigidly specified as a set of headers, while ISO C++ does not specify header content, and allows implementation either in the headers, or in a true library. In Computer programming, particularly in the C and C++ programming languages a header file or include file is a file, usually in
Contents |
The STL provides a ready-made set of common classes for C++, such as containers and associative arrays, that can be used with any built-in type and with any user-defined type that supports some elementary operations (such as copying and assignment). C++ (" C Plus Plus " ˌsiːˌplʌsˈplʌs is a general-purpose Programming language. In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects An associative array (also associative container, map, mapping, hash, dictionary, finite map, and in query-processing an STL algorithms are independent of containers, which significantly reduces the complexity of the library. In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects
The STL achieves its results through the use of templates. Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This approach provides compile-time polymorphism that is often more efficient than traditional run-time polymorphism. In Computer science, polymorphism is a Programming language feature that allows values of different Data types to be handled using a In simple terms polymorphism is the ability of one type A to appear as and be used like another type B Modern C++ compilers are tuned to minimize any abstraction penalty arising from heavy use of the STL. C++ (" C Plus Plus " ˌsiːˌplʌsˈplʌs is a general-purpose Programming language. A compiler is a Computer program (or set of programs that translates text written in a computer language (the source language) into another
The STL was created as the first library of generic algorithms and data structures for C++, with four ideas in mind: generic programming, abstractness without loss of efficiency, the Von Neumann computation model, and value semantics. C++ (" C Plus Plus " ˌsiːˌplʌsˈplʌs is a general-purpose Programming language. Generic programming is a style of Computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated In Computer science, abstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time The von Neumann architecture is a design model for a stored-program Digital computer that uses a processing unit and a single separate storage structure
The STL contains sequence containers and associative containers. In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects The standard sequence containers include vector, deque and list. In Computer science, a list is an ordered collection of entities / Items In the context of Object-oriented programming languages The standard associative containers are set, multiset, map and multimap. An associative array (also associative container, map, mapping, hash, dictionary, finite map, and in query-processing an
| Container | Description |
|---|---|
| Sequences (Arrays / Linked Lists) - ordered collections | |
| vector | a dynamic array, like C array (i. In Computer science, a list is an ordered collection of entities / Items In the context of Object-oriented programming languages In Computer science an array is a Data structure consisting of a group of elements that are accessed by indexing. In Computer science, a linked list is one of the fundamental Data structures and can be used to implement other data structures Vectors (or stdvector) are a C++ implementation of the Dynamic array Data structure. In Computer science, a dynamic array, growable array, resizable array, dynamic table, or array list is an Array tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured In Computer science an array is a Data structure consisting of a group of elements that are accessed by indexing. e. , capable of random access) with the ability to automatically resize itself when inserting or erasing an object. In Computer science, random access (sometimes called direct access) is the ability to access an arbitrary element of a sequence in equal time Inserting and removing an element to/from back of the vector at the end takes amortized constant time. In Computer science, especially Analysis of algorithms, amortized analysis refers to finding the average running time per operation over a Worst-case Inserting and erasing at the beginning or in the middle is linear in time. A specialization for type bool exists, which optimizes for space by storing bool values as bits. |
| list | a doubly-linked list; elements are not stored in contiguous memory. In Computer science, a linked list is one of the fundamental Data structures and can be used to implement other data structures In Computer science, a linked list is one of the fundamental Data structures and can be used to implement other data structures Opposite performance from a vector. Slow lookup and access (linear time), but once a position has been found, quick insertion and deletion (constant time). |
| deque (double ended queue) | a vector with insertion/erase at the beginning or end in amortized constant time, however lacking some guarantees on iterator validity after altering the deque. In Computer science theory a deque (short for double-ended queue &mdash Deque is usually pronounced deck A queue (pronounced /kjuː/ is a particular kind of collection in which the entities in the collection are kept in order and the principal (or only operations on the collection |
| Associative containers - unordered collections | |
| set | a sorted set; inserting/erasing elements in a set does not invalidate iterators pointing in the set. An associative array (also associative container, map, mapping, hash, dictionary, finite map, and in query-processing an In Computer science, a set is a collection ( container) of certain values without any particular order, and no repeated values Provides set operations union, intersection, difference, symmetric difference and test of inclusion. In Set theory, the term Union (denoted as ∪ refers to a set operation used in the convergence of set elements to form a resultant set containing the elements of both sets In Mathematics, the intersection of two sets A and B is the set that contains all elements of A that also belong to B (or equivalently In Discrete mathematics and predominantly in Set theory, a complement is a concept used in comparisons of sets to refer to the unique values of one set in relation In Mathematics, the symmetric difference of two sets is the set of elements which are in one of the sets but not in both Type of data must implement comparison operator < or custom comparator function must be specified. Implemented using a self-balancing binary search tree. In Computer science, a self-balancing binary search tree or height-balanced binary search tree is a Binary search tree that attempts to keep its height |
| multiset | same as a set, but allows duplicate elements. |
| map | a sorted associative array; allows mapping from one data item (a key) to another (a value). The class std map is a standard C++ container. It is a sorted Associative array, which is a data structure An associative array (also associative container, map, mapping, hash, dictionary, finite map, and in query-processing an Type of key must implement comparison operator < or custom comparator function must be specified. Implemented using a self-balancing binary search tree. |
| multimap | same as a map, but allows duplicate keys. |
| hash_set hash_multiset | similar to a set, multiset, map, or multimap, respectively, but implemented using a hash table; keys are not sorted, but a hash function must exist for key type. In Computer science, a hash table, or a hash map, is a Data structure that associates keys with values. A hash function is any well-defined procedure or mathematical function for turning some kind of Data into a relatively small integer, that may These containers are not part of the C++ Standard Library, but are included in SGI's STL extensions, and are included in common libraries such as the GNU C++ Library in the __gnu_cxx namespace. In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects These are scheduled to be added to the C++ standard as part of TR1, with the slightly different names of unordered_set, unordered_multiset, unordered_map and unordered_multimap. |
| Other types of containers | |
| bitset | stores series of bits similar to a fixed-sized vector of bools. Also optimizes for space |
| valarray | another C-like array like vector, but is designed for high speed numerics at the expense of some programming ease and general purpose use. In Computer science an array is a Data structure consisting of a group of elements that are accessed by indexing. It has many features that make it ideally suited for use with vector processors in traditional vector supercomputers and SIMD units in consumer-level scalar processors, and also ease vector mathematics programming even in scalar computers. |
The STL implements five different types of iterators. In Computer science, an iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific These are input iterators (which can only be used to read a sequence of values), output iterators (which can only be used to write a sequence of values), forward iterators (which can be read, written to, and move forward), bidirectional iterators (which are like forward iterators but can also move backwards) and random access iterators (which can move freely any number of steps in one operation).
It is possible to have bidirectional iterators act like random access iterators, as moving forward ten steps could be done by simply moving forward a step at a time a total of ten times. However, having distinct random access iterators offers efficiency advantages. For example, a vector would have a random access iterator, but a list only a bidirectional iterator.
Iterators are the major feature which allow the generality of the STL. For example, an algorithm to reverse a sequence can be implemented using bidirectional iterators, and then the same implementation can be used on lists, vectors and deques. User-created containers only have to provide an iterator which implements one of the 5 standard iterator interfaces, and all the algorithms provided in the STL can be used on the container. In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects
This generality also comes at a price at times. For example, performing a search on an associative container such as a map or set can be much slower using iterators than by calling member functions offered by the container itself. An associative array (also associative container, map, mapping, hash, dictionary, finite map, and in query-processing an In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects This is because an associative container's methods can take advantage of knowledge of the internal structure, which is opaque to algorithms using iterators. An associative array (also associative container, map, mapping, hash, dictionary, finite map, and in query-processing an In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects
A large number of algorithms to perform operations such as searching and sorting are provided in the STL, each implemented to require a certain level of iterator (and therefore will work on any container which provides an interface by iterators). In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects
The STL includes classes that overload the function operator (operator()). In Computer science, polymorphism is a Programming language feature that allows values of different Data types to be handled using a Classes that do this are called functors or function objects. A function object, also called a functor or functional, is a Computer programming construct allowing an object to be invoked or called as if They are useful for keeping and retrieving state information in functions passed into other functions. Regular function pointers can also be used as functors.
A particularly common type of functor is the predicate. In Mathematics, a predicate is either a relation or the Boolean-valued function that amounts to the Characteristic function or the For example, algorithms like find_if take a unary predicate that operates on the elements of a sequence. A unary function is a function that takes one argument. In Computer science, a Unary operator is a subset of unary function Algorithms like sort, partial_sort, nth_element and all sorted containers use a binary predicate which must provide a strict weak ordering, that is, it must behave like a membership test on a transitive, irreflexive and antisymmetric binary relation. In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects In Mathematics, a binary function, or function of two variables, is a function which takes two inputs In Mathematics, especially Order theory, a strict weak ordering is a Binary relation S that is a strict partial order In Mathematics, a binary relation (or a dyadic or 2-place relation) is an arbitrary association of elements within a set or with elements of If none is supplied, these algorithms and containers use less by default, which in turn calls the less-than-operator <. In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects
The Quality of Implementation (QoI) of the C++ compiler has a large impact on usability of STL (and templated code in general):
The architecture of STL is largely the creation of one person, Alexander Stepanov. This article is about the key person behind the C++ Standard Template Library. In 1979 he began working out his initial ideas of generic programming and exploring their potential for revolutionizing software development. Year 1979 ( MCMLXXIX) was a Common year starting on Monday (link displays the 1979 Gregorian calendar) Generic programming is a style of Computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated Although David Musser had developed and advocated some aspects of generic programming as early as 1971, it was limited to a rather specialized area of software development (computer algebra). David Musser is a professor of Computer science at the Rensselaer Polytechnic Institute in Troy New York, U Year 1971 ( MCMLXXI) was a Common year starting on Friday (link will display full calendar of the 1971 Gregorian calendar. Symbolic computation, algebraic computation, or less commonly symbolic manipulation, symbolic processing, symbolic mathematics, or symbolic
Stepanov recognized the full potential for generic programming and persuaded his then-colleagues at General Electric Research and Development (including, primarily, David Musser and Deepak Kapur) that generic programming should be pursued as a comprehensive basis for software development. David Musser is a professor of Computer science at the Rensselaer Polytechnic Institute in Troy New York, U At the time there was no real support in any programming language for generic programming.
The first major language to provide such support was Ada, with its generic units feature. Ada is a structured, Statically typed, imperative, and object-oriented high-level computer Programming language By 1987 Stepanov and Musser had developed and published an Ada library for list processing that embodied the results of much of their research on generic programming. Year 1987 ( MCMLXXXVII) was a Common year starting on Thursday (link displays 1987 Gregorian calendar) However, Ada had not achieved much acceptance outside the defense industry and C++ seemed more likely to become widely used and provide good support for generic programming even though the language was relatively immature. The defense industry, also called the military industry, is comprised of Government and commercial Industry involved in research development Another reason for turning to C++, which Stepanov recognized early on, was the C/C++ model of computation which allows very flexible access to storage via pointers is crucial to achieving generality without losing efficiency.
Much research and experimentation were needed, not just to develop individual components, but to develop an overall architecture for a component library based on generic programming. First at AT&T Bell Laboratories and later at Hewlett-Packard Research Labs, Stepanov experimented with many architectural and algorithm formulations, first in C and later in C++. Bell Laboratories (also known as Bell Labs and formerly known as AT&T Bell Laboratories and Bell Telephone Laboratories) is the Research organization tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured Musser collaborated in this research and in 1992 Meng Lee joined Stepanov's project at HP and became a major contributor. Year 1992 ( MCMXCII) was a Leap year starting on Wednesday (link will display full 1992 Gregorian calendar) Meng Lee is currently a Technical Contributor at Hewlett-Packard Research Labs
This work undoubtedly would have continued for some time being just a research project or at best would have resulted in an HP proprietary library if Andrew Koenig of Bell Labs had not become aware of the work and asked Stepanov to present the main ideas at a November 1993 meeting of the ANSI/ISO committee for C++ standardization. Andrew Koenig is a former AT&T researcher and programmer known for his work with C++. Bell Laboratories (also known as Bell Labs and formerly known as AT&T Bell Laboratories and Bell Telephone Laboratories) is the Research organization Year 1993 ( MCMXCIII) was a Common year starting on Friday (link will display full 1993 Gregorian calendar) The committee's response was overwhelmingly favorable and led to a request from Koenig for a formal proposal in time for the March 1994 meeting. Year 1994 ( MCMXCIV) was a Common year starting on Saturday (link will display full 1994 Gregorian calendar) Despite the tremendous time pressure, Alex and Meng were able to produce a draft proposal that received preliminary approval at that meeting.
The committee had several requests for changes and extensions (some of them major), and a small group of committee members met with Stepanov and Lee to help work out the details. The requirements for the most significant extension (associative containers) had to be shown to be consistent by fully implementing them, a task Stepanov delegated to Musser. An associative array (also associative container, map, mapping, hash, dictionary, finite map, and in query-processing an In Computer science, a container is a class, a Data structure, or an Abstract data type (ADT whose instances are collections of other objects It would have been quite easy for the whole enterprise to spin out of control at this point, but again Stepanov and Lee met the challenge and produced a proposal that received final approval at the July 1994 ANSI/ISO committee meeting. (Additional details of this history can be found in Stevens. ) Subsequently, the Stepanov and Lee document 17 was incorporated into the ANSI/ISO C++ draft standard (1, parts of clauses 17 through 27). It also influenced other parts of the C++ Standard Library, such as the string facilities, and some of the previously adopted standards in those areas were revised accordingly.
In spite of STL's success with the committee, there remained the question of how STL would make its way into actual availability and use. With the STL requirements part of the publicly available draft standard, compiler vendors and independent software library vendors could of course develop their own implementations and market them as separate products or as selling points for their other wares. One of the first edition's authors, Atul Saini, was among the first to recognize the commercial potential and began exploring it as a line of business for his company, Modena Software Incorporated, even before STL had been fully accepted by the committee.
The prospects for early widespread dissemination of STL were considerably improved with Hewlett-Packard's decision to make its implementation freely available on the Internet in August 1994. The Internet is a global system of interconnected Computer networks This implementation, developed by Stepanov, Lee, and Musser during the standardization process, became the basis of many implementations offered by compiler and library vendors today.