logo

Vector C++

Un vector este o clasă de container de secvență care implementează o matrice dinamică, înseamnă că dimensiunea se schimbă automat la adăugarea elementelor. Un vector stochează elementele în locații de memorie adiacente și alocă memoria după cum este necesar în timpul rulării.

șir de date java

Diferența dintre vector și matrice

O matrice urmează abordarea statică, înseamnă că dimensiunea sa nu poate fi modificată în timpul rulării, în timp ce vector implementează o matrice dinamică, înseamnă că se redimensionează automat atunci când se adaugă elemente.

Sintaxă

Luați în considerare un vector „v1”. Sintaxa ar fi:

javascript pentru buclă
 vector v1; 

Exemplu

Să vedem un exemplu simplu.

 #include #include using namespace std; int main() { vector v1; v1.push_back(&apos;javaTpoint &apos;); v1.push_back(&apos;tutorial&apos;); for(vector::iterator itr=v1.begin();itr!=v1.end();++itr) cout&lt;<*itr; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> javaTpoint tutorial </pre> <p>In this example, vector class has been used to display the string.</p> <h2>C++ Vector Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> at() </td> <td>It provides a reference to an element.</td> </tr> <tr> <td> back() </td> <td>It gives a reference to the last element.</td> </tr> <tr> <td> front() </td> <td>It gives a reference to the first element.</td> </tr> <tr> <td> swap() </td> <td>It exchanges the elements between two vectors.</td> </tr> <tr> <td> push_back() </td> <td>It adds a new element at the end.</td> </tr> <tr> <td> pop_back() </td> <td>It removes a last element from the vector.</td> </tr> <tr> <td> empty() </td> <td>It determines whether the vector is empty or not.</td> </tr> <tr> <td> <a href="/c-vector-insert">insert()</a> </td> <td>It inserts new element at the specified position.</td> </tr> <tr> <td> erase() </td> <td>It deletes the specified element.</td> </tr> <tr> <td> resize() </td> <td>It modifies the size of the vector.</td> </tr> <tr> <td> clear() </td> <td>It removes all the elements from the vector.</td> </tr> <tr> <td> <a href="/c-vector-size">size()</a> </td> <td>It determines a number of elements in the vector.</td> </tr> <tr> <td> capacity() </td> <td>It determines the current capacity of the vector.</td> </tr> <tr> <td> assign() </td> <td>It assigns new values to the vector.</td> </tr> <tr> <td> operator=() </td> <td>It assigns new values to the vector container.</td> </tr> <tr> <td> operator[]() </td> <td>It access a specified element.</td> </tr> <tr> <td> end() </td> <td>It refers to the past-lats-element in the vector.</td> </tr> <tr> <td> emplace() </td> <td>It inserts a new element just before the position pos.</td> </tr> <tr> <td> emplace_back() </td> <td>It inserts a new element at the end.</td> </tr> <tr> <td> rend() </td> <td>It points the element preceding the first element of the vector.</td> </tr> <tr> <td> rbegin() </td> <td>It points the last element of the vector.</td> </tr> <tr> <td> begin() </td> <td>It points the first element of the vector.</td> </tr> <tr> <td> max_size() </td> <td>It determines the maximum size that vector can hold.</td> </tr> <tr> <td> cend() </td> <td>It refers to the past-last-element in the vector.</td> </tr> <tr> <td> cbegin() </td> <td>It refers to the first element of the vector.</td> </tr> <tr> <td> crbegin() </td> <td>It refers to the last character of the vector.</td> </tr> <tr> <td> crend() </td> <td>It refers to the element preceding the first element of the vector.</td> </tr> <tr> <td> shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the vector.</td> </tr> </table></*itr;>

În acest exemplu, clasa vectorială a fost folosită pentru a afișa șirul.

Funcții vectoriale C++

Funcţie Descriere
la() Oferă o referință la un element.
înapoi() Oferă o referire la ultimul element.
față() Oferă o referire la primul element.
swap() Schimbă elementele între doi vectori.
împinge înapoi() Se adaugă un nou element la sfârșit.
pop_back() Îndepărtează un ultim element din vector.
gol() Acesta determină dacă vectorul este gol sau nu.
introduce() Inserează un element nou în poziția specificată.
şterge() Acesta șterge elementul specificat.
redimensiona() Modifică dimensiunea vectorului.
clar() Îndepărtează toate elementele din vector.
mărimea() Determină un număr de elemente în vector.
capacitate() Determină capacitatea curentă a vectorului.
atribui() Acesta atribuie noi valori vectorului.
operator=() Acesta atribuie noi valori containerului vectorial.
operator[]() Acesta accesează un element specificat.
Sfârşit() Se referă la elementul trecut-lats din vector.
Locație() Inserează un nou element chiar înainte de poziţia poz.
plasează_înapoi() Inserează un nou element la sfârșit.
face() Indica elementul care precede primul element al vectorului.
rbegin() Indica ultimul element al vectorului.
ÎNCEPE() Indica primul element al vectorului.
max_size() Determină dimensiunea maximă pe care o poate ține vectorul.
câțiva() Se referă la trecutul-ultimul-element din vector.
cbegin() Se referă la primul element al vectorului.
crbegin() Se referă la ultimul caracter al vectorului.
crend() Se referă la elementul care precede primul element al vectorului.
shrink_to_fit() Reduce capacitatea și o face egală cu dimensiunea vectorului.