Thanks for watching! If you're after more, try and conditionally erase elements from your data structure while iterating over it, and make sure it works! Also don't forget to check out Skillshare - the first 1000 people who click the link will get 2 free months of Skillshare Premium: skl.sh/thecherno0820
@The Cherno, Yes THANK you for these kind of vids! I don't mean to sound like a complete prik but please, less of the dev reviews/opinions and more of the educational lessons of C++ like you used to upload back. You probably heard this a gazillion times but you are one of the best C++ explainers out there hands down~ Cheers.
@@kadiyamsrikar9565 Uh what? C++20 renamed the old XIterator categories (e.g. InputIterator) to LegacyXIterator (e.g. LegacyInputIterator) and introduced a bunch of concepts to more explicitly categorize iterators. Modules has nothing to do with iterators, did you mean ranges? That doesn't replace iterators, but gives you a nicer set of algorithms that work on anything that models a range. Iterators are still there and aren't going away as far as I can tell. Even if ranges eventually replaces them, that will still be several years off.
@@oracleoftroy you are right mate but c++20 is trying to replace iterators and increase the capabilities of ranges . Module system is added in c++20 just like other modern languages.
University: "As a CS student you will always have to learn the newest technologies." Also university: "Yea you gonna use c++-98, cause thats what the professor learned back in the day and he never bothered to update his lecture for 20 years."
I was recently given a C++ assessment for C++03, this was the first time a company interviewed me for such an old version. Seems like certain companies still have 20 years to catch up.
As a JS developer learning C++, seeing the structured binding at the end made me smile. It literally looks the same, and in JS is called destructuring. Nice video
Since TypeScript become popular, to JS were introduced many features from other languages because people from typed languages could understand JS easier and eg. node.js is written in c++. Than came ES6, ES7... Some implementations failed in my opinion like const, which is not really const when it comes to arrays or objects.
@MyName Here lol. A big chunk of today's web servers are run on Node.JS, entire backends are written just in JavaScript and JavaScript derived technologies. I'd say javascript is as real of a programming language as C, C++, Rust, C#, whatever. Just higher level.
@@DoubleCoolOp well considering how nicely it's evolved and the ecosystem, and variety it's created, your opinion doesn't do it much justice, now does it? :)
Another good reason to use iterators in basic data structures (that you could access by an index) is to have iterators with different behaviors e.g. a reverse iterator (trivial), an iterator that gives you the next random element or that accesses the elements in a specific order or that applies some filter function.
Additionally, would be really useful to mention about the usage of the reference '&' for the iterator it self. I.e. instead of using "for (auto kv : map)" - using "for (const auto& kv : map)" would eliminate unnecessary copies when iterating.
absolutely amazing video. Each video of yours make me love the language even more. I use it a lot in embedded systems and they way C++ elegantly interfaces with the hardware is simply beatiful .
@@practicalsoftwaremarcus Great! Which C++ framework should I know?Do you have any advice on being a professional in this field? What else should I learn except core C++ and standard library and data structures and algorithms in C++?
We used turbo c++ in our college before we graduated in 2021. Stuff that has not been used since 1996. It makes the students hate this language. I really hope they update their system and their syllabus up to the current technology. Never had a lot of fun there 😅
Not really... As far as I know, ++it used to be more efficient for looping through objects/data structures, and pointless for looping through arrays/vectors of primitives. Nowadays the compiler is optimizing it++ (if it's a modern one), so it gets similar performance to ++it. Still, might be good practice to continue differentiating it++ and ++it in code, since the compiler is not all knowing and might not optimize sometimes.
@@Borgilian I say generally, because there are cases where that is not true. It is true, however, that ++it is at least as efficient as it++. It's not true that it++ is at least as efficient as ++it.
@@Borgilian Using prefix is just good practice though. You should probably only use postfix in the cases where you need the temporary generated since that justifies it's use case. Other than that, you should always be using prefix whether the compiler will optimize the postfix or not.
As a recent CS grad, I never quite understand when people say "my university used x language or version", because my university assignemnts (especially final year) were mostly generalized to whichever language you wanted to use. Aside from certain exceptions - for example groupwork, and my mobile apps course was taught in SwiftUI on a specific version of Xcode, for logistical reasons - this was the norm. Did/are other students experiencing a different kind of CS degree, perhaps more practical and less theoretical? Additionally, what is the merit of constraining students in this way?
Thank you for clarifying this, which helped me make sense of it right away: Iterators appeared in the language because not all collections can be iterated over by an index.
@@codingsaroj18 same what a nightmare! The mouse pointer stopped working halfway through randomly then you had to save the file and restart using the keyboard. Notepad is better than turbo c++.
C is actually lean enough for you to learn proper programming without all the OOP and C++ bullshit going on nowadays. You have an opportunity to learn how to code properly and in a way that favors the machine (performance).
I am learning c++ with a book called C++ in One Hour a Day. And when i confuse, don't get something, i know where to go. Your videos are AMAZING. Hope that your videos can reach more people.
Another excellent video! I wish I had these when I was learning this stuff. Even though most of these are not new to me I never miss a single c++ series video because they are so well done. Keep it up!
For the video about iterators, an example about how it could be used for something different than iterating over a collection of element would be to iterate over a random class. Maybe you would construct with arguments about the range (min, max, number of values) and iterate through it, but the iterator would generate random numbers on the fly.
Note on Game Engine: I make my 'Game Engine' in Flutter/Node.js/Sql etc... I make it fully multi interactive say Yatzee full functions etc. Then i have idea it would be easy to replace the Yatzee with say Chess and if good game engine it would be more or less easy to implement. My real idea is that when games came about in the beginning most where full programs not much to backup change since each program is isolated. Forward Amiga came with operating system where games could use the operating system simplifying the game structure. Forward more things are given to the game engine so the switch between games made simple. My idea is that a good game engine provides all most important features so ease of new creation.
In a sense I felt like this was much more useful video for map than the actual map video. Or maybe I remember poorly. However I remember struggling with maps in 2018 or so. It was a terrible school task of STL containers where you had a nature park hiking routes with locations and you'd have to return the route with least elevation change between requested A and B. I'm still having nightmares from that, I just couldn't wrap my head around that. And another thing was nested maps... It was kinda funny to do the first C++ class in 2011 and the second right in 2018. A lot had changed but I had no idea, because even today the material for the first class is the same. The fundamentals don't change. So I guess I'd love to see more of those more advanced/complicated applications of these simple tools. Things that force you to think but also demonstrations of what's actually going on in there.
Thanks once again for a highly informative video! Is there any significant speed difference between regular indexing and iterators on ordered data structures? For example, when I have multiple for loops running over arrays/matrices, would it be faster to use iterators and de-reference pointers or to obtain the values by the usual indexing?
I was told we are doing this is college because they have to show us the theory along with the way things actually work. You arent "supposed" to do certain things without knowing how they work first according to the colleges.
Im going to throw my 2 cents on the topic. I think the right question is not "What is iterator?" but "Why iterator ?" . Using for (or while ) loop(s) using indexing is actually random access order regardless that we (programmers) always ( like 99% of the time ) use the said for indexing only in one direction ... iterators are only sequential access order and can be used only as such ... in theory compiler may be able to generate faster code if using for iterator loop vs for index loop ... and that;s it about iterators ... implementation of iterators is next topic ... but again I would argue the more important is WHY vs WHAT
Iterators become way more important when using range checks as the compiler can't always proof the size of a range but ehen using iterators they can most certainly be eliminated
Hello! Great video! I think it would be interesting for you to make a video about how to implement a custom iterator for a custom STL compatible data structure (may be with some fancy algorithm). In the video you could touch something about template stuff and all these "using value_type = ", etc. Good luck! Looking forward to the new video!
Is it also true that the iterator reduces the number of implementations in the algorithm library? I heard it allows the algorithms to traverse on any container type.
Hey Cherno. Just want to leave some feedback to your videos: You are the BEST!!! Thanks a lot for your effort. You make it so easy to learn C++ and you are such a great character with always perfect hair :D!
speaking of c++03 we actually currently using c++98 and i disagree it's not a shame at all instead a wise decision , first starting from c++11 things really got complicated and if you don't suffer from some bottlenecks in older versions you'll find a hard time understand some of modern c++ features. it's always good to start with the basics.
Wish I knew about this 2 months ago. I had to write a compiler for class that used this everywhere. Normal when I write code, I already feel like a monkey on a typewriter trying to recreate Shakespeare. But after watching this, I think I need to burn and bury my code to atone for the shame.
I fuckin love you. I was just looking for iterators and didn't find your video about it. Seriously, without you I would not have become as good as I am right now thank god. If I could sub to your patrion I would. Good luck with your work boi!!
I love your videos Cherno. I have a question Why don’t they rename “first” and “second” to “key” and “value” respectively? We can then have a cleaner code like “it->key” and “it->value” ? 12:49
It depends on what you need to use them for. Vectors: +Good at sequential access and random access +Stored as a block in memory so not much cache misses -Not good at changing the size of the arrays unless you use the .reserve(x) function Lists: +Adding elements doesn't take much time unlike Vectors -Takes a long time to go through the list since the elements aren't stored directly next to each other in memory (cache misses) That's all I can think of right now, I hope I helped.
@@AraAra064 Thanks a lot. Another question, list is better than vector to sort data, right? I'm developing a Heuristic, and I need sort a lot of data, delete and create. I think list is the obvious choice.
@@victorhugomagalhaes6370 If you were just sorting, I'd go with vector. But since you need to delete and create stuff, list should be fine. I'd probably test to see which one is faster just in case because I'm not exactly a pro at C++.
@@victorhugomagalhaes6370 It's not so easy to know in advance if list or vector is better even when you work with lots of data and you need to delete and stuff. So as AraAra said testing both alternatives is the way to go if it really matters, e.g. for performance. It depends a lot on what your data actually is and how well it fits into caches, etc. I would recommend to start with what feels most natural to use for your data (probably list if your data sets are large and you really sort/delete/create a lot) and not worry much about alternatives until you really need an alternative. Because even if vector would be required performance wise (for large data sets) it would be better in many cases only if you take a few other measures like properly making your data cache friendly (e.g. aligning data) and for a real-time application you would even need to do exotic stuff like controlling releasing of memory. And then things get a lot of work...
He's making a reference to the value of the members of the std::pair pointed by it. This is a way to use more convenient names instead of the first and second of std::pair. On modern C++ you can use this syntax: auto&[key, value] = *it;. Its the same, but cleaner
auto& is auto reference, don't copy the value, auto on the other hand will if the value itself is not reference or pointer. If you don't want modifying the value of auto& referenced to, it's better to add const to it like this: auto const &key = kv.first;
Hey @TheCherno thanks for another great video! Can you also please make a video on different containers, and algorithms in c++, which are most commonly used.
std::vector values = { 1, 2, 3, 4, 5}; error C2552: 'values' : non-aggregates cannot be initialized with initializer list. I'm beginning to dislike Visual Studio (2008 ). Maybe a newer version would work, but I am working on a program targeting Windows XP.
Using C++ 03? I'm glad I didn't waste money going to university... Though I'm pretty sure PSU/UOP is pretty advanced given that Portland has multiple tech companies in it.
iterators are cool and all, but what actually are they? im assuming theyre not integers, but that makes me curious and excited to figure out how to actually make one!
thanks for the video. About the last two methods, is there versions of them to iterate to a certain position of the map and not to the full range of it?
Generally don't use books when it comes to programming languages only up-to-date documentation / tutorials etc. You can get frustrated fast if you try to solve problem, that doesn't exist in the current language version.
However there is one worth to mention c++ book I know for intermediate "C++ Reactive Programming" by Peter Abraham it's generally about available in many languages library called RX here rxcpp. It really helps when you handle multiple asynchronous actions and with no blocking and lazy code you want to solve the problem.
How does kv in "for(auto kv : map)" handle the string? Does it make some sort of reference to it or does it make a copy? Because I've seen people use both auto& and auto&& as well and I'm wondering about the difference.
Question is old... but for future reference: - for(auto kv : map) makes copies of the values (which is fine for very simple types) - for references you would use auto& (and you would typically prefer to use this) - auto&& does not make sense in such a simple example (besides creating confusion) as it results in the same as auto& (so technically it works). It is a more advanced form of references and is used in a few different contexts, the main being used with rvalues (for moving objects instead of copying, or for types which are rvalues like std::vector because they are proxy types) or they are used as so-called forwarding or universal references (which generically work both with rvalues and lvalues, that's why in this simple example it results in the same as auto&). You often see auto&& as parameters for generic lambdas.
Thanks for watching! If you're after more, try and conditionally erase elements from your data structure while iterating over it, and make sure it works! Also don't forget to check out Skillshare - the first 1000 people who click the link will get 2 free months of Skillshare Premium: skl.sh/thecherno0820
Hey cherno why don't you give your opinion on c++20 dropping support for iterators and adding modules
@The Cherno, Yes THANK you for these kind of vids! I don't mean to sound like a complete prik but please, less of the dev reviews/opinions and more of the educational lessons of C++ like you used to upload back. You probably heard this a gazillion times but you are one of the best C++ explainers out there hands down~ Cheers.
@@kadiyamsrikar9565 Uh what? C++20 renamed the old XIterator categories (e.g. InputIterator) to LegacyXIterator (e.g. LegacyInputIterator) and introduced a bunch of concepts to more explicitly categorize iterators. Modules has nothing to do with iterators, did you mean ranges? That doesn't replace iterators, but gives you a nicer set of algorithms that work on anything that models a range. Iterators are still there and aren't going away as far as I can tell. Even if ranges eventually replaces them, that will still be several years off.
@@oracleoftroy your are right mate but c++20 is trying to increase the capabilities and usage of ranges. Module system is added in c++20
@@oracleoftroy you are right mate but c++20 is trying to replace iterators and increase the capabilities of ranges . Module system is added in c++20 just like other modern languages.
University: "As a CS student you will always have to learn the newest technologies."
Also university: "Yea you gonna use c++-98, cause thats what the professor learned back in the day and he never bothered to update his lecture for 20 years."
Shit, I was pretty annoyed that my current professor doesn't really code in C++20 (it's so good) but I guess it could be worse LMAO
@Danilo lol that exist?
@@GalaxiaDeFavio Yeah, like in the Linux Kernel who just recently switched over to GNUC11 after sticking to C89 for decades.
@@jpviscaino good luck finding a job that uses C++20
Love how he mentioned university using c++ 2003 but I was using c++ 98 in my university and i graduated last year.
Psh, we're using K&R c with nonstandard function prototypes (the project was something the prof did as a student 30 years ago)
lol, while we learn a language call Racket, which no body use nowadays...
What year did you graduate?
@@MrCleverOnion July 2019
@@corbinlenning3505 That's insane, my professor uses C++17 and I was already about to complain (C++20 has some great features), wow...
I was recently given a C++ assessment for C++03, this was the first time a company interviewed me for such an old version. Seems like certain companies still have 20 years to catch up.
I updated almost all code bases of all C++ projects in my company from using C++98 to C++17 in the last 5 years.
actually 17 tho
As a JS developer learning C++, seeing the structured binding at the end made me smile. It literally looks the same, and in JS is called destructuring. Nice video
Since TypeScript become popular, to JS were introduced many features from other languages because people from typed languages could understand JS easier and eg. node.js is written in c++. Than came ES6, ES7... Some implementations failed in my opinion like const, which is not really const when it comes to arrays or objects.
@MyName Here lol. A big chunk of today's web servers are run on Node.JS, entire backends are written just in JavaScript and JavaScript derived technologies. I'd say javascript is as real of a programming language as C, C++, Rust, C#, whatever. Just higher level.
@@magicweaponr072 nothing against higher level languages but javascript is specially garbage
@@magicweaponr072 JavaScript is one of the worst languages ever invented, just like php.
@@DoubleCoolOp well considering how nicely it's evolved and the ecosystem, and variety it's created, your opinion doesn't do it much justice, now does it? :)
Another good reason to use iterators in basic data structures (that you could access by an index) is to have iterators with different behaviors e.g. a reverse iterator (trivial), an iterator that gives you the next random element or that accesses the elements in a specific order or that applies some filter function.
Note that the example with structured bindings will copy the elements. To iterate over a reference to the elements use:
for (auto& [key, value] : map)
thx!
you make my self taught journey of c++ so much easier. Thank you so much
hows its going the self taught journey? im currently in my 2nd month? i see its been a year for you now.
@@ifracing breh
Additionally, would be really useful to mention about the usage of the reference '&' for the iterator it self. I.e. instead of using "for (auto kv : map)" - using "for (const auto& kv : map)" would eliminate unnecessary copies when iterating.
yeah, this is necessary
True! I also use this technique to avoid copying
@Mattéo Rossillol @rawEngine's comment was regarding lthe auto at ine no. 40
ooof, yeah, that sounds expensive with a bigger map
@Mattéo Rossillol oh, right! yeah.. so you'd be able to write things like std::cout
absolutely amazing video. Each video of yours make me love the language even more. I use it a lot in embedded systems and they way C++ elegantly interfaces with the hardware is simply beatiful .
I would like to work in embedded systems too in the future :)
Best topic ever
@@practicalsoftwaremarcus Great! Which C++ framework should I know?Do you have any advice on being a professional in this field? What else should I learn except core C++ and standard library and data structures and algorithms in C++?
We used turbo c++ in our college before we graduated in 2021. Stuff that has not been used since 1996. It makes the students hate this language. I really hope they update their system and their syllabus up to the current technology. Never had a lot of fun there 😅
As a side note, ++it is generally more efficient than it++. The postfix operator has to create a temporary under the hood in addition to calling ++it.
Not really... As far as I know, ++it used to be more efficient for looping through objects/data structures, and pointless for looping through arrays/vectors of primitives. Nowadays the compiler is optimizing it++ (if it's a modern one), so it gets similar performance to ++it. Still, might be good practice to continue differentiating it++ and ++it in code, since the compiler is not all knowing and might not optimize sometimes.
@@Borgilian I say generally, because there are cases where that is not true. It is true, however, that ++it is at least as efficient as it++. It's not true that it++ is at least as efficient as ++it.
@@Borgilian Using prefix is just good practice though. You should probably only use postfix in the cases where you need the temporary generated since that justifies it's use case.
Other than that, you should always be using prefix whether the compiler will optimize the postfix or not.
Me : What are iterators ?
Cherno: Iterators are used to iterate .
Me:.......
Cherno: Sometimes my genius is ........ its almost frightening
I can Feel You
guys i'm gonna be honest...
my brain is my brain
As a recent CS grad, I never quite understand when people say "my university used x language or version", because my university assignemnts (especially final year) were mostly generalized to whichever language you wanted to use. Aside from certain exceptions - for example groupwork, and my mobile apps course was taught in SwiftUI on a specific version of Xcode, for logistical reasons - this was the norm.
Did/are other students experiencing a different kind of CS degree, perhaps more practical and less theoretical? Additionally, what is the merit of constraining students in this way?
Thank you for clarifying this, which helped me make sense of it right away: Iterators appeared in the language because not all collections can be iterated over by an index.
I feel kinda smug that my uni has made it all the way to C++ 11.
atmel studio uses c++ 11, they are getting you ready for MCU programming
@@LittleRainGames We program MCU in C, Assembly and C++ throughout uni, but have never used atmel studio for any of that.
my university teaches us C++ 98
C++11 is better than the new wacky standards anyway.
This is one of the best explanations of iterators I have seen, I was banging my head around google for so long! thanks
Make a short video of typing and shortcuts
I personally use a vim plugin in every other editor, pretty handy stuff
Great! I was waiting for a good tutorial on this. Thanks Cherno.
Just started my journey of c++ by your playlist....am really really excited 😃
This video is crafted overly well and i love it. Thank you for the thorough explanation of iterators.
You teach soo much clearly and go just one step deeper
0:27 yea and mine uses Turbo C which was released in 1993 when C++ wasn't even standardized
Lol Indian problems boy (same here)
yeah my school used turbo c++ in 8th class.
@@codingsaroj18 same what a nightmare! The mouse pointer stopped working halfway through randomly then you had to save the file and restart using the keyboard. Notepad is better than turbo c++.
C is actually lean enough for you to learn proper programming without all the OOP and C++ bullshit going on nowadays. You have an opportunity to learn how to code properly and in a way that favors the machine (performance).
I am learning c++ with a book called C++ in One Hour a Day. And when i confuse, don't get something, i know where to go. Your videos are AMAZING. Hope that your videos can reach more people.
Yerini biliyorsun reis :D
Thanks a lot. I really like the way you explain the iterators. The examples are simple and clear.
Brilliant tutorial on iterators, Cherno. Always good to have such refreshers!
I have never seen or heard of C++17 structured bindings. I love learning new things about C++!
Another excellent video! I wish I had these when I was learning this stuff. Even though most of these are not new to me I never miss a single c++ series video because they are so well done. Keep it up!
For the video about iterators, an example about how it could be used for something different than iterating over a collection of element would be to iterate over a random class.
Maybe you would construct with arguments about the range (min, max, number of values) and iterate through it, but the iterator would generate random numbers on the fly.
Thank you for the 70+ videos and the 20 sponsors from skillshare
This video is still GOLD. Thank you Cherno.
Really useful.
For those watching for a good reference book: C++ primer is the way to go(imo.).
it's very academic and I didn't like the bigger exercises and it goes very (too much?) into depth at times but it's a good book, yeah.
one of the better ones out there to really get into it
it doesn't go into multithreading and some other stuff tho
My input is that this book is for those who are already familiar with programming and beginner to C++.
"Sam's Teach Youself C++ in One Hour a day" is the best!
You are too good man. Thanks for this, to the point and easy to understand, no bs.
Note on Game Engine: I make my 'Game Engine' in Flutter/Node.js/Sql etc... I make it fully multi interactive say Yatzee full functions etc. Then i have idea it would be easy to replace the Yatzee with say Chess and if good game engine it would be more or less easy to implement. My real idea is that when games came about in the beginning most where full programs not much to backup change since each program is isolated. Forward Amiga came with operating system where games could use the operating system simplifying the game structure. Forward more things are given to the game engine so the switch between games made simple. My idea is that a good game engine provides all most important features so ease of new creation.
was waiting for this one!! Love this series, and that smooth sponsor transition
In a sense I felt like this was much more useful video for map than the actual map video. Or maybe I remember poorly. However I remember struggling with maps in 2018 or so. It was a terrible school task of STL containers where you had a nature park hiking routes with locations and you'd have to return the route with least elevation change between requested A and B. I'm still having nightmares from that, I just couldn't wrap my head around that. And another thing was nested maps...
It was kinda funny to do the first C++ class in 2011 and the second right in 2018. A lot had changed but I had no idea, because even today the material for the first class is the same. The fundamentals don't change.
So I guess I'd love to see more of those more advanced/complicated applications of these simple tools. Things that force you to think but also demonstrations of what's actually going on in there.
Expected to see operations between iterators, offsets and so on :)
My uni was C++11 last I took the classes. And I thought that was a bit outdated…
It would be great to have a video on static code analyzers and sanitizers like address/thread sanitizers, cppcheck, sourcemonitor, etc.
Thanks once again for a highly informative video! Is there any significant speed difference between regular indexing and iterators on ordered data structures? For example, when I have multiple for loops running over arrays/matrices, would it be faster to use iterators and de-reference pointers or to obtain the values by the usual indexing?
I'm really waiting for the exception in C++ class....
watch all the c++ videos in a week.
omg Cherno, you somehow look young->old->young
I was told we are doing this is college because they have to show us the theory along with the way things actually work. You arent "supposed" to do certain things without knowing how they work first according to the colleges.
Last for loop for map should be like below
for( const auto& [ key, value ] : map )
Excellent video, was worth watching the entire thing. Subscribed
i enjoyed watching every second of this tutorial. Thank you.
8:00 i would recommend you write ++it instead of it++ because its more efficient in iterators
Dude, you are a fantastic communicator. Far better teaching as compared with CS lectures that I've taken.
Im going to throw my 2 cents on the topic. I think the right question is not "What is iterator?" but "Why iterator ?" . Using for (or while ) loop(s) using indexing is actually random access order regardless that we (programmers) always ( like 99% of the time ) use the said for indexing only in one direction ... iterators are only sequential access order and can be used only as such ... in theory compiler may be able to generate faster code if using for iterator loop vs for index loop ... and that;s it about iterators ... implementation of iterators is next topic ... but again I would argue the more important is WHY vs WHAT
Iterators become way more important when using range checks as the compiler can't always proof the size of a range but ehen using iterators they can most certainly be eliminated
Hello! Great video! I think it would be interesting for you to make a video about how to implement a custom iterator for a custom STL compatible data structure (may be with some fancy algorithm). In the video you could touch something about template stuff and all these "using value_type = ", etc. Good luck! Looking forward to the new video!
Is it also true that the iterator reduces the number of implementations in the algorithm library? I heard it allows the algorithms to traverse on any container type.
Hey Cherno. Just want to leave some feedback to your videos:
You are the BEST!!! Thanks a lot for your effort. You make it so easy to learn C++ and you are such a great character with always perfect hair :D!
speaking of c++03 we actually currently using c++98 and i disagree it's not a shame at all instead a wise decision , first starting from c++11 things really got complicated and if you don't suffer from some bottlenecks in older versions you'll find a hard time understand some of modern c++ features. it's always good to start with the basics.
Wish I knew about this 2 months ago. I had to write a compiler for class that used this everywhere. Normal when I write code, I already feel like a monkey on a typewriter trying to recreate Shakespeare. But after watching this, I think I need to burn and bury my code to atone for the shame.
Iterators are used extensively in stl algorithms library.
auto is amazing
I was just having a problem with this, thank you so much!
Great explanation, as always!
Waiting for the next episode!
I fuckin love you. I was just looking for iterators and didn't find your video about it. Seriously, without you I would not have become as good as I am right now thank god. If I could sub to your patrion I would. Good luck with your work boi!!
nice work. As a student,i think that analyzing comlicated STL template design would be quite interesting.
Awesome overview! keep up the good work!
At 12:00, you can also use
for(auto it = map.cbegin(); it != map.cend(); std::advance(it, 1)) {...}
Nicely done! Thank you.
Thank you. I learned a lot by watching your channel
I'm looking for this tutorial! Thank you for making this!
I am going over this area right now at university. So this is an especially useful tutorial.
Time warp - I remember trying to code a card game in Turbo Pascal during computer science in the year 1986/87 I believe. That was not easy at all.
abi çok sağ ol çok yardımcı oldu ama iteratorlar pek olmamış
You made my day and just got a new subscriber
Very nice video! As a junior developer it helps me understand my colleagues code xD
Please make a video of making a c++ class that's iterable and define all the required methods. Thanks
cherno literally saving me through online school
That's exactly the video I was waiting for !
Thank you so much for this video
I love your videos Cherno. I have a question
Why don’t they rename “first” and “second” to “key” and “value” respectively?
We can then have a cleaner code like “it->key” and “it->value” ? 12:49
Well this kind of answered at 14:27
10:47
My man, rhyming.
"I wanna iterate through it,
How would I do it?"
3:30 STARTS..
This guy is saving my unilife
Please, make a vídeo about Vector vs List arrays. Thanks for sharing your knowledge with us.
It depends on what you need to use them for.
Vectors:
+Good at sequential access and random access
+Stored as a block in memory so not much cache misses
-Not good at changing the size of the arrays unless you use the .reserve(x) function
Lists:
+Adding elements doesn't take much time unlike Vectors
-Takes a long time to go through the list since the elements aren't stored directly next to each other in memory (cache misses)
That's all I can think of right now, I hope I helped.
@@AraAra064 Thanks a lot. Another question, list is better than vector to sort data, right? I'm developing a Heuristic, and I need sort a lot of data, delete and create. I think list is the obvious choice.
@@victorhugomagalhaes6370 If you were just sorting, I'd go with vector. But since you need to delete and create stuff, list should be fine. I'd probably test to see which one is faster just in case because I'm not exactly a pro at C++.
@@victorhugomagalhaes6370 It's not so easy to know in advance if list or vector is better even when you work with lots of data and you need to delete and stuff. So as AraAra said testing both alternatives is the way to go if it really matters, e.g. for performance. It depends a lot on what your data actually is and how well it fits into caches, etc.
I would recommend to start with what feels most natural to use for your data (probably list if your data sets are large and you really sort/delete/create a lot) and not worry much about alternatives until you really need an alternative.
Because even if vector would be required performance wise (for large data sets) it would be better in many cases only if you take a few other measures like properly making your data cache friendly (e.g. aligning data) and for a real-time application you would even need to do exotic stuff like controlling releasing of memory. And then things get a lot of work...
In my university we got taught Pascal. Never touched C++, but we learned Pascal... yeah not a single class of C++, because, you know... PASCAL
For school I have to compile in C++ 98 😅 Really useful stuff! But i'll remember this video after my class.
Thank you for still making programming tutorials (and hopefully more gfx!). I was afraid you wouldn’t after your reaction super stardom!
Thanks, very informative!
yup, My Uni is using C++03 if im not mistaken, and I teach my friend to AT LEAST C++11
why do u use auto&key = it.first
like it can be simple auto key = it.first
y use the '&' sign ?? please explain
same question, does it try to avoid copy?
He's making a reference to the value of the members of the std::pair pointed by it.
This is a way to use more convenient names instead of the first and second of std::pair.
On modern C++ you can use this syntax: auto&[key, value] = *it;. Its the same, but cleaner
auto& is auto reference, don't copy the value, auto on the other hand will if the value itself is not reference or pointer. If you don't want modifying the value of auto& referenced to, it's better to add const to it like this: auto const &key = kv.first;
Becuz sometime copying is costly (eg std::string) so he wanna avoid it by using ref
Thematically next would be ranges.
Yes! I've been waiting for iterators !
Hey @TheCherno thanks for another great video! Can you also please make a video on different containers, and algorithms in c++, which are most commonly used.
std::vector values = { 1, 2, 3, 4, 5};
error C2552: 'values' : non-aggregates cannot be initialized with initializer list.
I'm beginning to dislike Visual Studio (2008 ). Maybe a newer version would work, but I am working on a program targeting Windows XP.
Congratulations for channel. Video is much fine. Thank you for teaching. Peace and Health
10:45
Cherno: I don't use CPP, CPP uses me.
Using C++ 03? I'm glad I didn't waste money going to university... Though I'm pretty sure PSU/UOP is pretty advanced given that Portland has multiple tech companies in it.
Thanks. I Was Looking For This
This was brilliant.
iterators are cool and all, but what actually are they? im assuming theyre not integers, but that makes me curious and excited to figure out how to actually make one!
Thank you
turn off the auto focus on your camera when you are using a tripod or the camera is steady on it's own. it helps with that random focus loss problem
thanks for the video. About the last two methods, is there versions of them to iterate to a certain position of the map and not to the full range of it?
which book do you recommend for learning intermediate to advanced c++
Generally don't use books when it comes to programming languages only up-to-date documentation / tutorials etc. You can get frustrated fast if you try to solve problem, that doesn't exist in the current language version.
However there is one worth to mention c++ book I know for intermediate "C++ Reactive Programming" by Peter Abraham it's generally about available in many languages library called RX here rxcpp. It really helps when you handle multiple asynchronous actions and with no blocking and lazy code you want to solve the problem.
How does kv in "for(auto kv : map)" handle the string? Does it make some sort of reference to it or does it make a copy? Because I've seen people use both auto& and auto&& as well and I'm wondering about the difference.
Question is old... but for future reference:
- for(auto kv : map) makes copies of the values (which is fine for very simple types)
- for references you would use auto& (and you would typically prefer to use this)
- auto&& does not make sense in such a simple example (besides creating confusion) as it results in the same as auto& (so technically it works). It is a more advanced form of references and is used in a few different contexts, the main being used with rvalues (for moving objects instead of copying, or for types which are rvalues like std::vector because they are proxy types) or they are used as so-called forwarding or universal references (which generically work both with rvalues and lvalues, that's why in this simple example it results in the same as auto&). You often see auto&& as parameters for generic lambdas.