词条 | Standard Template Library | ||||||||||||||||||||||||||||||||||||||||||
释义 |
The Standard Template Library (STL) is a software library for the C++ programming language that influenced many parts of the C++ Standard Library. It provides four components called algorithms, containers, functions, and iterators.[1] The STL provides a 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). STL algorithms are independent of containers, which significantly reduces the complexity of the library. The STL achieves its results through the use of templates. This approach provides compile-time polymorphism that is often more efficient than traditional run-time polymorphism. Modern C++ compilers are tuned to minimize abstraction penalties arising from heavy use of the STL. 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,[2] and value semantics. History{{main|Standard Template Library History}}In November 1993 Alexander Stepanov presented a library based on generic programming to the ANSI/ISO committee for C++ standardization. The committee's response was overwhelmingly favorable and led to a request from Andrew Koenig for a formal proposal in time for the March 1994 meeting. The committee had several requests for changes and extensions and the committee members met with Stepanov and Meng 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 David Musser. A proposal received final approval at the July 1994 ANSI/ISO committee meeting. Subsequently, the Stepanov and Lee document 17 was incorporated into the ANSI/ISO C++ draft standard (1, parts of clauses 17 through 27). 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. 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. CompositionContainersThe STL contains sequence containers and associative containers. The containers are objects that store data. The standard sequence containers include {{Cpp|vector}}, {{Cpp|deque}}, and {{Cpp|list}}. The standard associative containers are {{Cpp|set}}, {{Cpp|multiset}}, {{Cpp|map}}, {{Cpp|multimap}}, {{Cpp|hash_set}}, {{Cpp|hash_map}}, {{Cpp|hash_multiset}} and {{Cpp|hash_multimap}}. There are also container adaptors {{Cpp|queue}}, {{Cpp|priority_queue}}, and {{Cpp|stack}}, that are containers with specific interface, using other containers as implementation.
IteratorsThe STL implements five different types of iterators. These are input iterators (that can only be used to read a sequence of values), output iterators (that can only be used to write a sequence of values), forward iterators (that can be read, written to, and move forward), bidirectional iterators (that are like forward iterators, but can also move backwards) and {{visible anchor|random access iterator}}s (that can move freely any number of steps in one operation). A fundamental STL concept is a range which is a pair of iterators that designate the beginning and end of the computation, and most of the library’s algorithmic templates that operate on data structures have interfaces that use ranges [5]. It is possible to have bidirectional iterators act like random access iterators, so 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 that 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 that implements one of the five standard iterator interfaces, and all the algorithms provided in the STL can be used on the container. 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. This is because an associative container's methods can take advantage of knowledge of the internal structure, which is opaque to algorithms using iterators. AlgorithmsA large number of algorithms to perform activities 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 that provides an interface by iterators). Searching algorithms like {{Cpp|binary_search}} and {{Cpp|lower_bound}} use binary search and like sorting algorithms require that the type of data must implement comparison operator {{Cpp|<}} or custom comparator function must be specified; such comparison operator or comparator function must guarantee strict weak ordering. Apart from these, algorithms are provided for making heap from a range of elements, generating lexicographically ordered permutations of a range of elements, merge sorted ranges and perform union, intersection, difference of sorted ranges. FunctionsThe STL includes classes that overload the function call operator ({{Cpp|operator()}}). Instances of such classes are called functors or function objects. Functors allow the behavior of the associated function to be parameterized (e.g. through arguments passed to the functor's constructor) and can be used to keep associated per-functor state information along with the function. Since both functors and function pointers can be invoked using the syntax of a function call, they are interchangeable as arguments to templates when the corresponding parameter only appears in function call contexts. A particularly common type of functor is the predicate. For example, algorithms like {{Cpp|find_if}} take a unary predicate that operates on the elements of a sequence. Algorithms like sort, partial_sort, nth_element and all sorted containers use a binary predicate that must provide a strict weak ordering, that is, it must behave like a membership test on a transitive, non reflexive and asymmetric binary relation. If none is supplied, these algorithms and containers use less by default, which in turn calls the less-than-operator <. Criticisms{{Criticism section|date=June 2011}}Quality of implementation of C++ compilersThe Quality of Implementation (QoI) of the C++ compiler has a large impact on usability of STL (and templated code in general):
Other issues
Implementations
See also
Notes1. ^{{cite book|last=Holzner|first=Steven|title=C++ : Black Book|year=2001|publisher=Coriolis Group|location=Scottsdale, Ariz.|isbn=1-57610-777-9|page=648|quote=The STL is made up of containers, iterators, function objects, and algorithms}} 2. ^{{Cite book | last = Musser | first = David | title = STL tutorial and reference guide: C++ programming with the standard template library | publisher = Addison Wesley | year = 2001 | isbn = 0-201-37923-6 }} 3. ^{{cite book |first=Nicolai M. |last=Josuttis |publisher=Addison-Wesley Professional |year=1999 |title=The C++ Standard Library: A Tutorial and Handbook |page=547}} 4. ^{{cite book |last1=Vandevoorde |first1=David |last2=Josuttis |first2=Nicolai M. |title=C++ Templates: The Complete Guide |publisher=Addison Wesley |year=2002 |isbn=0-201-73484-2}} 5. ^{{cite web |url=http://stepanovpapers.com/STL/DOC.PDF |title=The Standard Template Library |first1=Alexander | last1=Stepanov | first2=Meng | last2=Lee|date=31 October 1995|access-date=16 December 2018|quote="Most of the library’s algorithmic templates that operate on data structures have interfaces that use ranges. A range is a pair of iterators that designate the beginning and end of the computation. [...] in general, a range [i, j) refers to the elements in the data structure starting with the one pointed to by i and up to but not including the one pointed to by j. Range [i, j) is valid if and only if j is reachable from i."}} 6. ^{{Cite web|url=http://gameangst.com/?p=226|title=Minimizing Code Bloat: Template Overspecialization|author=Adrian Stone}} 7. ^{{Cite book | last = Meyers | first = Scott | title = Effective C++ Third Edition – 55 Specific Ways to Improve Your Designs | publisher = Addison Wesley | year = 2005 | isbn = 0-321-33487-6 }} 8. ^{{Cite book | first1 = Herb | last1 = Sutter | first2 = Andrei | last2 = Alexandrescu | authorlink1 = Herb Sutter | authorlink2 = Andrei Alexandrescu | year = 2004 | title = C++ Coding Standards: 101 Rules, Guidelines, and Best Practices | publisher = Addison-Wesley | isbn = 0-321-11358-6 }} 9. ^{{cite web | author = Andrei Alexandrescu | title = Iterators Must Go | url = https://github.com/boostcon/2009_presentations/raw/master/wed/iterators-must-go.pdf | date = 6 May 2009 | accessdate =19 March 2011 | publisher = BoostCon 2009 }} 10. ^{{Cite journal|author=Matthew Wilson |title=Callback Enumeration APIs & the Input Iterator Concept |date=February 2004 |journal=Dr. Dobb's Journal |url=http://www.ddj.com/cpp/184401766 |authorlink=Matthew Wilson (author)}} 11. ^{{Cite book|author=Bjarne Stroustrup |title=The C++ Programming Language |edition=3rd |isbn=0-201-70073-5 |publisher=Addison-Wesley |year=2000}}{{rp|p.530}} 12. ^More STL algorithms (revision 2) 13. ^{{cite web |url=https://gcc.gnu.org/libstdc++/ |title=libstdc++ Homepage}} 14. ^Apache C++ Standard Library. Stdcxx.apache.org. Retrieved on 2013-07-29. References{{refbegin}}
External links
2 : C++ Standard Library|Generic programming |
||||||||||||||||||||||||||||||||||||||||||
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。