introduzione
I vettori sono contenitori di sequenze che possono cambiare dimensione. Il contenitore è un oggetto che contiene dati dello stesso tipo. I contenitori di sequenza memorizzano gli elementi rigorosamente in sequenza lineare.
Vector memorizza gli elementi in posizioni di memoria contigue e consente l'accesso diretto a qualsiasi elemento utilizzando l'operatore pedice []. A differenza dell'array, il vettore può ridursi o espandersi secondo necessità in fase di esecuzione. La memorizzazione del vettore viene gestita automaticamente.
Per supportare la riduzione e l'espansione della funzionalità in fase di esecuzione, il contenitore vettoriale può allocare uno spazio di archiviazione aggiuntivo per adattarsi a una possibile crescita, quindi il contenitore ha una capacità effettiva maggiore delle dimensioni. Pertanto, rispetto all'array, il vettore consuma più memoria in cambio della capacità di gestire lo storage e di crescere dinamicamente in modo efficiente.
Sono validi anche i vettori di dimensione zero. In tal caso vector.begin () e vector.end () punta alla stessa posizione. Ma il comportamento di chiamare front () o back () non è definito.
Definizione
Di seguito è riportata la definizione di std :: vector dal file di intestazione <vector>
template < class T, class Alloc = allocator<T> > class vector;
Parametri
T - Tipo di elemento contenuto.
T può essere sostituito da qualsiasi altro tipo di dati incluso il tipo definito dall'utente.
Alloc - Tipo di oggetto allocatore.
Per impostazione predefinita, viene utilizzato il modello di classe dell'allocatore, che definisce il modello di allocazione della memoria più semplice ed è indipendente dal valore.
Tipi di membri
I seguenti tipi di membro possono essere usati come parametri o tipo restituito dalle funzioni membro.
| Sr.No. |
Tipi di membri |
Definizione |
| 1 |
value_type |
T (Primo parametro del modello) |
| 2 |
allocator_type |
Alloc (Secondo parametro del modello) |
| 3 |
riferimento |
value_type & |
| 4 |
const_reference |
const value_type & |
| 5 |
puntatore |
value_type * |
| 6 |
const_pointer |
const value_type * |
| 7 |
iteratore |
un iteratore ad accesso casuale per value_type |
| 8 |
const_iterator |
un iteratore ad accesso casuale per const value_type |
| 9 |
reverse_iterator |
std :: reverse_iterator <iterator> |
| 10 |
const_reverse_iterator |
std :: reverse_iterator <const_iterator> |
| 11 |
size_type |
size_t |
| 12 |
differenza_tipo |
ptrdiff_t |
Funzioni da <vector>
Di seguito è riportato un elenco di tutti i metodi dall'intestazione <vector>.
Costruttori
| Sr.No. |
Metodo e descrizione |
| 1 |
vector :: costruttore predefinito di vettori Costruisce un contenitore vuoto, con zero elementi. |
| 2 |
vector :: costruttore di riempimento vettoriale Costruisce un contenitore con n elementi e assegna val a ogni elemento. |
| 3 |
vector :: costruttore di intervalli vettoriali Costruisce un contenitore con altrettanti elementi nella gamma di prima per ultimo. |
| 4 |
vector :: costruttore di copie vettoriali Costruisce un contenitore con copia di ogni elemento presente nel contenitore x esistente . |
| 5 |
vector :: costruttore di spostamento vettoriale Costruisce il contenitore con il contenuto di altri utilizzando la semantica di spostamento . |
| 6 |
vector :: costruttore dell'elenco di inizializzatori di vettore Constructs a container from initializer list. |
Destructor
| Sr.No. |
Method & Description |
| 1 |
vector::~vector Destroys container by deallocating container memory. |
Member functions
| Sr.No. |
Method & Description |
| 1 |
vector::assign fill version Assign new values to the vector elements by replacing old ones. |
| 2 |
vector::assign range version Assign new values to the vector elements by replacing old ones. |
| 3 |
vector::assign initializer list version Assign new values to the vector elements by replacing old ones. |
| 4 |
vector::at Returns reference to the element present at location n in the vector. |
| 5 |
vector::back Returns a reference to the last element of the vector. |
| 6 |
vector::begin Return a random access iterator pointing to the first element of the vector. |
| 7 |
vector::capacity Returns the size of allocate storage, expressed in terms of elements. |
| 8 |
vector::cbegin Returns a constant random access iterator which points to the beginning of the vector. |
| 9 |
vector::cend Returns a constant random access iterator which points to the beginning of the vector. |
| 10 |
vector::clear Destroys the vector by removing all elements from the vector and sets size of vector to zero. |
| 11 |
vector::crbegin Returns a constant reverse iterator which points to the reverser beginning of the container. |
| 12 |
vector::crend Returns a constant reverse iterator which points to the reverse end of the vector. |
| 13 |
vector::data Returns a pointer to the first element of the vector container. |
| 14 |
vector::emplace Extends container by inserting new element at position. |
| 15 |
vector::emplace_back Inserts new element at the end of vector. |
| 16 |
vector::empty Tests whether vector is empty or not. |
| 17 |
vector::end Returns an iterator which points to past-the-end element in the vector container. |
| 18 |
vector::erase position version Removes single element from the the vector. |
| 19 |
vector::erase range version Removes single element from the the vector. |
| 20 |
vector::front Returns a reference to the first element of the vector. |
| 21 |
vector::get_allocator Returns an allocator associated with vector. |
| 22 |
vector::insert single element version Extends iterator by inserting new element at position. |
| 23 |
vector::insert fill version Extends vector by inserting new element in the container. |
| 24 |
vector::insert range version Extends vector by inserting new element in the container. |
| 25 |
vector::insert move version Extends vector by inserting new element in the container. |
| 26 |
vector::insert initializer list version Extends vector by inserting new element in the container. |
| 27 |
vector::max_size Returns the maximum number of elements can be held by vector. |
| 28 |
vector::operator= copy version Assign new contents to the vector by replacing old ones and modifies size if necessary. |
| 29 |
vector::operator= move version Assign new contents to the vector by replacing old ones and modifies size if necessary. |
| 30 |
vector::operator = initializer list version Assign new contents to the vector by replacing old ones and modifies size if necessary. |
| 31 |
vector::operator[] Returns a reference to the element present at location n. |
| 32 |
vector::pop_back Removes last element from vector and reduces size of vector by one. |
| 33 |
vector::push_back Inserts new element at the end of vector and increases size of vector by one. |
| 34 |
vector::rbegin Returns a reverse iterator which points to the last element of the vector. |
| 35 |
vector::rend Returns a reverse iterator which points to the reverse end of the vector. |
| 36 |
vector::reserve Requests to reserve vector capacity be at least enough to contain n elements. |
| 37 |
vector::resize Changes the size of vector. |
| 38 |
vector::shrink_to_fit Requests the container to reduce it's capacity to fit its size. |
| 39 |
vector::size Returns the number of elements present in the vector. |
| 40 |
vector::swap Exchanges the content of vector with contents of vector x. |
Non-member overloaded functions
| Sr.No. |
Method & Description |
| 1 |
operator == Tests whether two vectors are equal or not. |
| 2 |
operator != Tests whether two vectors are equal or not. |
| 3 |
operator < Tests whether first vector is less than other or not. |
| 4 |
operator <= Tests whether first vector is less than or equal to other or not. |
| 5 |
operator > Tests whether first vector is greater than other or not. |
| 6 |
operator >= Tests whether first vector is greater than or equal to other or not. |
| 7 |
swap Exchanges the contents of two vector. |