logo

C++ vektors

Vektors ir secības konteinera klase, kas ievieš dinamisku masīvu, kas nozīmē, ka lielums automātiski mainās, pievienojot elementus. Vektors saglabā elementus blakus esošās atmiņas vietās un piešķir atmiņu pēc vajadzības izpildes laikā.

javafx par aptumsumu

Atšķirība starp vektoru un masīvu

Masīvs izmanto statisku pieeju, kas nozīmē, ka tā lielumu nevar mainīt izpildes laikā, savukārt vektors ievieš dinamisku masīvu, kas nozīmē, ka tas automātiski maina izmērus, pievienojot elementus.

Sintakse

Apsveriet vektoru 'v1'. Sintakse būtu šāda:

bfs un dfs
 vector v1; 

Piemērs

Apskatīsim vienkāršu piemēru.

 #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;>

Šajā piemērā virknes attēlošanai ir izmantota vektoru klase.

C++ vektora funkcijas

Funkcija Apraksts
at () Tas nodrošina atsauci uz elementu.
atpakaļ () Tas sniedz atsauci uz pēdējo elementu.
priekšā () Tas sniedz atsauci uz pirmo elementu.
apmainīt () Tas apmainās ar elementiem starp diviem vektoriem.
atgrūst() Tā beigās pievieno jaunu elementu.
pop_back() Tas noņem pēdējo elementu no vektora.
tukšs () Tas nosaka, vai vektors ir tukšs vai nē.
ievietot () Tas ievieto jaunu elementu norādītajā vietā.
dzēst () Tas dzēš norādīto elementu.
mainīt izmēru () Tas maina vektora lielumu.
skaidrs () Tas noņem visus elementus no vektora.
Izmērs() Tas nosaka vairākus vektora elementus.
ietilpība () Tas nosaka vektora pašreizējo kapacitāti.
piešķirt () Tas piešķir vektoram jaunas vērtības.
operators=() Tas piešķir vektora konteineram jaunas vērtības.
operators[]() Tas piekļūst noteiktam elementam.
beigas () Tas attiecas uz pagātnes-lats-elementu vektorā.
atrašanās vieta () Tas ievieto jaunu elementu tieši pirms pozīcijas poz.
emplace_back() Tā beigās ievieto jaunu elementu.
renderēt () Tas norāda uz elementu, kas atrodas pirms pirmā vektora elementa.
rbegin() Tas norāda uz vektora pēdējo elementu.
sākt () Tas norāda uz vektora pirmo elementu.
max_size() Tas nosaka maksimālo vektora izmēru.
daži() Tas attiecas uz pagātnes-pēdējo elementu vektorā.
cbegin() Tas attiecas uz vektora pirmo elementu.
crbegin() Tas attiecas uz vektora pēdējo rakstzīmi.
crend ​​() Tas attiecas uz elementu, kas atrodas pirms pirmā vektora elementa.
shrink_to_fit() Tas samazina jaudu un padara to vienādu ar vektora izmēru.