Standard Template Library
la Standard Template Library (STL) è una Libreria software. Fa parte della Standard Library del linguaggio C++ e definisce Strutture dati generiche, Iteratori e Algoritmi generici.
Descrizione
La STL costituisce per i programmatori C++ una grossa comodità: dà ai programmatori un set precostituito di classi comuni, come containers e array associativi, che possono essere utilizzati con qualsiasi tipo, sia primitivo che definito dall'utente, con il supporto ad alcune operazioni elementari come copia ed assegnamento.
La STL raggiunge questo risultato attraverso il massiccio utilizzo dei templates. Questo tipo di approccio è molto potente, ma il codice generato risulta molto complicato, tale da costituire talvolta un problema per molti compilatori, ai quali può succedere di fallire la compilazione di costrutti validi, di produrre codice non valido, o richiedere al programmatore sforzi ulteriori per ottenere il risultato voluto.
La C++ Standard Library è definita dallo standard ISO/IEC 14882.
La Standard Template Library è stata la prima libreria a contenere algoritmi e strutture dati generici, seguendo quattro concetti base: programmazione generica, astrattezza senza perdita di efficienza, il modello computazionale di Von Neumann, and value semantics.
Storia
L'architettura della STL è stata creata in buona parte da Alexander Stepanov. Nel 1979 cominciò ad implementare le sue idee iniziali sulla Programmazione generica, esplorando le sue potenzialità, rivoluzionarie nel campo dello sviluppo del software. Anche se Dave Musser aveva già sviluppato precedentemente alcuni aspetti della programmazione generica nel 1971, i suoi contributi furnono limitati ad una area molto specializzata dello sviluppo software, la (computer algebra).
Stepanov riconobbe il pieno potenziale della programmazione generica e persuase i suoi allora colleghi della General Electric Research and Development (tra i quali, principalmente, Dave Musser e Deepak Kapur) che la programmazione generica should be pursued as a comprehensive basis for software development. A quei tempi non esisteva un supporto reale alla programmazione generica in nessun linguaggio di programmazione.
Il primo linguaggio di una certa importanza a dare tale supporto fu il Linguaggio di programmazione Ada, con le sue generic units. Dal 1987 Stepanov e Musser svilupparono e distribuirono una libreria Ada per il processamento di liste che racchiudeva i risultati di buona parte delle loro ricerche sulla programmazione generica. Comunque, l'Ada non ha mai avuto molta diffusione al di fuoari dell'industria della difesa e il C++ sembrava avere migliori possibilità di diffusione e di provvedere un buon supporto alla programmazione generica anche se il linguaggio era ancora relativamente immaturo (ancora non supportava i templates, aggiunti solo in un secondo momento). Another reason for turning to C++, which Stepanov recognized early on, was that 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++. Musser collaborated in this research and in 1992 Meng Lee joined Stepanov's project at HP and became a major contributor.
This work undoubtedly would have continued for some time as 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. 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. 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. 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 an interview Alexander Stepanov gave in the March 1995 issue of Dr. Dobb's Journal.)
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 have 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. This implementation, developed by Stepanov, Lee, and Musser during the standardization process, became the basis of all implementations offered by compiler and library vendors today.
Contents
Containers
The STL contains sequence containers and associative containers. The standard sequence containers include vector, string and deque. The standard associative containers are set, multiset, map and multimap.
vector - is a C-like array (i.e. capable of random access) with the ability to automatically resize itself when inserting or erasing an object. Inserting and removing an element to/from back of the vector at the end takes constant time. Inserting and erasing at the beginning or in the middle is linear in time.
deque (double ended queue) - a vector with insertion/erase at the beginning in amortized constant time, however lacking some guarantees on iterator validity after altering the deque.
set - inserting/erasing elements in a set does not invalidate iterators pointing in the set. Provides set operations union, intersection, difference, symmetric difference and test of inclusion.
Libraries implementing STL often include hashed variants: hash_set, hash_multiset, hash_map and hash_multimap, however this extension is not part of standard and are defined in various namespaces among implementations as a result.
Iterators
The STL implements five different types of iterators. These are input iterators, output iterators, forward iterators, bidirectional iterators and random access iterators.
Functors
The STL includes classes that overload the function operator (operator()). Classes that do this are called functor classes or function classes. They are useful for keeping and retrieving state information in functions passed into other functions.