Namespace
Varianti

std::vector

Da cppreference.com.
< cpp‎ | container
 
 
 
std::vector
Membri funzioni
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
vector::vector
vector::~vector
vector::operator=
vector::assign
vector::get_allocator
Elemento accesso
Original:
Element access
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
vector::at
vector::operator[]
vector::front
vector::back
vector::data(C++11)
Iteratori
Original:
Iterators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
vector::begin
vector::cbegin

(C++11)
vector::end
vector::cend

(C++11)
vector::rbegin
vector::crbegin

(C++11)
vector::rend
vector::crend

(C++11)
Capacità
Original:
Capacity
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
vector::empty
vector::size
vector::max_size
vector::reserve
vector::capacity
vector::shrink_to_fit(C++11)
Modificatori
Original:
Modifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
vector::clear
vector::insert
vector::emplace(C++11)
vector::erase
vector::push_back
vector::emplace_back(C++11)
vector::pop_back
vector::resize
vector::swap
 
Elemento definito nell'header <vector>
template<

    class T,
    class Allocator = std::allocator<T>

> class vector;
(1)
namespace pmr {

    template <class T>
    using vector = std::vector<T, std::pmr::polymorphic_allocator<T>>;

}
(2)
1) std::vector è un container sequenziale che incapsula un array di dimensione dinamica.
2) std::pmr::vector è un alias template che usa polymorphic allocator

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.
T deve soddisfare i requisiti di CopyAssignable e CopyConstructible. (fino al c++11)
I requisiti che sono imposti agli elementi dipendono dalle operazioni effettivamente compiute sul container. Generalmente è richiesto che il tipo dell'elemento sia completo e soddisfi i requisiti di Erasable, ma molte funzioni membro impongono requisiti più forti. (dal C++11)
(fino al c++17)
I requisiti che sono imposti agli elementi dipendono dalle operazioni effettivamente compiute sul container. Generalmente è richiesto che il tipo dell'elemento soddisfi i requisiti di Erasable, ma molte funzioni membro impongono requisiti più forti. Questo container (ma non i suoi membri) possono essere istanziati con un tipo di elementi incompleto se l'allocator soddisfa il allocator completeness requirements. (dal C++17)

[modifica]

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]

[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]

[modifica] Invalidazione degli iteratori

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 [modifica]
allocator_type Allocator [modifica]
size_type Tipo intero senza segno (generalmente size_t)[modifica]
difference_type Tipo intero con segno (generalmente ptrdiff_t) [modifica]
reference Allocator::reference (fino al c++11)
value_type& (dal C++11) [modifica]
const_reference Allocator::const_reference (fino al c++11)
const value_type& (dal C++11) [modifica]
pointer Allocator::pointer (fino al c++11)
std::allocator_traits<Allocator>::pointer (dal C++11) [modifica]
const_pointer Allocator::const_pointer (fino al c++11)
std::allocator_traits<Allocator>::const_pointer (dal C++11) [modifica]
iterator RandomAccessIterator [modifica]
const_iterator Iteratore ad accesso casuale su costanti[modifica]
reverse_iterator std::reverse_iterator<iterator> [modifica]
const_reverse_iterator std::reverse_iterator<const_iterator> [modifica]

[modifica] Funzioni membro

costruisce il vector
(metodo pubblico) [modifica]
distrugge il vector
(metodo pubblico) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
access the last element
(metodo pubblico) [modifica]
(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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
(C++11)
constructs element in-place
(metodo pubblico) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
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) [modifica]
changes the number of elements stored
(metodo pubblico) [modifica]
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]

[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) [modifica]
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) [modifica]
Cancella tutti gli elementi che soddisfano un certo criterio
(funzione di modello) [modifica]

[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