laLa '''Standard Template Library''' ('''STL)''') è una [[Librerialibreria software]]. Faper parteil dellalinguaggio Standarddi Library del linguaggioprogrammazione [[C++]] eche definisce [[Strutturequattro daticomponenti genericheprincipali: contenitori, [[iteratore|iteratori]], [[Iteratorialgoritmo|algoritmi]] e [[AlgoritmiFuntore generici(programmazione)|funtori]].
STL offre un insieme di [[classe (informatica)|classi]] C++, quali ad esempio i contenitori e gli [[Array associativo|array associativi]], che possono essere usati con qualunque [[tipo di dato]] – sia esso predefinito o costruito dall'utente – che supporti alcune istruzioni elementari (copia, assegnazione, ecc.). Gli algoritmi implementati in STL risultano indipendenti dai container, cosa che riduce significativamente la complessità della libreria.
==Descrizione==
La STL costituisce per i programmatori [[C Plus Plus|C++]] un grosso boon: 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.
STL è basata sui [[template]], un approccio che permette il [[Polimorfismo (informatica)|polimorfismo]] in fase di compilazione, nettamente più efficiente del polimorfismo in fase di esecuzione. STL fu la prima libreria di algoritmi e strutture dati generiche per il C++; si basa su quattro idee di fondo: programmazione generica, [[Astrazione (informatica)|astrazione]] senza perdita di efficienza, [[Architettura di von Neumann|modello di elaborazione]] di [[John von Neumann|Von Neumann]] e semantica dei valori.
La STL raggiunge questo risultato attraverso il massiccio utilizzo dei [[Programmazione generica|template]]s. Questo tipo di approccio è molto potente, ma il codice generato risulta molto complicato, tale da costituire talvolta un problema per molti [[Compilatore|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.
STL è stata progettata e sviluppata presso la [[Hewlett-Packard]] da Alexander Stepanov e Meng Lee e sono state incluse nello standard ANSI/ISO nel 1995.
''La C++ Standard Library'' è definita dal [[ISO/IEC 14882]].
STL e le idee contenute in essa, hanno avuto una notevole influenza nello sviluppo della [[C++ Standard Library]] con numerosi programmatori che hanno contribuito allo sviluppo di entrambe le librerie, malgrado ciò le due librerie sono rimaste distinte e nessuna delle due è un super-insieme definito dell'altra.
The Standard Template Library was created as the first library of generic algorithms and data structures, with four ideas in mind: generic programming, abstractness without loss of efficiency, the [[Von Neumann architecture|Von Neumann computation model]], and value semantics.
==History Contenuti ==
=== Contenitori ===
I [[container (informatica)|contenitori]] della STL si dividono in sequenziali e associativi. A loro volta, una parte dei contenitori sequenziali può essere definita come adattatori, in quanto sono in effetti delle interfacce ridotte e specializzate dei contenitori principali che non implementano iteratori nella loro interfaccia. I contenitori standard sequenziali includono vector, list e deque. E comprendono gli adattatori queue, priority_queue e stack. I contenitori associativi sono set, multiset, map e multimap.
{| class="wikitable" style="margin: 1em auto 1em auto"
The architecture of STL is largely the creation of one person, [[Alexander Stepanov]]. In [[1979]] he began working out his initial ideas of [[generic programming]] and exploring their potential for revolutionizing software development. Although [[Dave 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]]).
!Contenitore || Descrizione
|-
! colspan="2"| Sequenziali
|-
|[[vector (STL)|vector]]
|un [[array dinamico]], simile all'[[array]] del C (per esempio, capace di [[accesso casuale]]) con la capacità di ridimensionarsi automaticamente a causa dell'inserimento o della cancellazione di elementi. Gli elementi sono memorizzati su una porzione di memoria continua. L'inserimento e la rimozione degli elementi nel/dal vector in coda viene effettuato in tempo costante <math>(O(1))</math>. L'inserimento e la rimozione all'inizio o nel centro e la ricerca vengono effettuate in tempo lineare <math>(O(n))</math>.
|-
|[[list (STL)|list]]
|una lista bidirezionale; gli elementi non sono memorizzati in una memoria continua. Per questo motivo non è possibile accedere direttamente ad un elemento della lista [[accesso casuale]], ma è necessario farlo tramite l'utilizzo di un [[iteratore]]. L'accesso agli elementi viene quindi effettuato con tempo lineare <math>(O(n))</math> così come la ricerca, tuttavia le operazioni di inserimento e cancellazione vengono effettuate in tempo costante <math>(O(1))</math>.
|-
! colspan="2"| Associativi
|-
|[[set (STL)|set]]
|un insieme ordinato che non consente duplicati; l'inserimento e la cancellazione degli elementi in un insieme non invalida il puntamento degli iteratori nell'insieme. Le operazioni sono l'unione, intersezione, differenza, differenza simmetrica e il test di inclusione.
|-
|multiset
|come per il set, ma consente la presenza di elementi duplicati.
|-
|[[map (STL)|map]]
|un array associativo ordinato rispetto alla chiave; consente la mappatura di un dato (chiave) associato ad un altro (valore). Entrambi i tipi di dato possono essere definiti dall'utente. Permette ricerche rapide rispetto alla chiave, l'accesso ai dati ha tempo logaritmico <math>(O(log \ n))</math>. Non consente di assegnare più chiavi ad un singolo valore.
|-
|multimap
|come per la map, ma consente la presenza di chiavi duplicate.
|-
|hash_set
hash_multiset<br />hash_map<br />hash_multimap
|simili al set, multiset, map o multimap, rispettivamente, ma implementati usando una [[tabella hash]]; le chiavi non sono ordinate, ma una [[funzione hash]] deve esistere per ogni tipo di chiave. Questi contenitori non fanno parte della Libreria Standard C++, ma sono inclusi nella estensione SGI della STL, e sono comunemente incluse come per esempio nella libreria del GNU C++, nel [[namespace]] <code>__gnu_cxx</code> o nel namespace std_ext di Visual Studio. Potrebbero essere incluse nelle estensioni future dello standard C++.
|}
=== Algoritmi ===
Stepanov recognized the full potential for generic programming and persuaded his then-colleagues at [[General Electric|General Electric Research and Development]] (including, primarily, [[Dave Musser]] and [[Deepak Kapur]]) that generic programming should be pursued as a comprehensive basis for software development. At the time there was no real support in any programming language for generic programming.
Nella STL sono inclusi numerosi algoritmi per eseguire operazioni come la ricerca e l'ordinamento. Tali algoritmi sono comunemente utilizzati per la manipolazione dei container in maniera indiretta, cioè solo tramite [[iteratore|iteratori]]. Molti di questi algoritmi operano su un intervallo del container definito dall'utente tramite due iteratori che indicano gli estremi dell'intervallo.
{{C++}}
The first major language to provide such support was [[Ada programming language|Ada]], with its generic units feature. 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. 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 (it did not even have templates, added only later). 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.
{{portale|informatica}}
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 [[Bell Laboratories|AT&T Bell Laboratories]] and later at [[Hewlett-Packard|Hewlett-Packard Research Labs]], Stepanov experimented with many architectural and algorithm formulations, first in [[C programming language|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.
[[ frCategoria:Standard Template Library | ]] ▼
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 array|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 (set theory) | union]], [[intersection (set theory) | intersection]], difference, [[symmetric difference]] and test of inclusion.
Libraries implementing STL often include [[hash table | hashed]] variants: ''hash_set'', ''hash_multiset'', ''hash_map'' and ''hash_multimap'', however this extension is not part of standard and are defined in various [[namespace]]s 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 object|function classes]]. They are useful for keeping and retrieving state information in functions passed into other functions.
==References==
* [[Scott Meyers]], ''[[Effective STL|Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library]]'' ISBN 0201749629
==External links==
*[http://dmoz.org/Computers/Programming/Languages/C%2b%2b/Class_Libraries/STL/ Collection of Links for the STL]
*[http://www.sgi.com/tech/stl/stl_introduction.html SGI Introduction to the STL]
[[Category:C programming language family]]
[[de:Standard_Template_Library]]
[[et:CPP-STL]]
▲[[fr:Standard Template Library]]
[[pl:Standard Template Library]]
[[zh:标准模板库]]
|