Dynamic Arrays in C++ (std::vector)

  Рет қаралды 372,278

The Cherno

The Cherno

Күн бұрын

Patreon ► / thecherno
Twitter ► / thecherno
Instagram ► / thecherno
Discord ► thecherno.com/discord
Series Playlist ► thecherno.com/cpp
Why is it called "vector"? ► stackoverflow.com/questions/5...
EASTL ► github.com/electronicarts/EASTL
Thank you to the following Patreon supporters:
- Samuel Egger
- Dominic Pace
Gear I use:
-----------------
BEST laptop for programming! ► geni.us/pakTES
My FAVOURITE keyboard for programming! ► geni.us/zNhB
FAVOURITE monitors for programming! ► geni.us/Ig6KBq
MAIN Camera ► geni.us/t6xyDRO
MAIN Lens ► geni.us/xGoDWT
Second Camera ► geni.us/CYUQ
Microphone ► geni.us/wqO6g7K

Пікірлер: 267
@fayzankowshik3625
@fayzankowshik3625 3 жыл бұрын
I love how you treat C++ like an actual language you'd speak in
@BombeNissen
@BombeNissen 6 жыл бұрын
Great set of videos! I study computer engineering, and work with C++ for nearly all assignments. When there's issues with the code, I've been using these videos to get a deeper understanding on how a certain topic works, and it's been a huge help. Keep it up!
@davidcarwile9205
@davidcarwile9205 2 жыл бұрын
Seriously, you are better than my instructors at explaining everything. You are better at explaining than the damn books. These trimmed down and non bloated explanations are so much better than what I have been going through.
@bratanvonehre3559
@bratanvonehre3559 4 жыл бұрын
Brilliant channel man! Right now I have to implement an algorithm in an efficient way in C++ for my studies, your videos are an incredible help!
@ytacctaccnt
@ytacctaccnt 6 жыл бұрын
Go boy, go! You're my inspiration for learning C++. Your videos and your charisma and attitude!
@sizlax
@sizlax 6 жыл бұрын
I double this. He's not like most of the other people that teach this stuff on youtube. Listening to most people is like listening to text to speech or Ben stein and watching pain dry at the same time. This guy will make my life if he avoids using the word GOOEY and says G U I like a normal human being.
@sizlax
@sizlax 5 жыл бұрын
@@MrSerozka Sure, they make up something that sounds retarded and takes more to spell than the original spelling for... reasons! Fuckin millennials..
@mrbrian826
@mrbrian826 2 жыл бұрын
I wonder after 4 years if you ever did learn c++.
@ytacctaccnt
@ytacctaccnt 2 жыл бұрын
@@mrbrian826 Yes, but I still don't use it as my primary language, practically for anything... I'm into C# at the moment
@mrbrian826
@mrbrian826 2 жыл бұрын
@@ytacctaccnt nice
@nighma
@nighma 6 жыл бұрын
It will be so cool to compare one of the STL container (as an example) with one rewritten in the EA library and see the differences ;)
@arhantbararia380
@arhantbararia380 3 жыл бұрын
it will be 35$ extra for that DLC
@Blonder_Studio
@Blonder_Studio 2 жыл бұрын
better not to, BF2042 is one reason for that, game so broken that Engine so broken as well and plus extra charge for DLC, so good that Cherno left EA
@lightknight876
@lightknight876 2 жыл бұрын
@@Blonder_Studio he's too good, passionate and professional for them.
@abdulcool1268
@abdulcool1268 6 жыл бұрын
your videos are truly amazing. I wasn't able to catch up at first but now i really enjoy it and i am getting to understand more and more about c++ everyday
@matthewortiz7859
@matthewortiz7859 5 жыл бұрын
I learned more watching this than a week taking my CSII course! Keep it up
@woofelator
@woofelator 4 жыл бұрын
Same
Жыл бұрын
Fantastic channel! So great to get things explained by someone who actually knows what goes on behind the scenes, and who does not make absolutely everything into an programming analogy with food and animals. 10/10
@JoeyMcCart
@JoeyMcCart 5 жыл бұрын
Man i'm so glad I found your channel, you're saving me rn
@tanguyletessier
@tanguyletessier 2 жыл бұрын
Thank you! One of the best YT channels to explain simply difficult (at first glance) problems and especially put them into practice
@flippygat3
@flippygat3 3 жыл бұрын
Amazing channel, just found it. :) Also the first tutorial channel I have ever found that I do not listen to at 1.5X speed.
@ldmdesign5610
@ldmdesign5610 2 жыл бұрын
i need to slow it down sometimes i think lol
@keidon6841
@keidon6841 2 жыл бұрын
So much usefull information,u do not just give general understanding of a topic but provide the logic of its work i am glad i found your channel
@clodgozon3968
@clodgozon3968 4 жыл бұрын
11:34 Roughly speaking, abstract thinking develops over the years as a programmer which is a good trait so one doesn't have to stay longer into details like this.
@rcookie5128
@rcookie5128 6 жыл бұрын
Great stuff! The name confused me too when I first learned about std::vector..
@maryameskandari7510
@maryameskandari7510 5 жыл бұрын
I learned a lot , thanks pls keep it up and make some video about graph representation and boost library
@yl2415
@yl2415 2 жыл бұрын
Up to this point it's not that I don't know all these, it's just I want to see how you program, because you have way better programming style than I did. Thanks Cherno!
@sanjayreddy3295
@sanjayreddy3295 3 жыл бұрын
It's called a vector because Alex Stepanov, the designer of the Standard Template Library, was looking for a name to distinguish it from built-in arrays. He admits now that he made a mistake, because mathematics already uses the term 'vector' for a fixed-length sequence of numbers. C++11 compounds this mistake by introducing a class 'array' that behaves similarly to a mathematical vector. Alex's lesson: be very careful every time you name something.
@christianlabanca5377
@christianlabanca5377 Жыл бұрын
So now we have a class named Vector that behaves kinda like an array and a class named array that behaves kinda like a vector... Indeed, be careful when naming something
@shadyganem5448
@shadyganem5448 4 жыл бұрын
Great video. I learn so much from this guy. real talent. I would like to learn more about the best practices of memory allocation.
@ashleycalhoun1824
@ashleycalhoun1824 6 жыл бұрын
Probably my favorite programming channel, Not only are his videos exciting, his style is so much cleaner. Best channel on youtube!
@jannesopanen8032
@jannesopanen8032 3 жыл бұрын
Yea he tends to dig a bit deeper than others :)
@ashleycalhoun1824
@ashleycalhoun1824 3 жыл бұрын
@@jannesopanen8032 here you are replying to a comment from a couple years ago, and I've already forgotten all I knew about programming.
@MichaelYoussry
@MichaelYoussry 6 жыл бұрын
Thanks for putting so much effort into these videos.
@archimedeis
@archimedeis 2 жыл бұрын
Wow that was pretty well done. I think I got enough to try out my own sort of mini project.
@agentandy159
@agentandy159 3 жыл бұрын
11:30 Thanks for addressing this finally... Coming from C# I saw the method signatures in C++ and thought "are humans supposed to be able to read this"
@Xgamesvidoes
@Xgamesvidoes 4 жыл бұрын
Yepp... I was also confused with going from Unity C# vectors... To the C++ version of vectors! XD Nice video!
@stevefox7418
@stevefox7418 4 жыл бұрын
Very nicely explained and very informative!
@SkySentry7
@SkySentry7 2 жыл бұрын
Thank you SO SO SO MUCH BROOO! You don't know how you've just helped me... I understood every single thing you just said.😱👍👍👍
@work9167
@work9167 2 жыл бұрын
This is amazing, man. I like your videos so much
@cowboybebop521
@cowboybebop521 3 жыл бұрын
very nice little class learned so much cheers!
@erwinschrodinger2320
@erwinschrodinger2320 3 жыл бұрын
Great video as always!
@ahmedanwar976
@ahmedanwar976 5 жыл бұрын
at 9:48 if anyone doesn't understand how he initialized the struct you can search for "aggregate initialization".
@TheTrashpack234
@TheTrashpack234 6 жыл бұрын
Is it bad that instead of watching the video I'm just watching his hands?
@1Naif
@1Naif 6 жыл бұрын
No
@greatbullet7372
@greatbullet7372 6 жыл бұрын
explains pretty much everything
@mprecup
@mprecup 6 жыл бұрын
If I'm watching his hands, it's because he's getting close to backhanding the cactus.
@MsJavaWolf
@MsJavaWolf 5 жыл бұрын
That is the purpose of the video. The hand gestures are an advanced form of hypnosis that will make you understand vectors better.
@FilSapia
@FilSapia 4 жыл бұрын
I think The Cherno is secretly Italian ;)
@manuellabor2759
@manuellabor2759 5 жыл бұрын
Subscribed! Looking forward to going over your videos on C++👍👍👍👍👍
@vaddimurthyram3364
@vaddimurthyram3364 3 жыл бұрын
Thanks a lot......clearly explained basics about vector(Dynamic Array or Array List)👏👏👏
@younismalik8809
@younismalik8809 Жыл бұрын
Thanks The Cherno! Great explanation as always! 👍
@genelee2050
@genelee2050 5 жыл бұрын
it happened again. Near the end of the video, I thought someone else was playing music somewhere until i realized the source coming from this video...
@blackfun4448
@blackfun4448 3 жыл бұрын
you are amazing at teaching, keep it up!
@dmitrijjafisov1912
@dmitrijjafisov1912 6 жыл бұрын
Quality content! Thumbs up!
@hassantariq4731
@hassantariq4731 4 жыл бұрын
Thank You For Saving My Semester :)
@kemptcode
@kemptcode 6 жыл бұрын
You should mention emplace_back in another video.
@saikirangattu2924
@saikirangattu2924 6 жыл бұрын
Very nice , Please make more videos on other data structures
@damianzarzycki28
@damianzarzycki28 6 жыл бұрын
great content !
@CombatFXZone
@CombatFXZone 5 жыл бұрын
I absolutely love the comparison with Java, because I started out with Java in High School.
@Blonder_Studio
@Blonder_Studio 2 жыл бұрын
learning programmin in high school sucks balls i learned c# in high school and my "teacher" is a piece of shit bad
@MultiVishnu55
@MultiVishnu55 3 жыл бұрын
bro, you're too smart! goddamn, good shit
@manishgurawalia7625
@manishgurawalia7625 3 жыл бұрын
11:40 C++ is love♥️
@nhensonbass
@nhensonbass 2 жыл бұрын
I know this is about five years later, but another reason I've learned not to store pointers in vectors is that the element can be deleted from the vector leaving a memory leak. If you're using vectors with pointers, be careful.
@orbyfied
@orbyfied 3 жыл бұрын
so i didnt know how the vector class worked, so i created an "ArrayList" class, and i did the exact same thing that the vector class does xD to add an element just create a new array with current size + 1 and copy all of the previous contents into array[0 to size-1] and put the new element into array[size]
@orbyfied
@orbyfied 3 жыл бұрын
@Peterolen i know but it is sort of a simplified version of the vector and ArrayList. It has the same concept behind it
@abhayagarwal7103
@abhayagarwal7103 6 жыл бұрын
Man, your concepts are so clear.
@claudiofigueiredo8168
@claudiofigueiredo8168 4 жыл бұрын
hey, what do you use to program in c++? Visual Studio? Awsome work ^^
6 жыл бұрын
please make parallel process and simd tutorial
@zaid4708
@zaid4708 4 жыл бұрын
"Simple spice series" very accurate caption
@limmeh7881
@limmeh7881 3 жыл бұрын
To think I was modestly learning about pointers and jumped into this rabbit hole.
@Solo6R
@Solo6R 6 жыл бұрын
You're a god with a compiler. Been programming in C++ for longer than I care to admit, and when I stumble across something I'm not 100% confident with I think "I wonder if Cherno can help me out here" and sure enough you can, and have, several times! Keep up the good work bud, VERY much appreciated.
@abhisheksa6635
@abhisheksa6635 10 ай бұрын
Will be glad to see someone trying to rewrite a STL written thing which can be more optimal and more towards the personal use case.
@sidharthcs2110
@sidharthcs2110 3 жыл бұрын
In physics we can represent a vector as a row or column matrix( kinda like a 1d array ). maybe that's why
@hangyanma2501
@hangyanma2501 Жыл бұрын
你好,陈诺,你的课讲的太好了
@user-vc2pm5xv7s
@user-vc2pm5xv7s 4 жыл бұрын
I remember in Bjarne Stroustrup's book, he never talks about raw array, he just went a head and talks about std::vector.
@sewingsugar9892
@sewingsugar9892 4 жыл бұрын
Can you recommend a book or course that gets more in depth information about behind the scenes of c++?
@huthayfa1723
@huthayfa1723 6 жыл бұрын
Nice video ; I'm really confused of these vectors in computer science if we needed a dynamic array we would use a linked list . what is the difference between these two ?
@serkanozturk4217
@serkanozturk4217 Жыл бұрын
Personal Notes: -dynamic size, at the background it has limited memory but when reaches the limit, it creates a bigger sized memory and copies contents to it. - #include - std::vector vertices To add: entities.push_back((1,0,0)) (must be implicit conversion) - can use it in for loop: for(Vertex v: vertices) { //do sth } To prevent copying and being faster, for(Vertex& v: vertices) { //do sth } - to delete 2nd element: vertices.erase(vertices.begin()+1)
@bies_moron4404
@bies_moron4404 6 жыл бұрын
Good video !!! How we should use inline ? if function are short we should use inline otherwise we shouldn't ?
@sharrayyy
@sharrayyy 3 жыл бұрын
Coding and guitar That's an awesome combo!
@lizzienovigot
@lizzienovigot 2 жыл бұрын
In linear algebra pne of the basic example of vectors is exactly what programmer would think of as an array or a tuple. So mathematically its quite natural to identify arrays and vectors as being very similar, if not the same thing
@tannerbarcelos6880
@tannerbarcelos6880 5 жыл бұрын
Yet to understand why my professor decided to teach so much array stuff but said “vectors are the same so I’m gonna skip over them” when in reality, these vectors are beautiful works of art that shit on arrays lol. Simply the fact that they’re dynamic makes life so easy.
@thelonewolf9866
@thelonewolf9866 4 жыл бұрын
Excellent!
@tahaalkhashmany5678
@tahaalkhashmany5678 4 жыл бұрын
does it basically work like dynamic memory allocation with malloc/realloc in c?
@phantomstriker7996
@phantomstriker7996 Жыл бұрын
Dynamic arrays (mostly std::vector) is an array which can resize. You can push new things into it.
@otabektillavayev3309
@otabektillavayev3309 Жыл бұрын
Good job
@krood_
@krood_ 5 жыл бұрын
Whats the difference between dynamic array and vector ? are they both the same thing ?
@dan110024
@dan110024 2 ай бұрын
As a front-end JS dev, 3 days ago I had not a care in the world for performance. Half way through this C++ playlist and my first thought on hearing how this works is 'Damn that must be expensive.' JS seems so basic now lol.
@michaeldasilva5976
@michaeldasilva5976 4 жыл бұрын
Hm... While following along after making the Vertex class and attempting to print it to the console using std::cout
@stewartzayat7526
@stewartzayat7526 6 жыл бұрын
I have a question not related to the topic, why are classes in the standard library defined with lowercase identifiers, even though everyone declares classes with camel casing?
@Moren162
@Moren162 6 жыл бұрын
Please go in to data structures! All other videos is not even close your your quality!
@georgecristianiordan3712
@georgecristianiordan3712 5 жыл бұрын
Well i would like to see a video with data structures too, youtube lack of quality videos with data structures in c++.....
@ags0004
@ags0004 5 жыл бұрын
Data structures are supposed to be abstract and language agnostic, unless they need to meet a very specific need. But assuming you are talking about datastructures such as Queues, HashMaps and all that, the implementation is relatively the same. The only thing that might be a little different may be some fine tuning using your knowledge of the language to optimize some bits. But they should work the same way they would in every other language. Knowing how one is implemented in abstract and knowing the language you are going to implement them on is the only thing you need.
@joriskbos1115
@joriskbos1115 5 жыл бұрын
I was writing a little game in c as a bit of a challenge and I had to write my own dynamic array. The way I did it was that you create it with an initial size, but when it exceeds the size, the size of the array doubles. That way I don't have to reallocate the array every single time I add or remove an element, but I'm also not using up too much memory.
@sethtaylor7519
@sethtaylor7519 5 жыл бұрын
I'm pretty sure that is what vectors do as well.
@alexdavidsantiago3955
@alexdavidsantiago3955 4 жыл бұрын
All the videos have in common that when you made a question and the answer is: "It depends"
@obenashu9216
@obenashu9216 5 жыл бұрын
Please make a video on STL in C++
@ycp101
@ycp101 3 жыл бұрын
Yes, you are great! your ccoding skill are great! I envy you!
@paul87173
@paul87173 4 жыл бұрын
hey cherno, why dont you use "using namespace std" when you code?
@mcjustin1
@mcjustin1 3 жыл бұрын
Nice video bud.
@jms547
@jms547 4 жыл бұрын
At 8:50 there's a note about moving rather than copying. Is there a video about this in this series?
@NoName-tn8rq
@NoName-tn8rq 5 жыл бұрын
I tried to do this thing for like 4 hours in total, and now i learned there is library that does this. (wasn't able to do it btw, i got stuck on some exceptions and shit)
@theamjolnir9641
@theamjolnir9641 5 жыл бұрын
how do you access vectors of vectors? like in an matrix cause you can't exactly do matrix.assign() to a specific value can you?
@chainonsmanquants1630
@chainonsmanquants1630 3 жыл бұрын
Thanks
@_CazaBobos
@_CazaBobos 4 жыл бұрын
Can you do something similar in C or just the classic memory allocation commands? Also, if I know the amount that I'm going to insert but it's defined by the user is it correct to declare the array like "Arr[nAmount]"?
@_CazaBobos
@_CazaBobos 3 жыл бұрын
@Peterolen yes, I know about them. The problem is that not every c compiler accepts the example I wrote in the second paragraph. In some microchips you simply can't give variable length, only memory commands.
@1Naif
@1Naif 6 жыл бұрын
Awesome.
@kgravikumar
@kgravikumar 6 жыл бұрын
Can i use the vectors for struct with different data types
@jonathanolivier6932
@jonathanolivier6932 Жыл бұрын
Good video, but if you have a vector of string, what can you do if you want the first character of a string?
@akk5830
@akk5830 2 жыл бұрын
Vectors: Matrices consisting of a single column or row. - Wikipedia - Isn't that matching the definition?
@dhikshithrm
@dhikshithrm 2 жыл бұрын
If i have a pointer to a vector which then moves to a different memory address to due to resize does my pointer automatically point to the new memory address?
@informativecontent4778
@informativecontent4778 5 жыл бұрын
luv u mate
@diassagatov9784
@diassagatov9784 2 жыл бұрын
Enough eye contact for today
@giancarloandrebravoabanto7091
@giancarloandrebravoabanto7091 4 жыл бұрын
hi could you tell us how to watch a bool vector in debug time?. because l did it for int and strings without problems
@smachkim9996
@smachkim9996 6 жыл бұрын
how can we make the function for import series data (around 5000points)?
@MarciusOliveira
@MarciusOliveira 6 жыл бұрын
Fantastic \o
@JohnnyRelentless
@JohnnyRelentless 6 жыл бұрын
I thought that arrays were pointers, so that you are never really passing the array to the function, but only the pointer, so you wouldn't need to pass by reference. Is it different for dynamic arrays, or am I misunderstanding something?
@user-kr5uj4jf2u
@user-kr5uj4jf2u 3 жыл бұрын
A vector is a list of something (numbers for example) (can be in a row or column), A matrix is an array of vectors (one or more rows, one or more columns). More specifiec in Math, A vector is an object that has both a magnitude and a direction represented by numbers...and if you think about it, it is a line in some direction in some dimension that has a list of numbers and has start/end (size/magnitude). So you are little bit wrong, vector is a very good name (: Maybe hard to understand like ArrayList but Yes it makes sense.
@tablatronix
@tablatronix 6 жыл бұрын
How do you init a vector with values, not push after ?
@mohnish.deshpande
@mohnish.deshpande 3 жыл бұрын
Can I ask you which Uni did you go to?
@user-sz9wm4rm5c
@user-sz9wm4rm5c 6 жыл бұрын
From your video I learned Java and C#
@tribecalledshabbazz8758
@tribecalledshabbazz8758 4 жыл бұрын
张铁男 你是中国人吗?
@claudedestquentin3138
@claudedestquentin3138 7 ай бұрын
THE Best !
@PongPongDudlDu
@PongPongDudlDu Ай бұрын
Hey Cherno! I got a little question about passing the vector to a subfunction: In the Video at about 12:50 you are passing the vector via Function(vertices); I would have expected a Function(vertices&); to specify it as a pointer or a reference, basically. In C, every array is automatically passed by reference, it will never be copied. Is it in C++ depending on the parameters of the target function as it is a pointer in your example? Would love to hear your answer as I am a C developer and sometimes C++ is still a little strange to me. All the best!
Optimizing the usage of std::vector in C++
9:44
The Cherno
Рет қаралды 259 М.
Stack vs Heap Memory in C++
19:31
The Cherno
Рет қаралды 554 М.
ROCK PAPER SCISSOR! (55 MLN SUBS!) feat @PANDAGIRLOFFICIAL #shorts
00:31
We Got Expelled From Scholl After This...
00:10
Jojo Sim
Рет қаралды 73 МЛН
ОСКАР ИСПОРТИЛ ДЖОНИ ЖИЗНЬ 😢 @lenta_com
01:01
Arrays in C++
18:31
The Cherno
Рет қаралды 415 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2 МЛН
Conversion Operators in C++
19:56
The Cherno
Рет қаралды 35 М.
POINTERS in C++
16:59
The Cherno
Рет қаралды 1 МЛН
How C++ Vector Works
1:14:34
pikuma
Рет қаралды 20 М.
Copying and Copy Constructors in C++
20:52
The Cherno
Рет қаралды 420 М.
How to Deal with Multiple Return Values in C++
17:20
The Cherno
Рет қаралды 204 М.
TEMPLATES in Modern C++
9:31
cazz
Рет қаралды 26 М.
ROCK PAPER SCISSOR! (55 MLN SUBS!) feat @PANDAGIRLOFFICIAL #shorts
00:31