C++ vektorius
Vektorius yra sekos konteinerio klasė, kuri įgyvendina dinaminį masyvą, o tai reiškia, kad dydis automatiškai pasikeičia pridedant elementus. Vektorius išsaugo elementus gretimose atminties vietose ir paskirsto atmintį pagal poreikį vykdymo metu.
Skirtumas tarp vektoriaus ir masyvo
Masyve taikomas statinis metodas, o tai reiškia, kad jo dydis negali būti keičiamas vykdymo metu, o vektorius įgyvendina dinaminį masyvą, tai reiškia, kad pridedant elementus jis automatiškai keičia savo dydį.
Sintaksė
Apsvarstykite vektorių „v1“. Sintaksė būtų tokia:
vector v1;
Pavyzdys
Pažiūrėkime paprastą pavyzdį.
#include #include using namespace std; int main() { vector v1; v1.push_back('javaTpoint '); v1.push_back('tutorial'); for(vector::iterator itr=v1.begin();itr!=v1.end();++itr) cout<<*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;> Šiame pavyzdyje eilutei rodyti buvo naudojama vektoriaus klasė.
C++ vektorinės funkcijos
| Funkcija | apibūdinimas |
|---|---|
| adresu () | Jame pateikiama nuoroda į elementą. |
| atgal () | Jame pateikiama nuoroda į paskutinį elementą. |
| priekis () | Jame pateikiama nuoroda į pirmąjį elementą. |
| apsikeitimas () | Jis keičiasi elementais tarp dviejų vektorių. |
| pastumti atgal() | Pabaigoje jis prideda naują elementą. |
| pop_back() | Jis pašalina paskutinį elementą iš vektoriaus. |
| tuščia() | Jis nustato, ar vektorius tuščias, ar ne. |
| Įdėti() | Jis įterpia naują elementą nurodytoje vietoje. |
| ištrinti () | Jis ištrina nurodytą elementą. |
| pakeisti dydį () | Tai pakeičia vektoriaus dydį. |
| aišku () | Jis pašalina visus elementus iš vektoriaus. |
| dydis () | Jis nustato daugybę vektoriaus elementų. |
| talpa () | Jis nustato dabartinę vektoriaus talpą. |
| priskirti () | Jis priskiria vektoriui naujas reikšmes. |
| operatorius=() | Jis priskiria naujas reikšmes vektorių konteineriui. |
| operatorius[]() | Jis pasiekia nurodytą elementą. |
| galas() | Tai nurodo vektoriaus elementą past-lats. |
| vieta () | Jis įterpia naują elementą prieš pat poziciją poz. |
| emplace_back() | Pabaigoje įterpia naują elementą. |
| pateikti () | Jis nurodo elementą, esantį prieš pirmąjį vektoriaus elementą. |
| rbegin () | Jis nurodo paskutinį vektoriaus elementą. |
| pradėti () | Jis nurodo pirmąjį vektoriaus elementą. |
| max_size() | Jis nustato maksimalų dydį, kurį gali turėti vektorius. |
| keletas() | Tai reiškia ankstesnį-paskutinį elementą vektoriuje. |
| cbegin() | Tai reiškia pirmąjį vektoriaus elementą. |
| crbegin () | Tai reiškia paskutinį vektoriaus simbolį. |
| crend () | Tai reiškia elementą, esantį prieš pirmąjį vektoriaus elementą. |
| Sumažinti, kad tilptų() | Tai sumažina talpą ir prilygsta vektoriaus dydžiui. |