std::vector
Elemento definito nell'header <vector>
|
||
template< class T, |
(1) | |
namespace pmr { template <class T> |
(2) | |
std::vector
è un container sequenziale che incapsula un array di dimensione dinamica.
Gli elementi sono memorizzati in modo contiguo, il che significa che è possibile accedere agli elementi non solo tramite gli iteratori, ma anche utilizzando gli offset dai puntatori agli elementi. Questo significa che un puntatore ad un elemento di un vector può essere passato a qualunque funzione che si aspetti un puntatore ad un elemento di un array. |
(dal C++03) |
La memoria del vector viene gestita automaticamente, venendo espansa o contratta secondo necessità. Un vector di norma occupa più memoria di un array statico in quanto viene allocata più memoria del necessario, così da poter gestire più efficientemente una futura crescita: in questo modo il vector non ha bisogno di riallocare ogni volta che un elemento viene inserito, ma solo quando la memoria precedentemente allocata viene esaurita. L'ammontare totale della memoria allocata può essere richiesto mediante la funzione capacity(). La memoria extra può essere restituita al sistema chiamando shrink_to_fit(). (dal C++11)
La riallocazione è normalmente un'operazione costosa in termini di performance. La funzione reserve() può essere usata per evitare le riallocazioni se il numero di elementi è noto in anticipo.
La complessità (efficienza) delle più comuni operazioni sui vector è:
- Accesso casuale - O(1) costante
- Inserimento o rimozione di elementi alla fine - ammortizzato O(1) costante
- Inserimento o la rimozione di elementi - lineare distanza dalla fine dell vector O(n)
std::vector
(per T
diversi da bool) soddisfa i requisiti di Container, AllocatorAwareContainer, SequenceContainer, ContiguousContainer (dal C++17) e ReversibleContainer.
Indice |
[modifica] Parametri template
T | - | Il tipo degli elementi.
| ||||||
Allocator | - | Un allocator usato per acquisire/rilasciare memoria e costruire/distruggere gli elementi in quella memoria. Il tipo deve soddisfare i requisiti di Allocator. È undefined behavior se Allocator::value_type non è uguale a T. |
[modifica] Specializzazioni
La libreria standard fornisce una specializzazione di std::vector
per il tipo bool, ottimizzata per l'efficienza spaziale.
spazio-efficiente bitset dinamica Original: space-efficient dynamic bitset The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (classe template) |
[modifica] Invalidazione degli iteratori
This section is incomplete |
Operazioni | Invalidano |
---|---|
Tutte le operazioni di sola lettura | Mai |
swap, std::swap | end() |
clear, operator=, assign | Sempre |
reserve, shrink_to_fit | Se il vector cambia capacità, tutti gli iteratori sono invalidati; altrimenti nessuno. |
erase | Gli elementi eliminati e tutti gli elementi successivi (compreso end()) |
push_back, emplace_back | Se il vector cambia capacità, tutti gli iteratori sono invalidati; altrimenti solo end(). |
insert, emplace | Se il vector cambia capacità, tutti gli iteratori sono invalidati; altrimenti solo quelli dal punto di inserimento in avanti (compreso end()). |
resize | Se il vector cambia capacità, tutti gli iteratori sono invalidati; altrimenti solo end() ed ogni elemento cancellato. |
pop_back | L'elemento cancellato e end(). |
[modifica] Membri tipi
Tipo membro | Definizione |
value_type
|
T
|
allocator_type
|
Allocator
|
size_type
|
Tipo intero senza segno (generalmente size_t) |
difference_type
|
Tipo intero con segno (generalmente ptrdiff_t) |
reference
|
Allocator::reference (fino al c++11)value_type& (dal C++11)
|
const_reference
|
Allocator::const_reference (fino al c++11)const value_type& (dal C++11)
|
pointer
|
Allocator::pointer (fino al c++11)std::allocator_traits<Allocator>::pointer (dal C++11) |
const_pointer
|
Allocator::const_pointer (fino al c++11) std::allocator_traits<Allocator>::const_pointer (dal C++11) |
iterator
|
RandomAccessIterator
|
const_iterator
|
Iteratore ad accesso casuale su costanti |
reverse_iterator
|
std::reverse_iterator<iterator> |
const_reverse_iterator
|
std::reverse_iterator<const_iterator> |
[modifica] Funzioni membro
costruisce il vector (metodo pubblico) | |
distrugge il vector (metodo pubblico) | |
assegna valori al contenitore Original: assigns values to the container The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
assegna valori al contenitore Original: assigns values to the container The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
restituisce l'allocatore associato Original: returns the associated allocator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
Accesso agli elementi | |
accedere elemento specificato con verifica dei limiti Original: access specified element with bounds checking The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
accedere elemento specificato Original: access specified element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
accedere al primo elemento Original: access the first element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
access the last element (metodo pubblico) | |
(C++11) |
accesso diretto alla matrice sottostante Original: direct access to the underlying array The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) |
Iteratori | |
restituisce un iteratore all'inizio Original: returns an iterator to the beginning The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
restituisce un iteratore fino alla fine Original: returns an iterator to the end The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
restituisce un iteratore inverso all'inizio Original: returns a reverse iterator to the beginning The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
restituisce un iteratore inverso alla fine Original: returns a reverse iterator to the end The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
Capacità | |
verifica se il contenitore è vuoto Original: checks whether the container is empty The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
restituisce il numero di elementi Original: returns the number of elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
restituisce il massimo numero possibile di elementi Original: returns the maximum possible number of elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
riserve di stoccaggio Original: reserves storage The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
restituisce il numero di elementi che possono essere tenuti in deposito attualmente assegnate Original: returns the number of elements that can be held in currently allocated storage The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
(C++11) |
riduce l'utilizzo della memoria da liberare la memoria inutilizzata Original: reduces memory usage by freeing unused memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) |
Modificatori | |
cancella il contenuto Original: clears the contents The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
inserti di elementi Original: inserts elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
(C++11) |
constructs element in-place (metodo pubblico) |
cancella elementi Original: erases elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
aggiunge elementi alla fine Original: adds elements to the end The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
(C++11) |
costruisce elementi in-posto alla fine Original: constructs elements in-place at the end The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) |
rimuove l'ultimo elemento Original: removes the last element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
changes the number of elements stored (metodo pubblico) | |
swap il contenuto Original: swaps the contents The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) |
[modifica] Funzioni non membro
lessicografico confronta i valori nella vector Original: lexicographically compares the values in the vector The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) | |
specializzata l'algoritmo std::swap Original: specializes the std::swap algorithm The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) | |
Cancella tutti gli elementi che soddisfano un certo criterio (funzione di modello) |
[modifica] Deduction guides(dal C++17)
#include <iostream> #include <vector> int main() { // Crea un vector di interi std::vector<int> v = {7, 5, 16, 8}; // Aggiunge due interi al vector v.push_back(25); v.push_back(13); // Itera e stampa i valori del vector for(int n : v) { std::cout << n << '\n'; } }
Output:
7 5 16 8 25 13