Learn C++ With Me #18 - Vectors
Key Takeaways
This video tutorial covers the basics of vectors in C++, including dynamic resizing, accessing elements, and using methods like front(), back(), size(), and capacity(). It also discusses vector capacity, insertion, and erasure, as well as iteration using iterators and index-based methods. The tutorial uses C++ and references AlgoExpert for coding interview prep.
Full Transcript
hello everybody and welcome to another c plus plus tutorial for beginners in today's video i'll be covering vectors now a vector is a dynamically resizable array this means that the vector can change its size so unlike an array that is a set static size that cannot change if you try to add more elements into a vector and it is not large enough it will just increase itself now there's a lot of complexities involved with that but anyways we'll talk about them in this video all right so the first thing we're going to do here is include vector so we're going to say include if i can spell include correctly not happening very well here we will include vector and just a note here if you're ever noticing that for some reason i'm using some data type or object that's not working for you make sure you have the included statement right so previously i think we used maps and you needed to include that we had use some other things you need to include as well and sometimes like stuffs kind of works but then doesn't work and anyways just always check the include statement because that's probably like a pretty easy fix to a lot of problems you guys might be running into anyways what is a vector as i was saying a vector really is just like an array except it has a lot of extra functionality so it is kind of wrapping an array and allowing this array to dynamically change its size and when i say dynamically change its size as i was kind of stating in the introduction this means that we don't need to know how big the vector is when we first create it i don't need to define that it's only going to have five elements in or 20 elements or whatever i can do that there's options to specify how big the vector actually is but i don't have to and that also means that if i decide to add something into the vector it's not large enough it will just expand itself i can also have the vector shrink itself so that any of the kind of extra space it's allocating for new elements it no longer is using in our computer's memory anyways it's best if i just show you a few examples so let's say vector we're going to define the type of the vector which in this case is going to be int and then we need the name so in this case say v1 and this is going to be equal to and then just like an array so 1 2 comma 3. so now i've created a vector you can see there are no problems here and well that is how you make a vector so obviously you need to pick one type you put it in the angle brackets then everything in the vector must be that type so there's all kinds of stuff we can do with a vector first thing we can do is we can look at the position of different elements right let's say v1 at index 1 we can end l here that's going to give us 2 because well that's index 1 of the vector index 2 will give us 3 and there you go now we can also look at the front of the vector so i think i don't know if i need parentheses or not but i believe i can do front and yeah that's gonna give me one and then i think i can do back and that is going to give me the end so to get the start and the end of the vector you can do the front and the back now i have a little cheat sheet on my uh kind of right monitor here because there's so many different ones to go through those are kind of the main ones in terms of accessing elements in the vector so very similar to what you use for an array then we have the size uh this will tell us the number of elements in the vector so three obviously if i change this like four you know now we have four i can also look at what's known as the capacity of the vector now this one i'm going to do some kind of fancy examples to show you how this changes right now the capacity of this vector is four but if i decide to add some elements to this vector which i will show you how to do you'll notice this capacity is not necessarily going to be the same as the size of the vector so the capacity is how many elements it can currently hold and the size is the number of elements it's actually holding so if i decide to add an element to the vector and the way i can do this is v1 dot and then push back now what this means is push an element from the back of the vector so this is kind of equivalent to appending an element so adding to the end of the vector is what this really means and i decide to push maybe like nine here let's see if the capacity changes notice that i added one element and my capacity increased not by one it increased actually by what was mapped there by four it doubled the size of the vector so now the capacity is eight so let's actually add a total of eight elements or add a number of elements that we get to eight so if i do this now i push four nines and i push one more then technically our vector should be of size nine right so now if i do this notice that the capacity goes up to sixteen so what's actually happening here is as soon as i add an extra element and let's actually do this here so when we add just eight the capacity stays at eight as soon as i add this ninth element it knows that the vector is not big enough so it doubles the size of the vector right that's kind of the pattern we're running into now we get 16. and so if we print out the capacity and then we print out sorry we don't need two semicolons here we print out the size of the vector you see we're going to get 16 with the size of 9. so hopefully that's clear but that is kind of how the vector works it's just increasing the size of itself as required and it's trying to do it by an amount where it's not going to have to constantly increase the size because increasing the size takes a long time to do there's a few more things that we can do with vectors here so i was pushing from the back but i also can pop from the back or i believe push from the front i'm trying to see if i have that uh there's nothing for push for the front but i'll show you how we can like add something to the front of the vector um let's also see here again just kind of scanning my cheat sheet okay i'll show you pop from back so if i pop from back what i'm doing is removing an element from the back of the vector now when i say pop from back i don't need to give any index or anything and what this will do is return to me the element that was removed and it will remove the very last element from the vector but you'll notice what happens here i'm going to add four elements in and i'm going to pop four elements off and i'm going to print the size and the capacity at kind of each step here so after we add all of the elements on i'm going to look at the capacity and the size i'm then going to pop off four elements and then we'll look at the capacity and the size of the vector so let's do this and notice that we get 8 8 and then we get 8 4. and so the capacity of the vector did not change even though we removed elements from it and so that's something to keep in mind that even if i say pop one more off of here so let's actually copy this line and do this let's see what we get here we get eight eight eight three so the capacity is not going to decrease when you remove elements it's only increasing so if you want to decrease the capacity of a vector and again the reason why you would actually even want to do this is because you don't really want to have a vector that has like one element in it that's using a capacity of say 10 000 or something crazy because in that situation you're just wasting a ton of space your vector is kind of taking up this space in memory and saying okay i have this much room and it's using that space and so if you don't need to be using that space you shouldn't so if you remove a ton of elements from a vector what you can do is shrink that vector and the method to do this is shrink to underscore to underscore fit uh let me make sure that's the correct one i think that's right uh i want to make sure we're not doing this wrong yes uh shrink to fit that is correct so if we run this now uh oops we need a semicolon so let's add that notice that what we get now is 8833 and so it shrank the vector to fit the number of elements that were in it so the capacity of the vector is equal to its size after we say shrink to fit and so those are kind of the core methods for the vector now the few uh the next things i'll show you sorry is to insert and erase elements from the vector so popping from back was pretty straightforward we were removing an element from the end of the vector and so we would have removed these uh four nines and then i guess actually the four after we removed all of that so now how do we insert and kind of erase specific elements so let's erase all of this so we will continue in one second but i need to quickly thank the sponsor of this video and the series which is algo expert algo expert is the best platform to use when preparing for your software engineering coding interviews they have over 150 coding interview questions on the platform which are taught by the best instructors and curated such that you are not wasting your time going through a ton of problems that you will never be asked in a coding interview anyways check out algo expert from the link in the description and use the discount code tech with tim for a discount on the platform okay so the next thing i'm going to show you is how to insert and erase elements now this is not necessarily super intuitive to actually be able to insert an element you need a pointer to the position of the element that you want to insert this element before and so let's kind of look at this here what i can say is v1 dot insert and then if i wanted to insert at the very beginning i would say v1 dot begin and then i would say what i want to insert which is 5 and now if i see out v1 at index 0 let's just end l here and i run this notice that we get 5. so we inserted at the very beginning right the pointer to the kind of first element here of the element 5. now if we do this again and we do 7 this will now insert before 5. so now notice that we get 7 printing out there now if we say v1 dot begin and maybe we add 1 to this well let's just see what's going to happen now and yeah let's see what we get when we print out v1 at index 0. notice that we get 1 but if we print out v1 at index 1 we are getting 5 the element that we inserted at position one so before the element at position one which means that this element will be at whatever position we define here and so that's kind of how this works now this is not really the best way to go about doing this like v1.begin plus one i'm actually i haven't tested if what i'm about to do is gonna work but let's try this let's try v1 at position and let's go with position maybe yeah we can actually we can do zero and let's see if that actually works here i don't think that's going to work yet no that doesn't work but if i try an ampersand here and do that yeah okay so it still doesn't work so we need to do a dot begin and then we can kind of add to dot begin to figure out where we actually want to insert this element now unfortunately i don't really like how this works i wish that there was a way that we could do it just based on the index maybe there is a way to do it just based on the index if there is let me know please in the comments but regardless i'm going to show you now how we can erase so to erase is a very similar thing what we're going to do is say b1 dot arrays and then obviously we don't need to like put an element if we're going to erase one we just need to put what element we want to erase so in this case we'll say v1 dot begin and then this will erase the element that's at this position to the very first position so if i do this notice that we now get two printing out because we well we erased one okay so that's kind of all the core methods and functions i want to show you relating to the vectors the next thing i'm going to show you is how we iterate through a vector so i'm going to get rid of this erase here and i'm just going to show you how we can do this there's a few different ways the first way is to do something like into i is equal to 0. i is less than v1.size notice i'm using the size not the capacity that's very important plus plus i and then i do something like co to be v1 at index i and then end l okay so if we run this we get one two three four as we would expect now that's fine we can go with that method or what we can do is say auto itr is equal to v1 dot begin we can say itr does not equal v1 dot end and then plus plus itr and let's see what we get here if we say c out and we see out itr now let's give a test here let's run this notice we're getting an error no match for operator blah blah let's see what happens now if we do an asterisk so if we do an asterisk notice how now we're actually getting the proper elements because we had to de-reference this pointer so this actually works all right so that's another way you can do this again this gives us all the elements now let's just see what happens if i decide to insert an element in this vector so i say maybe after we do the first one v1 dot insert let's insert at the beginning so v1 dot begin let's insert 10. now let's run this with the iterator we need a semicolon okay now notice we get 10 1 2 3 4 and so all is working all is fine so yeah that's how you iterate through a vector and really there's not much more for me to show you there is a lot of other methods and stuff involved with a vector that you can use with a vector these are kind of the core ones again this is what you would use when you need a kind of dynamically sized structure that can increase or decrease its size just be careful when you're using a vector because if you remove a ton of elements from a vector you could be in a situation where well you have just a ton of space being used by this vector and well it only really needs to soar like two or three elements so anyways with that said i hope you guys enjoyed if you did make sure to leave a like subscribe to the channel i will see you in another youtube video
Original Description
Welcome back to another C++ tutorial! In this video, we're going over vectors. Vectors are resizeable arrays that are able to change their size based off of what is inside them.
💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" https://algoexpert.io/techwithtim
🔍 Playlist: https://youtube.com/playlist?list=PLzMcBGfZo4-lmGC8VW0iu6qfMHjy7gLQ3
⭐️ Timestamps ⭐️
00:00 | Introduction
00:37 | What Are Vectors?
01:54 | Creating Vectors
02:24 | Access Vector Elements
03:12 | Common Vector Methods and Capacity
08:21 | Insert and Erase Elements
10:49 | Iterating Through Vectors
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
💰 Courses & Merch 💰
💻 The Fundamentals of Programming w/ Python: https://tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python
👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop
🔗 Social Medias 🔗
📸 Instagram: https://www.instagram.com/tech_with_tim
📱 Twitter: https://twitter.com/TechWithTimm
⭐ Discord: https://discord.gg/twt
📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/
🌎 Website: https://techwithtim.net
📂 GitHub: https://github.com/techwithtim
🔊 Podcast: https://anchor.fm/tech-with-tim
🎬 My YouTube Gear 🎬
🎥 Main Camera (EOS Canon 90D): https://amzn.to/3cY23y9
🎥 Secondary Camera (Panasonic Lumix G7): https://amzn.to/3fl2iEV
📹 Main Lens (EFS 24mm f/2.8): https://amzn.to/2Yuol5r
🕹 Tripod: https://amzn.to/3hpSprv
🎤 Main Microphone (Rode NT1): https://amzn.to/2HrZxXc
🎤 Secondary Microphone (Synco Wireless Lapel System): https://amzn.to/3e07Swl
🎤 Third Microphone (Rode NTG4+): https://amzn.to/3oi0v8Z
☀️ Lights: https://amzn.to/2ApeiXr
⌨ Keyboard (Daskeyboard 4Q): https://amzn.to/2YpN5vm
🖱 Mouse (Logitech MX Master): https://amzn.to/2HsmRDN
📸 Webcam (Logitech 1080p Pro): https://amzn.to/2B2IXcQ
📢 Speaker (Beats Pill): https://amzn.to/2XYc5ef
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
Related Reads
📰
📰
📰
📰
The Silent Costs of AI APIs Nobody Warns You About
Dev.to · Shaw Sha
The Only AI Tools a Small Business Actually Needs in 2026
Dev.to AI
I Built an AI Life Planner the Month I Graduated and Switched to Linux Halfway Through
Dev.to · Hilal
Your Second Brain Is a Graveyard. Your AI Has Amnesia. (Part 1)
Medium · AI
Chapters (7)
| Introduction
0:37
| What Are Vectors?
1:54
| Creating Vectors
2:24
| Access Vector Elements
3:12
| Common Vector Methods and Capacity
8:21
| Insert and Erase Elements
10:49
| Iterating Through Vectors
🎓
Tutor Explanation
DeepCamp AI