You are on page 1of 14
Vector in C++ STL Ditfeuly Lo Easy © Last Updeted :17 Feb, 2021 Vectors are same as dynamic arrays with the ability to resize itself automatically when an elementis inserted or deleted, with their storage being handled automatically by the container, Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators, In vectors, data is inserted at the end, Inserting at the end takes differential time, as sometimes there may be a need of extending the array. Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is tinearin time. Certain functions associated with the vector are: Rerators 1. egin() - Returns an iterator pointing to the first element inthe vector 2. end().- Returns an iterator pointing to the theoretical element that follows the last element in the vector Wo use cokes ansire you havo he best bows xgeenc nour west By Usher, you ackhowde a you Nave reas and Got it! undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth wna ‘everse iterator pointing to the last elementiin the vector (reverse beginning)..It Start Your Coding Journey Now! 7. eebegin() ~ Returnsa constant moves fram last to first element 8. crendl) - Returns 2 constant reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end) 11 Cot progran to illustrate the 11 iterators in vector ‘include ‘include sing namespace std; int main) vectorcint> gts for (int i= 1; 4 <5) ise) push_back(4)5 cout << "Output of begin and end: “; for (auto i = gi.begin(); 4 != gl.end(); +4) cout << “1 ce cout £6 \nbutnut of choot and cond: * Wo use cokes ansire you havo he best bows xgeenc nour west By Usher, you ackhowde a you Nave reas and Got it! undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth Start Your Coding Journey Now! for (auto ir ~ givcrbegin(); 4 cout ce Mir ces = gleerend()s s4ir) return 85 Output: output of begin anc end: 12345 Output of cbegin and cend: 12345 Output of rbegin and rend: 5 43.22 Output of crbegin and crend : 54.322 Capacity 1. size().- Returns the number of elements in the vector. 2. max size().- Returns the m 3. capacty() ~ Returns the sizeof the storage space currently allocated to the vector expressed as number af imum number of elements that the vector can hold. elements. 4, easize(n) - Resizes the container so thatit contains ‘n’ elements, 5, empty().~ Returns whether the container is empty. 6. shrink to fit() - Reduces the capacity of the container to fit ts size and destrovs all elements bevand the Wo use cokes ansire you havo he best bows xgeenc nour west By Usher, you ackhowde a you Nave reas and Got it! undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth we Start Your Coding Journey Now! int main) ‘ veerorcint> ts for (int (5 1; fe 5s 48) et. push back(4); cout <¢ "Size: ce gtisize(); cout << *\ncapacity :* ce gi-capacity(); cout << "\nMax Size i" cc gl.max_size(); U1 resizes the vector size to 4 eieresize(4); 1/ prints the vector size after resize() cout ¢e "\nsize + * ce gi.size(): 1/ checks if the vector is enpty or not Af (gh.cepty() == false) cout << "\nector is not empty"; alse cout << "\nvectar is empty": 11 Shrinks the vector pl-shrink ¢0_f880; cout << "\nWector elesents are undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth Gotit! Start Your Coding Journey Now! Capacity : 8 MaxSize : a61169601827387903 size : 4 Vector is not expty Vector elenents are: 1234 Element access: 1 reference operator fg] ~ Returns a reference to the element at position'g'in the vector 2. at(g).— Returns a reference to the element at position’ in the vector 3, frant() ~ Returns @ reference to the first element in the vector 4. back() ~ Returns a reference to the last element in the vector 5. data() ~ Returns a direct pointer to the memory array used internally by the vector to store its owned elements. 11 Cot progran to illustrate the 7/ elenent accesser in vector Hinclude using namespace std; int ain() Wo use cokes ansire you havo he best bows xgeenc nour west By Usher, you ackhowde a you Nave reas and Got it! undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth Start Your Coding Journey Now! cout << "\nfront() + gtefront() =" << glefront()s cout ce "\nback() + gt-back() ce ga.back()s 11 pointer to the First elenent Ant pos = gi.dsta(); cout << *\nthe first element is" ce *poss return &5 output: Reference operator [g] : gi(2] = 30 at: gl.at(4) = 52 Front() : gi.front() = 1¢ back() : gl-back() = 108 The first elenent is 10 Modifiers: 1 assign It assigns new value to the vector elements by replacing old ones Wo use cokes ansire you havo he best bows xgeenc nour west By Usher, you ackhowde a you Nave reas and undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth Gotit! Data Structures Start Your Coding Journey Now! Algorthms Interview Preparation Topic-wise Practice end of the vector 11 Cox progran to illustrate the 1 WoasFiers in vector Finclude ‘include using namespace std; int main) ‘ 1/ Assign vector veckorcint> vj 11 #4221 the areay with 10 Five tines veassign(5, 18); cout << "The vector elenents are: *s for (int i = 0; 3 < v.size(); i+) cout << v[i] <<" // Angerts 15 to the last position vspush_back(35); undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth Jeve Python Competitive Programming Machine Learnir Gotit! Start Your Coding Journey Now! cout < ¥EA) ce" 45 [/ Angerts 5 at the beginning veinsert(v.begin(), 5)5 < "othe first element ts: © << viel; 11 venoves the first element verase(y.begin())s cout << "\nthe first element is: * << v{0]; U/ Angerts at the begioning vebegin(), 5); nthe First element ts: * << v[@]; 11 Inserts 20 at the end veemplace back(28); <"\athe last elenent is: " << vin - 13; 11 erases the vector veclear() cout cc "\nVector size after erase( 11 to vector to perform swap vectorcdnt> vi, v25 undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth Gotit! Start Your Coding Journey Now! cout << * vector 2: 5 for (int i = 0; 4 < v2-size(); S++) cout << vafi] ce 11 Swaps vt and v2 vi. swap(v2); cout << "\nafter Swap \nvector 2: "5 for (Ant i= 0; < vi.size(); i+) cout << vifi] <7 cout << *\nveetor for (int i = 0; i < v2.size(); i++) cout <« vali] < Output: The vector elenents are: 10 1@ 10 10 18 The last elenent is: 15 The vector elenents are: 1@ 10 10 10 20 The first elerent is: 5 The first elenent is: 18 The first elenent is: 5 Wo use cokes ansire you havo he best bows xgeenc nour west By Usher, you ackhowde a you Nave reas and undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth Gotit! Start Your Coding Journey Now! Login Register All Vector Functions : * vectorsbegin() and vector:iend/) * vector rbegin() and reng() + vectorscbegin() and vector:cend() © vector:crend() and vector:erbegin(). * vectorsassignt) + vector:at() © vector:back |) © vector:scapacity() + vectorsctear!) * vector:push back!) © vectorupop back") © vectorempty\) © vectorierase(), vectorsize() vectorsswap() vectorreserve(), vectorsresize() vector::shrink to fit() ectorsonerator=, vectorsoperator!) vectocsfront(), vector:data() vectorvemplace back(). vectoremplace) vectormax_size() vectorsinsert(), ea you have ea and ott! undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth Start Your Coding Journey Now! Login ster nd anything i hare more information about the topic discussed above. Want to learn from the best curated videos and practice problems, check out the {++ Foundation Course for Basic to Advanced C++ and G+# STL Course for foundation plus STL To complete your preparation from learning @ language te DS Algo and many more, please refer Complete Interview Preparation Courss Undesaed ou Gothia Ply & Peay Ply Got it! exer gsrpnbeecoersosth start Your Caling Journey Now! | Examples eT RECOMMENDED ARTICLES Page: 123 vector::empty( and vector::size() in C++ STL vector:begin0 and vector:iendQ in C++ STL O1 ees 05 me vector::push_back( and veetor:pop_backO vector: ebegin( and vector : condO in C++ 02 wore 06 s 2,06617 Moy vector:operator= and vector::operator{ Jin vector:sfrant() and veetor::back()in C++ STL 03 es O7 se, osm 08. vestorssrendo & vectorserbegind with undesood o Gooia Paley & Prey Paley ea you have ea and ott! exer gsrpnbeecoersosth sa start Your Caling Journey Now! Article Contributed By : Vote for difficulty Improved By: estenger, msakibhr Aricle Tage: epp-eonieiners-library, epp-veetor, STL. C++ Practice Tags: STL CPP prove Article Report esue \Woting code n comment? Please use Me eskaforpeks.org, generate nk and share the nk here, Wo use cokes ansire you havo he best bows xgeenc nour west By Usher, you ackhowde a you Nave reas and Got it! undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth as Company About Us Privacy Pliey Contact Us Copyright Policy Learn Algorithns Machine learning CS Subjects Video Tutorials start Your Caling Journey Now! News Technology Finance Lifestyle Languages Pythen Jove colang c# Web Development Web Tutoriale JevaSerigt Bootitrop, @geekstorgeeis, Some rights rezerved Wo use cokes ansire you havo he best bows xgeenc nour west By Usher, you ackhowde a you Nave reas and undesood o Gooia Paley & Prey Paley exer gsrpnbeecoersosth Contribute Write an Article Pick Topics to Write Internships Video Internship Gotit!

You might also like