1 00:00:00,000 --> 00:00:10,000 2 00:00:10,000 --> 00:00:11,000 3 00:00:11,000 --> 00:00:15,000 This presentation is delivered by the Stanford Center for Professional 4 00:00:15,000 --> 00:00:25,000 Development. 5 00:00:25,000 --> 00:00:29,000 I'm guessing by the fact that I can see myself that we're on. 6 00:00:29,000 --> 00:00:33,000 Welcome back to CS106L. My name's Keith. I'm a section 7 00:00:33,000 --> 00:00:37,000 leader here. I teach CS106L, which is the standard C++ program and 8 00:00:37,000 --> 00:00:37,000 laboratory. 9 00:00:37,000 --> 00:00:39,000 I recognize a few people here, which 10 00:00:39,000 --> 00:00:40,000 is great. 11 00:00:40,000 --> 00:00:44,000 Julie wanted me to come in today and talk to you about the C++ 12 00:00:44,000 --> 00:00:47,000 programming language, what's it look like outside of CS106B? 13 00:00:47,000 --> 00:00:50,000 What sorts of things do you need to be aware of to just kind of give you a general 14 00:00:50,000 --> 00:00:53,000 picture of what this language looks like? 15 00:00:53,000 --> 00:00:56,000 Before I get into the actual C++ stuff, I wanted to start off by 16 00:00:56,000 --> 00:00:59,000 congratulating all of you for making it through this class. That's not a 17 00:00:59,000 --> 00:01:01,000 small accomplishment. 18 00:01:01,000 --> 00:01:04,000 If you think about when you started, day one, probably looking at this screen right here, probably 19 00:01:04,000 --> 00:01:05,000 thinking, 20 00:01:05,000 --> 00:01:08,000 what's that [inaudible], what's Genlib, what's C [inaudible], and why's it 21 00:01:08,000 --> 00:01:11,000 really, really less than something? 22 00:01:11,000 --> 00:01:12,000 Look where you are now. 23 00:01:12,000 --> 00:01:16,000 You now know how to be a client of the vector, the stack, the map, the queue 24 00:01:16,000 --> 00:01:17,000 and the set. 25 00:01:17,000 --> 00:01:20,000 You know that recursion, you know pointers, you know Linklis and binary 26 00:01:20,000 --> 00:01:21,000 [inaudible] and graphs. 27 00:01:21,000 --> 00:01:25,000 You know algorithmic analysis and graph algorithms and searching and sorting. 28 00:01:25,000 --> 00:01:26,000 You know how the lexicon works. 29 00:01:26,000 --> 00:01:29,000 You know how to implement all of these classes. You know data abstraction. 30 00:01:29,000 --> 00:01:34,000 That's not a small accomplishment. That's really something to be proud of. 31 00:01:34,000 --> 00:01:37,000 What I wanted to say is that these are real, practical tools that will follow 32 00:01:37,000 --> 00:01:39,000 you, no matter where you take them. 33 00:01:39,000 --> 00:01:43,000 Some of you might go on in CS. You might think, wow, I really like this class. I want to 34 00:01:43,000 --> 00:01:45,000 see where it goes. That's great. 35 00:01:45,000 --> 00:01:48,000 No matter where you take it, if you go and take algorithms, you take 36 00:01:48,000 --> 00:01:52,000 compliers or data bases or operating systems, the skills you've learned here is really 37 00:01:52,000 --> 00:01:54,000 going to be the foundation 38 00:01:54,000 --> 00:01:57,000 of all of your programing. You're always going to be using maps. You're always 39 00:01:57,000 --> 00:02:00,000 going to be using vectors. You're going to need recursion, no matter where you 40 00:02:00,000 --> 00:02:01,000 apply it. 41 00:02:01,000 --> 00:02:04,000 It's really wonderful that you have these skills. 42 00:02:04,000 --> 00:02:06,000 Of course, some of you aren't going to go on in computer science. That's totally fine. You 43 00:02:06,000 --> 00:02:11,000 took this class and said, it's not for me or, you know, maybe not. Well, 44 00:02:11,000 --> 00:02:12,000 that's fine too because 45 00:02:12,000 --> 00:02:15,000 whether you go into sociology or philosophy or psychology or math or 46 00:02:15,000 --> 00:02:16,000 physics or chemistry, 47 00:02:16,000 --> 00:02:18,000 there are problems to be solved, 48 00:02:18,000 --> 00:02:20,000 and there are problems you can solve with a computer. 49 00:02:20,000 --> 00:02:23,000 What you've learned in this class is the most valuable skill of them all, which 50 00:02:23,000 --> 00:02:25,000 is how to take a problem, 51 00:02:25,000 --> 00:02:26,000 model it 52 00:02:26,000 --> 00:02:27,000 and solve it. 53 00:02:27,000 --> 00:02:29,000 That really is something. 54 00:02:29,000 --> 00:02:32,000 The skills you've learned here transcend any specific programming language. 55 00:02:32,000 --> 00:02:36,000 You can do everything you've done in here, in Java, in C++, in PHP, 56 00:02:36,000 --> 00:02:41,000 in Python. Everything that you think of, you will be using these same tools. 57 00:02:41,000 --> 00:02:42,000 That said, 58 00:02:42,000 --> 00:02:46,000 we've taught you everything in this class using the C++ programming 59 00:02:46,000 --> 00:02:46,000 language. 60 00:02:46,000 --> 00:02:48,000 We use C++ 61 00:02:48,000 --> 00:02:52,000 because it's very good for expressing the concepts that we were going over. It 62 00:02:52,000 --> 00:02:56,000 has great support for recursion, has exposed pointers, so you can do things like 63 00:02:56,000 --> 00:02:58,000 Linklis and binary trees quite nicely. 64 00:02:58,000 --> 00:03:01,000 It has objects so you can do data abstraction. It's got templates. It's a very 65 00:03:01,000 --> 00:03:05,000 good mix of different programing strategies and different programming styles 66 00:03:05,000 --> 00:03:07,000 that means we can teach you things without having to bog you down in 67 00:03:07,000 --> 00:03:09,000 language syntax. 68 00:03:09,000 --> 00:03:11,000 That said, this really hasn't been much of a class 69 00:03:11,000 --> 00:03:13,000 in C++. 70 00:03:13,000 --> 00:03:16,000 Think of it was we all put you inside the C++ bus and drove 71 00:03:16,000 --> 00:03:19,000 you up to the top of the mountain of CS106B knowledge. 72 00:03:19,000 --> 00:03:21,000 You know that the bus got you up there, 73 00:03:21,000 --> 00:03:24,000 but you don't really know what's going on under the hood. 74 00:03:24,000 --> 00:03:27,000 That's okay because what you learn in this class is more important than that. 75 00:03:27,000 --> 00:03:30,000 All the programing language and knowledge in the universe is not going to help you 76 00:03:30,000 --> 00:03:33,000 solve a problem if you don't know how to use a vector, you don't know how to make a 77 00:03:33,000 --> 00:03:36,000 map, and you can't make a recursive function solve things. 78 00:03:36,000 --> 00:03:40,000 What I want to do today is talk about what the C++ language is. What's 79 00:03:40,000 --> 00:03:42,000 it look like the real world? 80 00:03:42,000 --> 00:03:43,000 What kind of stuff do you need to know? 81 00:03:43,000 --> 00:03:46,000 More importantly, how do you take these skills that you've learned, which are very 82 00:03:46,000 --> 00:03:50,000 generic, and go and apply them in this language. 83 00:03:50,000 --> 00:03:53,000 I love C++. I've been using it for years. I think it's a beautiful 84 00:03:53,000 --> 00:03:56,000 language. It's got support for so many different programing paradigms. It's 85 00:03:56,000 --> 00:03:59,000 easy to use once you get a handle on it. 86 00:03:59,000 --> 00:04:01,000 It lets you solve problems in so many different ways. 87 00:04:01,000 --> 00:04:04,000 Really, I just want to show you what it looks like. 88 00:04:04,000 --> 00:04:07,000 Think of this as an appetizer. I'm just going to give you the - this 89 00:04:07,000 --> 00:04:10,000 is the C++ appetizer plate. I'll show you a little bit of this, a 90 00:04:10,000 --> 00:04:11,000 little bit of that, 91 00:04:11,000 --> 00:04:15,000 give you enough working knowledge that you know where to go to get help. 92 00:04:15,000 --> 00:04:16,000 You can see 93 00:04:16,000 --> 00:04:18,000 this is what you need to learn, get 94 00:04:18,000 --> 00:04:21,000 an overview of what we've been doing, what we've provided you, what we haven't 95 00:04:21,000 --> 00:04:22,000 provided you, 96 00:04:22,000 --> 00:04:25,000 so that by the time you're done, you can think, 97 00:04:25,000 --> 00:04:27,000 maybe I want to go on in this language. 98 00:04:27,000 --> 00:04:30,000 I'm not going to force you to learn C++. I can't do that. I'm 99 00:04:30,000 --> 00:04:33,000 not going to follow you around, did you learn C++ yet? Did you learn C++ yet? 100 00:04:33,000 --> 00:04:35,000 No, I'm not going to do that. 101 00:04:35,000 --> 00:04:38,000 You guys are all competent programmers right now. 102 00:04:38,000 --> 00:04:41,000 You know how to solve these problems. You know how to take on these challenges. 103 00:04:41,000 --> 00:04:43,000 So I just want to show you the language so you can decide whether or not you 104 00:04:43,000 --> 00:04:44,000 want to pursue it. 105 00:04:44,000 --> 00:04:47,000 Maybe you will. That'd be great. If not, 106 00:04:47,000 --> 00:04:48,000 that's okay, too, 107 00:04:48,000 --> 00:04:51,000 because you know how to tackle problems, and all you need now is a language to 108 00:04:51,000 --> 00:04:53,000 do it 109 00:04:53,000 --> 00:04:56,000 in. I want to show you basically four aspects of C++. First 110 00:04:56,000 --> 00:05:00,000 some philosophy and some history because every language has a personality. 111 00:05:00,000 --> 00:05:03,000 Every language wants to treat you as a programmer a different way, give you some 112 00:05:03,000 --> 00:05:05,000 different options. So I just want to show you what to expect when 113 00:05:05,000 --> 00:05:07,000 you're working with C++. 114 00:05:07,000 --> 00:05:10,000 As a language, it has a lot of criticisms. How many of you have heard 115 00:05:10,000 --> 00:05:15,000 bad things about C++ in any way, shape or form? 116 00:05:15,000 --> 00:05:18,000 Wow, every, single person in this room, basically. 117 00:05:18,000 --> 00:05:21,000 So there's a lot of criticisms leveled at this language, and what I want to 118 00:05:21,000 --> 00:05:22,000 do 119 00:05:22,000 --> 00:05:25,000 is show you what the mindset is so you can take a look at these 120 00:05:25,000 --> 00:05:28,000 criticisms and evaluate them. In all seriousness, most of the 121 00:05:28,000 --> 00:05:30,000 criticisms leveled at this language are 122 00:05:30,000 --> 00:05:33,000 criticisms with things a language doesn't try to do. 123 00:05:33,000 --> 00:05:37,000 At the same time, if you get this philosophy and you get this history, you'll 124 00:05:37,000 --> 00:05:38,000 understand where this language came from, 125 00:05:38,000 --> 00:05:41,000 what it's designed to do and where it's going. 126 00:05:41,000 --> 00:05:44,000 The second thing I want to talk about is the actual mechanics. 127 00:05:44,000 --> 00:05:47,000 What does the programing language look like? So I'll talk about the 128 00:05:47,000 --> 00:05:47,000 libraries, and 129 00:05:47,000 --> 00:05:49,000 I'll talk about the core language features. 130 00:05:49,000 --> 00:05:52,000 What happens when you take away things like [inaudible] and Synfi? What are you going to have 131 00:05:52,000 --> 00:05:54,000 to do now that you don't have those? In 132 00:05:54,000 --> 00:05:58,000 terms of it's actually pretty straightforward and you already know most of it. 133 00:05:58,000 --> 00:06:00,000 Then some language features that we didn't cover, things that you would expect to 134 00:06:00,000 --> 00:06:02,000 see in a professional setting 135 00:06:02,000 --> 00:06:05,000 that we didn't go over in this class because it's needlessly complicated and 136 00:06:05,000 --> 00:06:08,000 doesn't have much to do with this general data structure and 137 00:06:08,000 --> 00:06:10,000 algorithm challenges we've been giving you. 138 00:06:10,000 --> 00:06:12,000 Finally, just some references. 139 00:06:12,000 --> 00:06:14,000 Where do you go? 140 00:06:14,000 --> 00:06:17,000 You want to learn C++? Great. We have a list of wonderful books and resources 141 00:06:17,000 --> 00:06:18,000 you can go and reference, 142 00:06:18,000 --> 00:06:20,000 and it can get you up to speed very quickly. 143 00:06:20,000 --> 00:06:22,000 So it's kind of a game plan. 144 00:06:22,000 --> 00:06:23,000 See what's out there, 145 00:06:23,000 --> 00:06:28,000 have some fun with it and learn where to go. Sound good? Okay. 146 00:06:28,000 --> 00:06:29,000 So I want to start off 147 00:06:29,000 --> 00:06:32,000 with a quick history of C++. 148 00:06:32,000 --> 00:06:34,000 So C++ did not come into being one day when a whole bunch of programmers came 149 00:06:34,000 --> 00:06:37,000 into a room, made some demonic incantations over a bubbling cauldron and 150 00:06:37,000 --> 00:06:39,000 walked out. It didn't happen. 151 00:06:39,000 --> 00:06:42,000 It was not invented in one day. People did not sit down and say, our language is going to look like 152 00:06:42,000 --> 00:06:44,000 this. 153 00:06:44,000 --> 00:06:45,000 It has a history. 154 00:06:45,000 --> 00:06:49,000 It has evolution, and you can see different traces of we tried doing 155 00:06:49,000 --> 00:06:51,000 this. It didn't work. 156 00:06:51,000 --> 00:06:54,000 Let's go add this feature, etc. 157 00:06:54,000 --> 00:06:56,000 It started off 158 00:06:56,000 --> 00:06:58,000 with this guy. 159 00:06:58,000 --> 00:07:02,000 So I will attempt to pronounce his name. It's Bjarne Stroustrup. 160 00:07:02,000 --> 00:07:05,000 I've been told that the way to pronounce this is to say Stroustrup with a 161 00:07:05,000 --> 00:07:10,000 Norwegian accent while cramming a potato down your throat. I'll 162 00:07:10,000 --> 00:07:10,000 just 163 00:07:10,000 --> 00:07:14,000 get it close. Anyway, he's a Danish computer scientist, and he was getting his PhD in computer 164 00:07:14,000 --> 00:07:15,000 science from Cambridge, 165 00:07:15,000 --> 00:07:19,000 and his specialty was in distributive systems and operating systems. So his goal 166 00:07:19,000 --> 00:07:22,000 was, let's take some problems, spread it out over several computers, have 167 00:07:22,000 --> 00:07:26,000 them communicate via network protocol and get some results. 168 00:07:26,000 --> 00:07:29,000 He chose to write this program in a language called Simula. I 169 00:07:29,000 --> 00:07:34,000 confess, I don't know much about this language. I haven't heard anyone using it, but 170 00:07:34,000 --> 00:07:37,000 back then, it was the his object-oriented language, and he said it helped him think 171 00:07:37,000 --> 00:07:38,000 about the problem. 172 00:07:38,000 --> 00:07:42,000 It had classes, so you could build a computer object, a protocol object, a 173 00:07:42,000 --> 00:07:43,000 network object, 174 00:07:43,000 --> 00:07:46,000 and they'd send messages to each other the same way that a 175 00:07:46,000 --> 00:07:48,000 physical computer, a physical network 176 00:07:48,000 --> 00:07:51,000 and a physical protocol would communicate with each other. He 177 00:07:51,000 --> 00:07:54,000 got it working pretty fast and ran it, 178 00:07:54,000 --> 00:07:57,000 and much to his surprise, he found out that it was so abysmally slow that he could get no 179 00:07:57,000 --> 00:07:59,000 practical results out of it. The 180 00:07:59,000 --> 00:08:01,000 question is what went wrong? 181 00:08:01,000 --> 00:08:04,000 It wasn't his program. It was the Simula language implementation. 182 00:08:04,000 --> 00:08:08,000 He actually ran a profiler on his code to figure out why it was going slowly. 183 00:08:08,000 --> 00:08:10,000 80 percent of the time his program was running, it was going automatic 184 00:08:10,000 --> 00:08:13,000 garbage collection, even though he was doing [inaudible] memory management. 185 00:08:13,000 --> 00:08:14,000 So think about this. 186 00:08:14,000 --> 00:08:17,000 You spend several weeks writing a program. 187 00:08:17,000 --> 00:08:17,000 You run it, 188 00:08:17,000 --> 00:08:20,000 and for every line of code you're writing, four lines of meaningless code 189 00:08:20,000 --> 00:08:24,000 that someone else wrote are also executing. That's really slow. So 190 00:08:24,000 --> 00:08:27,000 he had to change approaches. He went and rewrote this entire program in a 191 00:08:27,000 --> 00:08:30,000 different language called BCPL, which is related to C and the B 192 00:08:30,000 --> 00:08:33,000 programing language that came before C. 193 00:08:33,000 --> 00:08:36,000 Of course, it's very low-level, it's very fast, but it didn't have these nice 194 00:08:36,000 --> 00:08:37,000 high-level features in it. 195 00:08:37,000 --> 00:08:39,000 He got it working, but it took a lot of time. 196 00:08:39,000 --> 00:08:42,000 When it was done, he decided, I'm never going to tackle a 197 00:08:42,000 --> 00:08:46,000 problem like this again until I have a proper tool for the job. He 198 00:08:46,000 --> 00:08:49,000 ended up at AT&T Bell labs, which is the same place that the C programming language came 199 00:08:49,000 --> 00:08:53,000 out of. In fact, he knew Kernighan and Ritchie, who invented that, 200 00:08:53,000 --> 00:08:55,000 and came up with this language called C with classes. It was 201 00:08:55,000 --> 00:08:58,000 a precursor to C++, and people loved it. 202 00:08:58,000 --> 00:09:02,000 It combined the best features of Simula, which is this object-oriented design, 203 00:09:02,000 --> 00:09:04,000 with the best features of C, which is the runtime efficiency. 204 00:09:04,000 --> 00:09:06,000 It was something that was fast, 205 00:09:06,000 --> 00:09:09,000 flexible and helped you think about problems. 206 00:09:09,000 --> 00:09:11,000 What he would do is work on this implementation. 207 00:09:11,000 --> 00:09:14,000 He had people actually using the C++, and when they needed new 208 00:09:14,000 --> 00:09:15,000 features, they said, 209 00:09:15,000 --> 00:09:17,000 Bjarne, it doesn't work. He'd go fix it. 210 00:09:17,000 --> 00:09:20,000 We had this cycle of real programmers solving real problems 211 00:09:20,000 --> 00:09:23,000 with this developer saying, okay, let's go fix it. 212 00:09:23,000 --> 00:09:26,000 After a while, you ended up with C++. I think this 213 00:09:26,000 --> 00:09:28,000 is the 25th anniversary of C++, so 214 00:09:28,000 --> 00:09:30,000 how exciting. 215 00:09:30,000 --> 00:09:33,000 I want to talk about the philosophy a little bit. What is really driving the 216 00:09:33,000 --> 00:09:36,000 development of this language? What does it expect out of you? 217 00:09:36,000 --> 00:09:39,000 So I have some quotes from this book called The Design and Evolution of C++. It's 218 00:09:39,000 --> 00:09:40,000 a good book. You should read it. 219 00:09:40,000 --> 00:09:42,000 The first one, 220 00:09:42,000 --> 00:09:45,000 C++'s evolution should be driven by real problems, and you don't 221 00:09:45,000 --> 00:09:47,000 want to get down on a quest for perfection. 222 00:09:47,000 --> 00:09:51,000 Basically, this language is designed to solve real problems that 223 00:09:51,000 --> 00:09:53,000 real programmers like you are going to run into 224 00:09:53,000 --> 00:09:55,000 in not necessarily the best way. 225 00:09:55,000 --> 00:09:56,000 It tries to do a good job. 226 00:09:56,000 --> 00:10:01,000 It's not intended to be perfect. There are some sloppy edges, but it works. 227 00:10:01,000 --> 00:10:04,000 Because it is driven by real problems and real design issues, you can solve 228 00:10:04,000 --> 00:10:06,000 real problems with it. 229 00:10:06,000 --> 00:10:07,000 If you think that's a good thing in a language, 230 00:10:07,000 --> 00:10:11,000 I personally do. I think it's good to have a language that can solve problems, 231 00:10:11,000 --> 00:10:14,000 then C++ is a good choice. 232 00:10:14,000 --> 00:10:17,000 This is probably the one that you've seen the most. Don't try to force people. 233 00:10:17,000 --> 00:10:21,000 C++ gives you so many choices that your question should not be, 234 00:10:21,000 --> 00:10:24,000 how do I do this in the language, but more, which of these hundreds of 235 00:10:24,000 --> 00:10:26,000 options is the best choice for me? 236 00:10:26,000 --> 00:10:29,000 It really trusts you as a language. It will give you an incredible amount of 237 00:10:29,000 --> 00:10:33,000 flexibility, even to the point where it will let you make the wrong decision. 238 00:10:33,000 --> 00:10:36,000 I've always thought C++ is a language that will let you ride a 239 00:10:36,000 --> 00:10:37,000 bicycle without a helmet 240 00:10:37,000 --> 00:10:40,000 because that one time that you need to go under a bridge that's exactly a quarter inch over your 241 00:10:40,000 --> 00:10:43,000 head, you wont have your helmet knock you off your bike. You 242 00:10:43,000 --> 00:10:45,000 do have to worry about a lot of risks, 243 00:10:45,000 --> 00:10:48,000 but in the end, you'll have more flexibility than you'll find in most other 244 00:10:48,000 --> 00:10:50,000 languages you 245 00:10:50,000 --> 00:10:52,000 see. Finally, I think the most important one 246 00:10:52,000 --> 00:10:54,000 is this. 247 00:10:54,000 --> 00:10:56,000 C++ makes programming more enjoyable 248 00:10:56,000 --> 00:10:58,000 for serious programmers. 249 00:10:58,000 --> 00:11:01,000 The two things here are serious programmers and more enjoyable. 250 00:11:01,000 --> 00:11:04,000 This is a professional language. It's used in industry everywhere. 251 00:11:04,000 --> 00:11:05,000 It's very fast. 252 00:11:05,000 --> 00:11:08,000 It's very efficient. It has a bit of a learning curve. It is kind of 253 00:11:08,000 --> 00:11:11,000 tricky to get into the language, but once you've got it, 254 00:11:11,000 --> 00:11:13,000 the second part, the more enjoyable, is so true. 255 00:11:13,000 --> 00:11:16,000 This language is fun to write things in. 256 00:11:16,000 --> 00:11:19,000 Once you've got it down, you will actually step back from your program and say, wow, 257 00:11:19,000 --> 00:11:22,000 I just wrote something that took every, single word out of this set that I had 258 00:11:22,000 --> 00:11:24,000 [inaudible] contains a specific letter 259 00:11:24,000 --> 00:11:25,000 in a single line of code. 260 00:11:25,000 --> 00:11:29,000 Or I lettered all the contents of this file into my set in one line of code. Or I 261 00:11:29,000 --> 00:11:31,000 just wrote a template that 262 00:11:31,000 --> 00:11:34,000 is a multi-dimensional ray of any dimension I want. 263 00:11:34,000 --> 00:11:37,000 Those are not trivial accomplishments, and you can do it with 264 00:11:37,000 --> 00:11:38,000 this language. 265 00:11:38,000 --> 00:11:39,000 It's fun. 266 00:11:39,000 --> 00:11:41,000 It's a great language, and if you think that this sort of philosophy is what you 267 00:11:41,000 --> 00:11:43,000 want, where it will trust you, 268 00:11:43,000 --> 00:11:46,000 it will really let you make the decisions rather than forcing you into any one 269 00:11:46,000 --> 00:11:47,000 paradigm, 270 00:11:47,000 --> 00:11:48,000 learn C++. 271 00:11:48,000 --> 00:11:51,000 I think you'll love it. So 272 00:11:51,000 --> 00:11:55,000 philosophy. There's lots of it, but I'm not here to teach a philosophy 273 00:11:55,000 --> 00:11:58,000 class. I'm here to teach you about C++, so let's get down to some of the details. What does this 274 00:11:58,000 --> 00:12:00,000 language look like? 275 00:12:00,000 --> 00:12:02,000 The first thing I want to do is talk about 276 00:12:02,000 --> 00:12:04,000 what Genlib.H has. 277 00:12:04,000 --> 00:12:06,000 Since day one, you probably have seen 278 00:12:06,000 --> 00:12:07,000 things like this. How 279 00:12:07,000 --> 00:12:10,000 to include Genlib. Very first line, right here. 280 00:12:10,000 --> 00:12:14,000 What is in this Genlib thing? Has anybody actually looked at Genlib before? Actually, pulled up 281 00:12:14,000 --> 00:12:20,000 the Genlib file and looked inside? No one? Okay. 282 00:12:20,000 --> 00:12:22,000 If you look inside Genlib, 283 00:12:22,000 --> 00:12:24,000 it looks mostly like this. 284 00:12:24,000 --> 00:12:27,000 There are basically three important lines you need to know. 285 00:12:27,000 --> 00:12:28,000 Time includes string. 286 00:12:28,000 --> 00:12:31,000 So we talked to you about the string classes that was a built-in type, like Ent or 287 00:12:31,000 --> 00:12:35,000 double. It's a class. It's like any other object, like a vector or a map. You do need 288 00:12:35,000 --> 00:12:36,000 to pound include it. 289 00:12:36,000 --> 00:12:39,000 We just took care of it for you because since you use it everywhere and it's easy to 290 00:12:39,000 --> 00:12:42,000 forget and you can get some pretty nasty compiler errors if you don't, 291 00:12:42,000 --> 00:12:44,000 we thought, 292 00:12:44,000 --> 00:12:47,000 we'll be nice. We'll do that for you. 293 00:12:47,000 --> 00:12:48,000 The second thing is this error function. 294 00:12:48,000 --> 00:12:52,000 You've seen error. Genlib just gives you a prototype for it. The end. 295 00:12:52,000 --> 00:12:56,000 But this last one right here using name space STD. Using the standard 296 00:12:56,000 --> 00:12:58,000 name space. What on 297 00:12:58,000 --> 00:13:00,000 earth is this line? 298 00:13:00,000 --> 00:13:03,000 It looks almost like an English sentence. 299 00:13:03,000 --> 00:13:05,000 Compiler, make my code work. 300 00:13:05,000 --> 00:13:07,000 What's going on here? 301 00:13:07,000 --> 00:13:10,000 Well, I'll show you. 302 00:13:10,000 --> 00:13:14,000 Suppose I have this little program back here. Can anybody see this? So I want to pound include [inaudible] 303 00:13:14,000 --> 00:13:17,000 and pound include string. 304 00:13:17,000 --> 00:13:19,000 If you notice here, I don't have Genlib, and I'm going to make 305 00:13:19,000 --> 00:13:22,000 a string and print it out. 306 00:13:22,000 --> 00:13:26,000 So the question is, when I say string, my string, 307 00:13:26,000 --> 00:13:28,000 what am I referring to? 308 00:13:28,000 --> 00:13:31,000 Well, when this happens, the compiler says, well, go look for something called 309 00:13:31,000 --> 00:13:32,000 string. 310 00:13:32,000 --> 00:13:34,000 When we pound included string, 311 00:13:34,000 --> 00:13:38,000 it put it inside this big bucket of something called name space standard. 312 00:13:38,000 --> 00:13:41,000 The idea is that the real name of string is standard string. The real name of C out 313 00:13:41,000 --> 00:13:44,000 is standard C out. The real name of endal is standard endal. 314 00:13:44,000 --> 00:13:47,000 It's just so that if you make your own versions of those things, it doesn't have 315 00:13:47,000 --> 00:13:51,000 the same name as the standard. It won't conflict, and your program will still compile. 316 00:13:51,000 --> 00:13:54,000 But the problem is that if you try to run this, what 317 00:13:54,000 --> 00:13:55,000 will happen is that 318 00:13:55,000 --> 00:13:56,000 the compiler says string, 319 00:13:56,000 --> 00:13:59,000 looks up here and hits this wall. It 320 00:13:59,000 --> 00:14:03,000 says, oops, that's inside the standard name space. I can't go in there, 321 00:14:03,000 --> 00:14:06,000 and you'll get this really nasty compiler error, something like, string's not defined. C out's not defined. 322 00:14:06,000 --> 00:14:08,000 Endal's not defined. 323 00:14:08,000 --> 00:14:12,000 Now, most of the time when you're programming, you want things like C out and 324 00:14:12,000 --> 00:14:14,000 string to actually exist. 325 00:14:14,000 --> 00:14:17,000 So rather than saying you have to call it standard string, standard C out, standard 326 00:14:17,000 --> 00:14:19,000 endal, 327 00:14:19,000 --> 00:14:22,000 you can introduce this little phrase right here, 328 00:14:22,000 --> 00:14:24,000 using name space standard. What it says 329 00:14:24,000 --> 00:14:26,000 is take this wall and get rid of it. 330 00:14:26,000 --> 00:14:29,000 It's like, Mr. Compiler, tear down this wall, 331 00:14:29,000 --> 00:14:30,000 and the result 332 00:14:30,000 --> 00:14:34,000 is that this barrier gets a little bit 333 00:14:34,000 --> 00:14:34,000 transparent. 334 00:14:34,000 --> 00:14:38,000 It's all still included inside of the standard name 335 00:14:38,000 --> 00:14:39,000 space, but now it's accessible. 336 00:14:39,000 --> 00:14:42,000 So when you go, string goes, oh, yeah, that thing, and 337 00:14:42,000 --> 00:14:44,000 actually compiles. 338 00:14:44,000 --> 00:14:46,000 Now most of today, I'm not going to be hitting you with C++ and saying, 339 00:14:46,000 --> 00:14:49,000 you must know this. You must know this. There's just not enough time to 340 00:14:49,000 --> 00:14:50,000 go over everything, 341 00:14:50,000 --> 00:14:53,000 but this little line might be something worth committing to memory because it's 342 00:14:53,000 --> 00:14:56,000 the most noticeable change you'll see when you stop using Genlib. 343 00:14:56,000 --> 00:14:59,000 If you don't put that line in your code, you'll get lots of compiler errors, and it will 344 00:14:59,000 --> 00:15:00,000 just scare you. 345 00:15:00,000 --> 00:15:02,000 So if you put this line in, 346 00:15:02,000 --> 00:15:06,000 I'd say 90 percent of your compiler problems will go away. 347 00:15:06,000 --> 00:15:07,000 That's that line. 348 00:15:07,000 --> 00:15:10,000 That's Genlib, and if you want to take a look, 349 00:15:10,000 --> 00:15:13,000 if you right this code, you basically replace all of 350 00:15:13,000 --> 00:15:16,000 Genlib. The first header file you'll have to include is this one, CSTDLIB, the C 351 00:15:16,000 --> 00:15:18,000 standard library. 352 00:15:18,000 --> 00:15:22,000 I'm thinking that when the invented the C programing language, it cost them 353 00:15:22,000 --> 00:15:26,000 several million dollars per consonant or vowel they put in their header files. 354 00:15:26,000 --> 00:15:30,000 So they put it as small as possible but still readable. The idea 355 00:15:30,000 --> 00:15:32,000 is that you have these two lines. 356 00:15:32,000 --> 00:15:33,000 Pound include string 357 00:15:33,000 --> 00:15:35,000 using the standard name 358 00:15:35,000 --> 00:15:38,000 space. Then right here in this error function, you just 359 00:15:38,000 --> 00:15:41,000 print everything to C error, which is like C out. It's just designed for error 360 00:15:41,000 --> 00:15:45,000 handling. You call exit minus one, which says, quit this program. Report some error to 361 00:15:45,000 --> 00:15:49,000 the operating system and we're done. 362 00:15:49,000 --> 00:15:52,000 This is it. This is all that is contained in Genlib, and 363 00:15:52,000 --> 00:15:54,000 the only two important lines are these two right here. 364 00:15:54,000 --> 00:15:57,000 Including the string class and using the standard name space. If you do 365 00:15:57,000 --> 00:15:58,000 that, 366 00:15:58,000 --> 00:16:00,000 and you get [inaudible] Genlib, 367 00:16:00,000 --> 00:16:03,000 you're going to be in perfectly good shape. 368 00:16:03,000 --> 00:16:06,000 Now, Genlib is probably one of the easier ones, but what about some of these other 369 00:16:06,000 --> 00:16:08,000 headers you've seen? 370 00:16:08,000 --> 00:16:11,000 I could go into some detail about all of these, but I don't have 371 00:16:11,000 --> 00:16:14,000 time. I really wish I could show you how these work, 372 00:16:14,000 --> 00:16:17,000 but I'm going to give you a quick overview today 373 00:16:17,000 --> 00:16:19,000 to show you what you'll need to know if you want to get off these 374 00:16:19,000 --> 00:16:21,000 libraries. 375 00:16:21,000 --> 00:16:25,000 The first one is [inaudible]. This is the one that gives you string to integer, 376 00:16:25,000 --> 00:16:30,000 integer to string, string to reel, convert to uppercase, convert to lowercase, etc. 377 00:16:30,000 --> 00:16:32,000 Behind the scenes, most of this stuff is backed by this class called a string stream. You've seen 378 00:16:32,000 --> 00:16:33,000 [inaudible] 379 00:16:33,000 --> 00:16:37,000 streams, you've seen - they're all streams. You can do 380 00:16:37,000 --> 00:16:39,000 less than, you can do greater than. 381 00:16:39,000 --> 00:16:42,000 The point of the string stream, it does the same thing, except that 382 00:16:42,000 --> 00:16:44,000 it writes to a string buffer. 383 00:16:44,000 --> 00:16:48,000 So you can take some integer, dump it into a string, that's your integer to 384 00:16:48,000 --> 00:16:49,000 string function right there. 385 00:16:49,000 --> 00:16:52,000 There's a couple other things you can do with it. We use it for doing 386 00:16:52,000 --> 00:16:52,000 387 00:16:52,000 --> 00:16:56,000 conversion between strings in other data types like integers. 388 00:16:56,000 --> 00:16:59,000 So you might want to look into that if you want to play around with it. 389 00:16:59,000 --> 00:17:01,000 As for convert to upper or lower case, 390 00:17:01,000 --> 00:17:04,000 there are these functions, two upper and two lower, that take individual characters 391 00:17:04,000 --> 00:17:07,000 and convert them to upper or lower case. Just 392 00:17:07,000 --> 00:17:10,000 fore loop over your string. Call that function every time. 393 00:17:10,000 --> 00:17:13,000 Presto. Your string is in uppercase. That was pretty straightforward. 394 00:17:13,000 --> 00:17:16,000 If you're interested, in CS106L, we actually wrote some of these 395 00:17:16,000 --> 00:17:20,000 functions. If you want to have a working implementation of [inaudible], if you go to 396 00:17:20,000 --> 00:17:24,000 the CS106L web site, under the code section, there is a working implementation 397 00:17:24,000 --> 00:17:26,000 there. So if you want to keep using these things, 398 00:17:26,000 --> 00:17:28,000 just go download that file, take a look at it, 399 00:17:28,000 --> 00:17:31,000 play around with it, and it should work. 400 00:17:31,000 --> 00:17:32,000 So 401 00:17:32,000 --> 00:17:34,000 if you wanted to set up a 402 00:17:34,000 --> 00:17:36,000 coding environment on some 403 00:17:36,000 --> 00:17:38,000 Linux machine that 404 00:17:38,000 --> 00:17:41,000 doesn't have any of the standard PC or Mac startup files, 405 00:17:41,000 --> 00:17:46,000 we can just use that? Yeah, it uses only standard C++, so it will compile everywhere. 406 00:17:46,000 --> 00:17:49,000 It works pretty well. I think we only defined a few of them, but you 407 00:17:49,000 --> 00:17:51,000 can see how they work, and you can define it for 408 00:17:51,000 --> 00:17:55,000 - you can do string to integer, string to double or string to reel pretty easily just be changing a 409 00:17:55,000 --> 00:18:00,000 couple types around. Okay. 410 00:18:00,000 --> 00:18:03,000 This is probably the big one you're going to miss. Sympio.H. 411 00:18:03,000 --> 00:18:09,000 How many of you have ever played around with raw C++ input functions before? 412 00:18:09,000 --> 00:18:12,000 They're kind of nasty. I've always thought this thing called C in, which 413 00:18:12,000 --> 00:18:15,000 is the counterpart of C out that does input, is kind of like a rose. It's 414 00:18:15,000 --> 00:18:19,000 very sweet. It's very delicate, but the second you break it, it stabs you with a thorn, 415 00:18:19,000 --> 00:18:22,000 and it hurts a lot. 416 00:18:22,000 --> 00:18:25,000 Really, you have to think of this as probably one of the most powerful one of 417 00:18:25,000 --> 00:18:29,000 the hardest-to-use input libraries out of any programing language you'll see. 418 00:18:29,000 --> 00:18:32,000 We gave you this Sympio library just to take care of all these things for you so 419 00:18:32,000 --> 00:18:34,000 you don't have to worry about 420 00:18:34,000 --> 00:18:36,000 it. 421 00:18:36,000 --> 00:18:39,000 Everybody remember get line? You can use it to get a line out of a file? 422 00:18:39,000 --> 00:18:43,000 You can use get line on this C in stream to do input reading. 423 00:18:43,000 --> 00:18:44,000 Real line as text 424 00:18:44,000 --> 00:18:46,000 uses string stream to convert it to an integer, 425 00:18:46,000 --> 00:18:49,000 plus or minus a couple extra things. That's get integer. 426 00:18:49,000 --> 00:18:52,000 Again, if you go to the CS106L web site, there is a working implementation of 427 00:18:52,000 --> 00:18:53,000 this. 428 00:18:53,000 --> 00:18:56,000 If you do want to consider doing C++ beyond this, it actually is 429 00:18:56,000 --> 00:19:00,000 a pretty good set of input functions. You'd be surprised how many people don't 430 00:19:00,000 --> 00:19:01,000 actually know how to write these things. 431 00:19:01,000 --> 00:19:04,000 Go fire it up. Have some fun, 432 00:19:04,000 --> 00:19:05,000 see what they do and 433 00:19:05,000 --> 00:19:07,000 that way, you continue using the coding conventions that you've seen 434 00:19:07,000 --> 00:19:12,000 but by only using standard C++. What about 435 00:19:12,000 --> 00:19:14,000 random? 436 00:19:14,000 --> 00:19:17,000 The random functions in C++ are 437 00:19:17,000 --> 00:19:21,000 pretty simple. There's two functions, rand and Crand. 438 00:19:21,000 --> 00:19:23,000 Random and C, the randomizer. Again, 439 00:19:23,000 --> 00:19:27,000 consonants are expensive, vowels are expensive. 440 00:19:27,000 --> 00:19:30,000 Rand gives you random integers in the range zero 441 00:19:30,000 --> 00:19:31,000 to rand max. 442 00:19:31,000 --> 00:19:34,000 So if you want to get a random double, you want to get a random integer, a random Goulian, 443 00:19:34,000 --> 00:19:39,000 just take the number in that range, scale it down to zero one, scale it back up, translate it, 444 00:19:39,000 --> 00:19:40,000 etc. 445 00:19:40,000 --> 00:19:42,000 It turns out that this is probably the easiest one to rewrite from scratch. You just have to 446 00:19:42,000 --> 00:19:46,000 be a little bit careful of how you do our bounce checking, but it's not so bad. 447 00:19:46,000 --> 00:19:49,000 The header file for that is C standard library, but you can probably build 448 00:19:49,000 --> 00:19:53,000 this one up from scratch pretty simply. 449 00:19:53,000 --> 00:19:56,000 The one that you actually are going to be missing is this one. 450 00:19:56,000 --> 00:19:58,000 Graphics.H and extended graphics. 451 00:19:58,000 --> 00:20:02,000 Unfortunately, C++ does not have a standard graphics library. It 452 00:20:02,000 --> 00:20:05,000 just doesn't exist. There's a couple reasons. One, it's very hard to 453 00:20:05,000 --> 00:20:08,000 standardize graphics. You have to make sure that it's something that works on every, 454 00:20:08,000 --> 00:20:10,000 single platform, which is very hard to do. 455 00:20:10,000 --> 00:20:13,000 Also, if you're writing C++ code from a microprocessor where 456 00:20:13,000 --> 00:20:16,000 you don't have graphics, it would be difficult to support the 457 00:20:16,000 --> 00:20:18,000 library. 458 00:20:18,000 --> 00:20:21,000 So there's no standard C++ equivalent, but I bet you this program 459 00:20:21,000 --> 00:20:24,000 that I'm using right now is written in C++. It's got some kind of 460 00:20:24,000 --> 00:20:25,000 graphics. 461 00:20:25,000 --> 00:20:27,000 Most of this is from third-party libraries. I do 462 00:20:27,000 --> 00:20:29,000 some Windows programing, so I use - for the Win32 API, which does some 463 00:20:29,000 --> 00:20:31,000 graphics stuff. 464 00:20:31,000 --> 00:20:35,000 There's a bunch of cross-platform ones like open GL. You can do X Window system. 465 00:20:35,000 --> 00:20:36,000 I think Mac's is called Carbon. I 466 00:20:36,000 --> 00:20:38,000 might be wrong about that. 467 00:20:38,000 --> 00:20:41,000 [Crosstalk] Yeah, 468 00:20:41,000 --> 00:20:44,000 there's a lot, and you won't have trouble finding it. You just won't 469 00:20:44,000 --> 00:20:47,000 have a standard library that does it for you. So shop around with this. The 470 00:20:47,000 --> 00:20:48,000 471 00:20:48,000 --> 00:20:50,000 ones we give you are pretty nice. 472 00:20:50,000 --> 00:20:53,000 I might tell you how they work except that I don't know because it's 473 00:20:53,000 --> 00:20:55,000 really, really hard. 474 00:20:55,000 --> 00:21:00,000 So take a look around, and you'll find some ones that are quite nice. 475 00:21:00,000 --> 00:21:02,000 Everything I've shown you up to this point are simple, like 476 00:21:02,000 --> 00:21:03,000 how to read input, 477 00:21:03,000 --> 00:21:04,000 how to convert to a string, 478 00:21:04,000 --> 00:21:07,000 things that, while they're nice and important, are not going to 479 00:21:07,000 --> 00:21:08,000 make or break your program. 480 00:21:08,000 --> 00:21:11,000 The thing that is going to make a difference are 481 00:21:11,000 --> 00:21:12,000 these ADTs. 482 00:21:12,000 --> 00:21:14,000 The vector, the set, the map, 483 00:21:14,000 --> 00:21:15,000 the stack, the queue. 484 00:21:15,000 --> 00:21:16,000 Those are important. 485 00:21:16,000 --> 00:21:17,000 If you don't have a map, 486 00:21:17,000 --> 00:21:20,000 the number of things you can do in a program drops exponentially. 487 00:21:20,000 --> 00:21:24,000 It's really impressive how much you need these data structures. 488 00:21:24,000 --> 00:21:27,000 The good news is that C++ not only has a very good support for these data 489 00:21:27,000 --> 00:21:31,000 structures, it has probably the best library in any standard programming 490 00:21:31,000 --> 00:21:32,000 language 491 00:21:32,000 --> 00:21:33,000 that does these things for you. 492 00:21:33,000 --> 00:21:37,000 It's called the Standard Template Library, which is the 493 00:21:37,000 --> 00:21:41,000 STL. For every, single container class you have seen in this class, all the ADTs, 494 00:21:41,000 --> 00:21:42,000 you will find them in the STL. 495 00:21:42,000 --> 00:21:46,000 The names might be a little bit different, the syntax is a little bit weirder, but conceptually, 496 00:21:46,000 --> 00:21:49,000 it's the same thing. A vector is a vector, no matter what language 497 00:21:49,000 --> 00:21:51,000 you write it 498 00:21:51,000 --> 00:21:54,000 in. The STL 499 00:21:54,000 --> 00:21:57,000 on top of that gives you these things called algorithms. 500 00:21:57,000 --> 00:22:00,000 I'll talk about this in a little bit. 501 00:22:00,000 --> 00:22:02,000 Basically, it's the best thing since sliced bread. 502 00:22:02,000 --> 00:22:06,000 I'm not kidding. It's really wonderful and amazing, and I'll talk about that in a 503 00:22:06,000 --> 00:22:07,000 little bit. 504 00:22:07,000 --> 00:22:10,000 The STL is one of the major features of the C++ standard library. 505 00:22:10,000 --> 00:22:11,000 It's 506 00:22:11,000 --> 00:22:12,000 brilliantly designed. 507 00:22:12,000 --> 00:22:13,000 It's not necessarily 508 00:22:13,000 --> 00:22:17,000 the most intuitive thing, but once you get a handle of it, it's very 509 00:22:17,000 --> 00:22:20,000 powerful. 510 00:22:20,000 --> 00:22:23,000 The way you can visualize what this looks like is as this 511 00:22:23,000 --> 00:22:25,000 somewhat pyramid 512 00:22:25,000 --> 00:22:25,000 structure. 513 00:22:25,000 --> 00:22:27,000 At the very bottom, you've got containers. 514 00:22:27,000 --> 00:22:31,000 Your data structures, this is your vector, you map, your set. 515 00:22:31,000 --> 00:22:33,000 You have iterators on top of that. You've all seen iterators in this class. 516 00:22:33,000 --> 00:22:36,000 You've seen map and set iterators. 517 00:22:36,000 --> 00:22:37,000 The STL is basically 518 00:22:37,000 --> 00:22:41,000 all about iterators. Every, single container class has 519 00:22:41,000 --> 00:22:42,000 iterators. 520 00:22:42,000 --> 00:22:45,000 You can iterate over a vector or map or set. 521 00:22:45,000 --> 00:22:46,000 Those worked 522 00:22:46,000 --> 00:22:49,000 to build these things called algorithms where they're 523 00:22:49,000 --> 00:22:51,000 functions that operate on ranges of data. 524 00:22:51,000 --> 00:22:54,000 It's really impressive what you can do with these things. I'll show you a 525 00:22:54,000 --> 00:22:57,000 little bit of that 526 00:22:57,000 --> 00:23:01,000 later. These container classes, the good news is that the names are pretty similar. 527 00:23:01,000 --> 00:23:04,000 Basically, take the 106 version, make it lowercase, you've got your 528 00:23:04,000 --> 00:23:08,000 STL. So big vector turns into little vector. Big map turns into little map, 529 00:23:08,000 --> 00:23:13,000 etc. One exception is grid. There is no STL grid class. It's very easy to 530 00:23:13,000 --> 00:23:16,000 make one. All you have to do is take a vector and do some math to wrap 531 00:23:16,000 --> 00:23:20,000 a two-dimensional container into a one-dimensional one. In fact, that's how our 532 00:23:20,000 --> 00:23:22,000 grid works, so you'll have to build that one. 533 00:23:22,000 --> 00:23:25,000 Otherwise, everything's taken care for you, and that's nice. 534 00:23:25,000 --> 00:23:28,000 Again, they have the same fundamental abstractions. You know how to work with a 535 00:23:28,000 --> 00:23:33,000 stack, so you know how to use the STL stack with a little extra syntax. 536 00:23:33,000 --> 00:23:34,000 The big thing I will say up front, 537 00:23:34,000 --> 00:23:36,000 the emphasis in the STL 538 00:23:36,000 --> 00:23:37,000 is on speed. 539 00:23:37,000 --> 00:23:40,000 In fact, they are designed to be really, really fast. 540 00:23:40,000 --> 00:23:45,000 That means their notion of safety is, good one. 541 00:23:45,000 --> 00:23:48,000 So what this means is you'll have to do your own bounce checking. 542 00:23:48,000 --> 00:23:50,000 When you're iterating, you'll have to make sure you don't walk off the edge of your 543 00:23:50,000 --> 00:23:51,000 container 544 00:23:51,000 --> 00:23:52,000 and stuff like that. 545 00:23:52,000 --> 00:23:56,000 One of the major reasons we didn't go over the STL in this class 546 00:23:56,000 --> 00:23:57,000 is that, 547 00:23:57,000 --> 00:24:00,000 which is if you're trying to figure out how to work with a vector, it's 548 00:24:00,000 --> 00:24:03,000 really bad if you also have to worry about doing your own bounce checking. 549 00:24:03,000 --> 00:24:05,000 If you're working with a map and you have to use the STL map, 550 00:24:05,000 --> 00:24:07,000 you probably wouldn't ever want to use another map 551 00:24:07,000 --> 00:24:09,000 for the rest of your life. It's pretty complicated. 552 00:24:09,000 --> 00:24:12,000 The point is this is what you'll see in the professional environment because it's a very good library, and it's very 553 00:24:12,000 --> 00:24:16,000 fast. 554 00:24:16,000 --> 00:24:19,000 I want to give you a quick example of what this might look like. 555 00:24:19,000 --> 00:24:22,000 This is a program I've written using the CS106 vector. I want to go 556 00:24:22,000 --> 00:24:24,000 change it to use the STL to show you 557 00:24:24,000 --> 00:24:28,000 it's the same thing with a little bit different syntax. 558 00:24:28,000 --> 00:24:32,000 The first thing you have to do is say, the Specter.H we gave you, so you have 559 00:24:32,000 --> 00:24:33,000 to change that to the standard vector, 560 00:24:33,000 --> 00:24:36,000 which is vector in brackets. 561 00:24:36,000 --> 00:24:40,000 Number of characters we have changed so far. Three characters. 562 00:24:40,000 --> 00:24:44,000 Next thing, the big vector becomes little vector. Four characters. 563 00:24:44,000 --> 00:24:48,000 The only big difference is this add function. Anyone who isn't in 564 00:24:48,000 --> 00:24:50,000 my class want to guess 565 00:24:50,000 --> 00:24:54,000 what you would call the function to append something to the end of a vector? Anyone 566 00:24:54,000 --> 00:24:57,000 want to take a guess? [Inaudible]. 567 00:24:57,000 --> 00:24:58,000 Append? That would 568 00:24:58,000 --> 00:25:02,000 be nice. It's actually called push back. 569 00:25:02,000 --> 00:25:05,000 There's good reason for this. It means all these different containers 570 00:25:05,000 --> 00:25:08,000 have similar function names. Push back, 571 00:25:08,000 --> 00:25:09,000 append, potato, 572 00:25:09,000 --> 00:25:12,000 potato. 573 00:25:12,000 --> 00:25:15,000 Okay. That's probably about ten or eleven character changes. Then 574 00:25:15,000 --> 00:25:16,000 to loop over the thing, 575 00:25:16,000 --> 00:25:18,000 it's exactly what you've seen before. 576 00:25:18,000 --> 00:25:19,000 There is a size function. 577 00:25:19,000 --> 00:25:23,000 There are brackets. You can go and iterate. You can go and access things. 578 00:25:23,000 --> 00:25:26,000 The result, as you can see, is not that different. 579 00:25:26,000 --> 00:25:28,000 There are a couple subtleties that I didn't go into here about how the 580 00:25:28,000 --> 00:25:32,000 STL behaves different from the vector you've seen, but overall, it 581 00:25:32,000 --> 00:25:36,000 is the same container. 582 00:25:36,000 --> 00:25:39,000 The other one I want to call your attention to is the STL map. 583 00:25:39,000 --> 00:25:42,000 So if you work with the STL, you need to know two containers, and the rest 584 00:25:42,000 --> 00:25:44,000 follow from there. If you know vector and you know map, 585 00:25:44,000 --> 00:25:46,000 you've got the whole thing down. 586 00:25:46,000 --> 00:25:50,000 The STL map is like our map except that instead of going string to some value, it's 587 00:25:50,000 --> 00:25:51,000 any key type you want 588 00:25:51,000 --> 00:25:53,000 to any value type you want. 589 00:25:53,000 --> 00:25:56,000 So you can map strings to ints, you can map ints to strings, 590 00:25:56,000 --> 00:26:00,000 you can map stack of queue of int to vector of double. 591 00:26:00,000 --> 00:26:04,000 I don't know why you would, but you have the ability to do so. 592 00:26:04,000 --> 00:26:07,000 The problem is that the interface on map 593 00:26:07,000 --> 00:26:11,000 was probably designed by someone who didn't like people. It's really hard 594 00:26:11,000 --> 00:26:14,000 to use. Anyone who has used it will tell you that. Once you 595 00:26:14,000 --> 00:26:17,000 get the hang of it, though, it's pretty intuitive. The result is you have a 596 00:26:17,000 --> 00:26:22,000 very fast map container that is going to be the backbone of any of the programs that you write. 597 00:26:22,000 --> 00:26:23,000 It's quite a useful class. 598 00:26:23,000 --> 00:26:26,000 Again, I don't expect you to memorize every, single bit of syntax here, 599 00:26:26,000 --> 00:26:27,000 everything I'm saying. 600 00:26:27,000 --> 00:26:29,000 Just keep it in the back of the mind if this is what you want to look into. 601 00:26:29,000 --> 00:26:33,000 The map you do need to know. Go take a look at that one if you want to 602 00:26:33,000 --> 00:26:36,000 pursue C++. Now 603 00:26:36,000 --> 00:26:37,000 iterators. 604 00:26:37,000 --> 00:26:40,000 So the iterators you've seen in this class are very nice. They're well-behaved. 605 00:26:40,000 --> 00:26:44,000 Has next, 606 00:26:44,000 --> 00:26:45,000 next. Has next, 607 00:26:45,000 --> 00:26:47,000 next. I'm done. [Inaudible] 608 00:26:47,000 --> 00:26:50,000 iterators are designed to look like pointers. 609 00:26:50,000 --> 00:26:53,000 You might wonder why anyone would voluntarily make something look like a 610 00:26:53,000 --> 00:26:54,000 pointer. There's a very good reason. 611 00:26:54,000 --> 00:26:58,000 This means they're about as smart as a pointer, which means they know 612 00:26:58,000 --> 00:26:59,000 nothing. 613 00:26:59,000 --> 00:27:02,000 So if you want to iterate over a range, you have to have two different iterators. You have 614 00:27:02,000 --> 00:27:03,000 to say start here, 615 00:27:03,000 --> 00:27:04,000 stop there, 616 00:27:04,000 --> 00:27:07,000 keep walking forward until you get there. 617 00:27:07,000 --> 00:27:08,000 Admittedly, 618 00:27:08,000 --> 00:27:10,000 it's more work, 619 00:27:10,000 --> 00:27:12,000 but it's very useful because it means that 620 00:27:12,000 --> 00:27:15,000 if you want to iterate over a 20-element slice out of a 400 621 00:27:15,000 --> 00:27:17,000 million element container, 622 00:27:17,000 --> 00:27:19,000 you can do that. Try doing that with our libraries. You have to go iterate all the 623 00:27:19,000 --> 00:27:21,000 way up the start, 624 00:27:21,000 --> 00:27:23,000 then keep iterating more. 625 00:27:23,000 --> 00:27:26,000 The other thing you need to know about the iterators is that they're read/write. 626 00:27:26,000 --> 00:27:30,000 So you can actually crawl over a container and update the values with an iterator, 627 00:27:30,000 --> 00:27:32,000 something that you cannot do with our libraries. 628 00:27:32,000 --> 00:27:35,000 It's a bit more work. The syntax basically looks like pointer reading and 629 00:27:35,000 --> 00:27:35,000 writing, 630 00:27:35,000 --> 00:27:40,000 but the result is that they're much more powerful, and they're a lot more flexible. 631 00:27:40,000 --> 00:27:43,000 Here's a quick example. 632 00:27:43,000 --> 00:27:46,000 I just wrote some code here that uses a vector iterator. The thing to take a look at is this 633 00:27:46,000 --> 00:27:49,000 right here. 634 00:27:49,000 --> 00:27:53,000 So basically, there's a couple parts to point out. I want to show you this so that if you 635 00:27:53,000 --> 00:27:54,000 see this later on, 636 00:27:54,000 --> 00:27:57,000 you'll get it. Can everybody see this? 637 00:27:57,000 --> 00:27:59,000 The first thing is this begin function. 638 00:27:59,000 --> 00:28:03,000 We call it iterator, they call it begin. It just says, give me a start iterator. 639 00:28:03,000 --> 00:28:07,000 Since we need two iterators to define a range, we keep going until we hit this iterator called 640 00:28:07,000 --> 00:28:09,000 N, which means stop. To 641 00:28:09,000 --> 00:28:13,000 move the iterator forward, has next and next would be too nice, so it's ++, 642 00:28:13,000 --> 00:28:15,000 and then to read from an iterator or write to it, you just de-reference 643 00:28:15,000 --> 00:28:17,000 it. 644 00:28:17,000 --> 00:28:20,000 So you do star iterator, as if it's a pointer, which in many cases, it 645 00:28:20,000 --> 00:28:22,000 actually is. 646 00:28:22,000 --> 00:28:24,000 That's the gist of what this looks like. There's a few more nuances 647 00:28:24,000 --> 00:28:25,000 and subtleties, 648 00:28:25,000 --> 00:28:28,000 but if you keep in mind that if you're looking at STL stuff, just treat this 649 00:28:28,000 --> 00:28:34,000 like pointers, it will make a lot of sense. The 650 00:28:34,000 --> 00:28:37,000 last thing I want to talk about are algorithms. These things are 651 00:28:37,000 --> 00:28:40,000 amazing. The idea is that everything's got iterators, 652 00:28:40,000 --> 00:28:42,000 so if you write functions that work on iterators, they will work on any 653 00:28:42,000 --> 00:28:43,000 container type. 654 00:28:43,000 --> 00:28:47,000 So if you want a binary search or a vector, you call binary search. 655 00:28:47,000 --> 00:28:51,000 It's a prewritten function. You want to sort something? Great. Go call sort. 656 00:28:51,000 --> 00:28:55,000 You guys have seen permutations before, right? You have to go do recursion pulsing in the 657 00:28:55,000 --> 00:28:58,000 front, permute the rest, stick it back on, that sort of stuff? 658 00:28:58,000 --> 00:29:00,000 STL, you can do it in a while loop. 659 00:29:00,000 --> 00:29:02,000 You can while you next permutation something, 660 00:29:02,000 --> 00:29:05,000 come up with every, single permutation, and you don't have to write a single recursive 661 00:29:05,000 --> 00:29:07,000 function ever. 662 00:29:07,000 --> 00:29:10,000 These sorts of things, I think there's 75 different algorithms, anything 663 00:29:10,000 --> 00:29:12,000 from permuting to searching to sorting, 664 00:29:12,000 --> 00:29:15,000 to transforming to rearranging to sorting to splitting. 665 00:29:15,000 --> 00:29:18,000 You name it, there's probably an algorithm that does it. If 666 00:29:18,000 --> 00:29:19,000 you play around with the STL, 667 00:29:19,000 --> 00:29:26,000 you get to see all this stuff. It's a lot of fun. 668 00:29:26,000 --> 00:29:29,000 I've just been showing you some of the libraries. That's basically all the library changes you need to 669 00:29:29,000 --> 00:29:29,000 know. 670 00:29:29,000 --> 00:29:32,000 Basically, everything you want to do is still there. 671 00:29:32,000 --> 00:29:35,000 The syntax is a little different. Some of the libraries you'll have to write yourself, but it's 672 00:29:35,000 --> 00:29:39,000 all there. It's not like you have to relearn everything from scratch. 673 00:29:39,000 --> 00:29:42,000 But the libraries are only as good as the language is, 674 00:29:42,000 --> 00:29:45,000 and there are a couple of language features of C++ that we didn't really talk about 675 00:29:45,000 --> 00:29:48,000 in this class. I want to go over some of them because you will see them a lot in 676 00:29:48,000 --> 00:29:50,000 professional code. 677 00:29:50,000 --> 00:29:53,000 After all, you cannot use a library any more than you understand how the 678 00:29:53,000 --> 00:29:55,000 language works. So getting a rough understanding of what this will do 679 00:29:55,000 --> 00:29:59,000 will make your life easier in the long 680 00:29:59,000 --> 00:30:01,000 run. The first thing I want to talk about is 681 00:30:01,000 --> 00:30:06,000 const. You've seen const before in the context of const and my context equals five. 682 00:30:06,000 --> 00:30:10,000 Just make a global constant. Saying that const makes global constants is 683 00:30:10,000 --> 00:30:13,000 like saying a computer is something that adds numbers. 684 00:30:13,000 --> 00:30:13,000 Yes, 685 00:30:13,000 --> 00:30:17,000 but they can also run Windows, for example. 686 00:30:17,000 --> 00:30:21,000 Const shows up just about everywhere. It's the little keyword that could. 687 00:30:21,000 --> 00:30:24,000 You'd be amazed just how useful this little keyword can be. 688 00:30:24,000 --> 00:30:26,000 The biggest thing that it does 689 00:30:26,000 --> 00:30:29,000 is helps protect you against your own mistakes. 690 00:30:29,000 --> 00:30:32,000 If you're writing a program and you know something shouldn't change, tag 691 00:30:32,000 --> 00:30:33,000 it const 692 00:30:33,000 --> 00:30:36,000 because that way, later down the lie, if you do accidentally change it, it says, 693 00:30:36,000 --> 00:30:39,000 that's a mistake. Compiler error. You screwed up. 694 00:30:39,000 --> 00:30:42,000 It protects you. That should've been a double equal, not 695 00:30:42,000 --> 00:30:44,000 a single equal, 696 00:30:44,000 --> 00:30:46,000 those sorts of things. 697 00:30:46,000 --> 00:30:49,000 You've all probably seen that the C++ compiler doesn't treat anything as sacred. Like, oh, you don't 698 00:30:49,000 --> 00:30:51,000 want to return 699 00:30:51,000 --> 00:30:56,000 something? Or something like, oh, wow, you did single equal instead of double equal. 700 00:30:56,000 --> 00:30:57,000 You're the boss. 701 00:30:57,000 --> 00:31:00,000 The second you try to break const, it pulls out its big ruler of judgment and 702 00:31:00,000 --> 00:31:04,000 whacks you on the wrist with it, like shame on your for breaking this constants. It 703 00:31:04,000 --> 00:31:06,000 is the compiler trying to help you out. 704 00:31:06,000 --> 00:31:08,000 Please know about this keyword. I'll give you 705 00:31:08,000 --> 00:31:11,000 an example. See 706 00:31:11,000 --> 00:31:15,000 this function prototype? Void do something takes a vector by reference. 707 00:31:15,000 --> 00:31:18,000 So I've passed my vector into this function, and the question is, what's it going to 708 00:31:18,000 --> 00:31:20,000 hold when it comes back? 709 00:31:20,000 --> 00:31:22,000 Well, it could be the same. It 710 00:31:22,000 --> 00:31:25,000 could be completely empty. There could be 137 copies of the 711 00:31:25,000 --> 00:31:26,000 number 137. 712 00:31:26,000 --> 00:31:28,000 You don't know. 713 00:31:28,000 --> 00:31:31,000 In this class, we've shown you, pass by reference can be either for 714 00:31:31,000 --> 00:31:33,000 efficiency reasons because it's not fast enough, 715 00:31:33,000 --> 00:31:35,000 or it can be 716 00:31:35,000 --> 00:31:37,000 because you want to actually change the thing. 717 00:31:37,000 --> 00:31:40,000 It's difficult to see what this function does because you have no idea 718 00:31:40,000 --> 00:31:43,000 which of those two it is. 719 00:31:43,000 --> 00:31:46,000 The idea, though, and this is something that you will see in professional code everywhere, 720 00:31:46,000 --> 00:31:48,000 is doing something like this. 721 00:31:48,000 --> 00:31:51,000 If I say, void do something const 722 00:31:51,000 --> 00:31:53,000 vector and my vector, 723 00:31:53,000 --> 00:31:56,000 that says that this vector can't be modified within the function. 724 00:31:56,000 --> 00:32:00,000 It's like handing this thing off saying, I'm giving you this value 725 00:32:00,000 --> 00:32:01,000 for efficiency reasons. 726 00:32:01,000 --> 00:32:04,000 Donft try to modify it. In fact, you can't modify it. 727 00:32:04,000 --> 00:32:07,000 It makes your code self-documenting. Someone looking at your functions can go, 728 00:32:07,000 --> 00:32:09,000 that's const. I can't possibly change anything there, 729 00:32:09,000 --> 00:32:13,000 and they'll be totally fine handing off the string containing their graduate thesis 730 00:32:13,000 --> 00:32:14,000 to it, for example. 731 00:32:14,000 --> 00:32:18,000 If you had your graduate thesis stored as a string, 732 00:32:18,000 --> 00:32:19,000 which I don't 733 00:32:19,000 --> 00:32:20,000 know why you would ever do this, 734 00:32:20,000 --> 00:32:24,000 but if you see a function take the string and parameter, you'd probably be a little 735 00:32:24,000 --> 00:32:27,000 bit worried. Like, is it going to replace my graduate thesis with, 736 00:32:27,000 --> 00:32:30,000 hi, I don't have a graduate thesis or what? 737 00:32:30,000 --> 00:32:34,000 The const is useful. 738 00:32:34,000 --> 00:32:36,000 The question is, if it's such a useful keyword, 739 00:32:36,000 --> 00:32:39,000 why didn't we show you this? 740 00:32:39,000 --> 00:32:43,000 The biggest reason is you can't just use const every now and then. You can't say, I'm going 741 00:32:43,000 --> 00:32:47,000 to mark this parameter const. I'm gong to say that one's const right there. It 742 00:32:47,000 --> 00:32:48,000 doesn't work that way. 743 00:32:48,000 --> 00:32:52,000 Let's say I do mark something const. That object has to know how to behave 744 00:32:52,000 --> 00:32:53,000 when it can't modify itself. 745 00:32:53,000 --> 00:32:56,000 So you have to make the entire class written const correct. 746 00:32:56,000 --> 00:32:59,000 Then if it has any data members that are classes, that class has to be const correct. You get this 747 00:32:59,000 --> 00:33:00,000 748 00:33:00,000 --> 00:33:01,000 exploding effect where 749 00:33:01,000 --> 00:33:05,000 you stick a singe const in, and suddenly every, single piece of your code is marked 750 00:33:05,000 --> 00:33:06,000 const. 751 00:33:06,000 --> 00:33:09,000 That's a good thing because it makes your code self-documenting, 752 00:33:09,000 --> 00:33:13,000 but the point is it's an unnecessary language complication. When 753 00:33:13,000 --> 00:33:16,000 you're trying to write the PQ class, you should be worried, how do I build a 754 00:33:16,000 --> 00:33:17,000 chunk list, not oh, 755 00:33:17,000 --> 00:33:20,000 is this function const? So in the 756 00:33:20,000 --> 00:33:23,000 professional world, you will see it. Here we thought, it makes things too 757 00:33:23,000 --> 00:33:28,000 complicated. We'll give it a pass for right 758 00:33:28,000 --> 00:33:30,000 now. Another big one. Object copying an assignment. 759 00:33:30,000 --> 00:33:34,000 You've seen disallow copy. It says, don't copy this 760 00:33:34,000 --> 00:33:38,000 object. It's not standard C++, but I've been told that Google actually 761 00:33:38,000 --> 00:33:41,000 has a macro that does something just like this, so you're programming the same way Google 762 00:33:41,000 --> 00:33:42,000 people do. 763 00:33:42,000 --> 00:33:43,000 The difference is that 764 00:33:43,000 --> 00:33:45,000 if you're trying to write a PQ 765 00:33:45,000 --> 00:33:47,000 and it can't copy itself, 766 00:33:47,000 --> 00:33:51,000 it's a little bit frustrating. Think about it. You make some PQ. You can make a vector 767 00:33:51,000 --> 00:33:51,000 copy. 768 00:33:51,000 --> 00:33:52,000 You can make a map copy. 769 00:33:52,000 --> 00:33:56,000 If you can't make a PQ copy, you'll be wondering, why not? 770 00:33:56,000 --> 00:33:59,000 So it turns out C++ lets you redefine the way copying 771 00:33:59,000 --> 00:34:01,000 works. So these two functions here 772 00:34:01,000 --> 00:34:04,000 called copy constructors and assignment operators. 773 00:34:04,000 --> 00:34:07,000 While it's very useful to know how to do this, there's a major reason we didn't tell 774 00:34:07,000 --> 00:34:08,000 you about it in this class. 775 00:34:08,000 --> 00:34:12,000 It's because it's really hard. 776 00:34:12,000 --> 00:34:16,000 I think I spent, in 106L, an entire week going over this. 777 00:34:16,000 --> 00:34:18,000 We don't have a week to tell you how to do this. 778 00:34:18,000 --> 00:34:21,000 Here's a list of some of the things you have to keep in mind when writing these 779 00:34:21,000 --> 00:34:21,000 functions. 780 00:34:21,000 --> 00:34:25,000 [Inaudible] to clean up your own memory, not memory you don't own. To clean 781 00:34:25,000 --> 00:34:28,000 up the memory, to copy an object, to copy the object correctly, to handle 782 00:34:28,000 --> 00:34:31,000 these [inaudible], to follow the same rules that C++ syntax dictates. 783 00:34:31,000 --> 00:34:34,000 There's an awful lot of stuff going on there. 784 00:34:34,000 --> 00:34:37,000 To expect you to get all this right when you're worrying about your PQ is just 785 00:34:37,000 --> 00:34:37,000 too much. 786 00:34:37,000 --> 00:34:40,000 It really is. 787 00:34:40,000 --> 00:34:41,000 Imagine it's the day before it's due. 788 00:34:41,000 --> 00:34:43,000 You've got the PQ working beautifully, and 789 00:34:43,000 --> 00:34:45,000 it doesn't copy 790 00:34:45,000 --> 00:34:49,000 right. What does that tell you? You've written your copy function wrong, but you totally 791 00:34:49,000 --> 00:34:50,000 understand and made your point, 792 00:34:50,000 --> 00:34:52,000 which is how to build a priority queue. 793 00:34:52,000 --> 00:34:54,000 This is a chunk list. This is a heap. 794 00:34:54,000 --> 00:34:55,000 This is an unsorted 795 00:34:55,000 --> 00:34:58,000 vector. That's what's actually important, not building stuff like this. 796 00:34:58,000 --> 00:35:00,000 When you're in the professional world, 797 00:35:00,000 --> 00:35:03,000 you do need to do some extra work to make your objects copy 798 00:35:03,000 --> 00:35:07,000 correctly. So you should look into these functions a little bit. 799 00:35:07,000 --> 00:35:08,000 Again, to stress, 800 00:35:08,000 --> 00:35:11,000 the stuff you've learned in here, the actual here's how you build these data 801 00:35:11,000 --> 00:35:14,000 structures, is more important than these global syntactic nuances that you've got to be 802 00:35:14,000 --> 00:35:18,000 aware of. 803 00:35:18,000 --> 00:35:20,000 One more thing. This is pretty cool. 804 00:35:20,000 --> 00:35:22,000 I have this 805 00:35:22,000 --> 00:35:23,000 code snippet right here. 806 00:35:23,000 --> 00:35:24,000 I make a string, 807 00:35:24,000 --> 00:35:25,000 and I say, 808 00:35:25,000 --> 00:35:30,000 my string is equal to this is, and then I tack onto it, a string. 809 00:35:30,000 --> 00:35:32,000 I'm going to iterate over this entire string, 810 00:35:32,000 --> 00:35:35,000 and every step, I'm going to print out the current character. So 811 00:35:35,000 --> 00:35:37,000 this is just print out the string 812 00:35:37,000 --> 00:35:39,000 one letter at a time. Ifm going to 813 00:35:39,000 --> 00:35:42,000 highlight a few things. 814 00:35:42,000 --> 00:35:45,000 Why is it legal to [inaudible] equals a string with something else? 815 00:35:45,000 --> 00:35:48,000 Why string but not priority queue? Why are we allow to do this? 816 00:35:48,000 --> 00:35:50,000 It's a string. It's an 817 00:35:50,000 --> 00:35:54,000 object, and we just code plus equals on it. 818 00:35:54,000 --> 00:35:56,000 Why does it let you do less than, less than? 819 00:35:56,000 --> 00:35:59,000 What does that mean? Why do streams let you do that but nothing else? Why can 820 00:35:59,000 --> 00:36:03,000 I read a string with brackets even though it's really an object? 821 00:36:03,000 --> 00:36:07,000 At a high level, we all know what it means to add something to a string. It 822 00:36:07,000 --> 00:36:11,000 means take something and stick it on. You know what this less than, less than. It means put into 823 00:36:11,000 --> 00:36:14,000 a stream. 824 00:36:14,000 --> 00:36:18,000 [Inaudible] C++, I want to define these operators for my classes. 825 00:36:18,000 --> 00:36:21,000 It's a technique known as operator overloading. 826 00:36:21,000 --> 00:36:25,000 Basically, all you do is write functions that are called operator and 827 00:36:25,000 --> 00:36:28,000 whatever the name of your operator is. So operator equals, operator brackets, operator 828 00:36:28,000 --> 00:36:29,000 less than, less than. 829 00:36:29,000 --> 00:36:30,000 Operator plus, plus. 830 00:36:30,000 --> 00:36:34,000 It's just a syntax convenience. You can write code that looks more 831 00:36:34,000 --> 00:36:34,000 intuitive 832 00:36:34,000 --> 00:36:37,000 that does something complex behind the scenes. For 833 00:36:37,000 --> 00:36:41,000 example, if I write my vector bracket I, it's not like, oh, it's a bracket. It's much 834 00:36:41,000 --> 00:36:43,000 faster. It really means, call the function called 835 00:36:43,000 --> 00:36:45,000 operator brackets. 836 00:36:45,000 --> 00:36:49,000 Again, it's a convenience and nothing else. If you treat it as anything more than a 837 00:36:49,000 --> 00:36:51,000 convenience, you can kind of hurt yourself with it. 838 00:36:51,000 --> 00:36:54,000 You can overload basically every operator in C++. You can 839 00:36:54,000 --> 00:36:58,000 overload things like bit wise [inaudible] with assignment. You can overload the 840 00:36:58,000 --> 00:37:01,000 exore operator. You can overload parenthesis, and, comma, 841 00:37:01,000 --> 00:37:03,000 all these operators that most people never use. 842 00:37:03,000 --> 00:37:06,000 The ones that you see most frequently, though, are these 843 00:37:06,000 --> 00:37:09,000 ones. Overloading operator less than, less than for stream insertion. 844 00:37:09,000 --> 00:37:12,000 You can actually make it so that you can make a vector class than 845 00:37:12,000 --> 00:37:15,000 can print itself out to the screen. It's kind 846 00:37:15,000 --> 00:37:17,000 of useful. The assignment operator, operator equals, 847 00:37:17,000 --> 00:37:20,000 the less than operator for comparing things and the parentheses operator. 848 00:37:20,000 --> 00:37:23,000 You can actually overload parentheses. It's pretty cool stuff. It's 849 00:37:23,000 --> 00:37:27,000 actually for something called functors, which is really awesome. If you do play around with 850 00:37:27,000 --> 00:37:30,000 C++, this is definitely something to keep in mind because it will make your 851 00:37:30,000 --> 00:37:33,000 life a lot more enjoyable. 852 00:37:33,000 --> 00:37:36,000 I think more than any other language features, C++, except possibly 853 00:37:36,000 --> 00:37:40,000 multiple inherence, this is the most widely-criticized 854 00:37:40,000 --> 00:37:41,000 feature of the language. 855 00:37:41,000 --> 00:37:44,000 The reason is that you can make things that make no sense legal. 856 00:37:44,000 --> 00:37:47,000 Like, defining the modular operator for two 857 00:37:47,000 --> 00:37:50,000 PQ. My PQ, my other PQ. Anybody think 858 00:37:50,000 --> 00:37:54,000 of a sensible interpretation of what it means to mod one PQ by 859 00:37:54,000 --> 00:37:57,000 another? If you do, please send me an email or 860 00:37:57,000 --> 00:37:59,000 shout it out right now. I can't think of one. 861 00:37:59,000 --> 00:38:01,000 But you can make it legal. 862 00:38:01,000 --> 00:38:04,000 I don't know why you would, but you can. 863 00:38:04,000 --> 00:38:05,000 The point of this is, if you think 864 00:38:05,000 --> 00:38:07,000 about it, call it the philosophy. Let 865 00:38:07,000 --> 00:38:11,000 the programmer make the choice, even if it lets them choose wrong. 866 00:38:11,000 --> 00:38:14,000 You can do this. You probably shouldn't do it, but 867 00:38:14,000 --> 00:38:16,000 by giving you the opportunity to do so, 868 00:38:16,000 --> 00:38:19,000 it means that when you really do need to write a class that has a module 869 00:38:19,000 --> 00:38:21,000 that's defined, you can do it. 870 00:38:21,000 --> 00:38:22,000 It's flexibility. It 871 00:38:22,000 --> 00:38:23,000 means that you have to 872 00:38:23,000 --> 00:38:30,000 make sure that what you're doing makes sense, but it's flexibility. You've 873 00:38:30,000 --> 00:38:32,000 all just finished Pathfinder, right? 874 00:38:32,000 --> 00:38:37,000 Did anybody templatize the [inaudible] PQ, by any chance? Anyone? You guys 875 00:38:37,000 --> 00:38:39,000 are smart. 876 00:38:39,000 --> 00:38:44,000 Here's the thing. Let's say I wrote something that says, A equals B. 877 00:38:44,000 --> 00:38:47,000 What does this mean? It says assign B 878 00:38:47,000 --> 00:38:49,000 A. What if I write this in a template function? 879 00:38:49,000 --> 00:38:53,000 I have a template of some unknown type. I'll just say A equals B. 880 00:38:53,000 --> 00:38:54,000 What's it mean? 881 00:38:54,000 --> 00:38:58,000 Well, if it's an int, it means take the int and copy it. If it's a floating point 882 00:38:58,000 --> 00:39:01,000 number, it says take the floating point number and copy it. If 883 00:39:01,000 --> 00:39:04,000 it's a string, it says make a string deep copy. 884 00:39:04,000 --> 00:39:08,000 The point is that every, single time you use this same syntax, A equals B, 885 00:39:08,000 --> 00:39:11,000 but the result is different, and it's always the correct result. 886 00:39:11,000 --> 00:39:15,000 This is what you can get with operator [inaudible], the ability to take code 887 00:39:15,000 --> 00:39:19,000 and make it look right for every, single class so that in templates, you can just 888 00:39:19,000 --> 00:39:23,000 call the function, and you're guaranteed it will work. 889 00:39:23,000 --> 00:39:26,000 That's a quick tour of some of the language features. I know this 890 00:39:26,000 --> 00:39:30,000 might seem like an awful lot. I've been going very quickly through it, 891 00:39:30,000 --> 00:39:33,000 but want to conclude by saying so what? 892 00:39:33,000 --> 00:39:37,000 I just harangued you with a lot of language features, a lot of syntax, a lot of 893 00:39:37,000 --> 00:39:37,000 libraries. 894 00:39:37,000 --> 00:39:39,000 What's the point? 895 00:39:39,000 --> 00:39:41,000 The point to take out of this is that 896 00:39:41,000 --> 00:39:43,000 C++ is big, 897 00:39:43,000 --> 00:39:46,000 but it's also a very powerful language, and it's a very expressive language. I 898 00:39:46,000 --> 00:39:48,000 think it's a very beautiful language. 899 00:39:48,000 --> 00:39:51,000 The features that you've learned in this class will go anywhere you want them to. 900 00:39:51,000 --> 00:39:54,000 If you want to take your coding skills 901 00:39:54,000 --> 00:39:55,000 and apply 902 00:39:55,000 --> 00:39:57,000 them to C++, you can, 903 00:39:57,000 --> 00:39:58,000 and you have this flexibility. 904 00:39:58,000 --> 00:40:00,000 You really have this gift. 905 00:40:00,000 --> 00:40:03,000 What you've learned in here is something most people never learn. It's how to make 906 00:40:03,000 --> 00:40:05,000 a computer solve a problem for you. 907 00:40:05,000 --> 00:40:07,000 That's a skill that's going to follow you for the rest of your life, no matter where 908 00:40:07,000 --> 00:40:09,000 you take it. 909 00:40:09,000 --> 00:40:12,000 If you're interested in C++, I have a huge number of references here, if you 910 00:40:12,000 --> 00:40:13,000 want to go see them. 911 00:40:13,000 --> 00:40:16,000 The point is that if you want to solve a problem with a computer, you need three things. 912 00:40:16,000 --> 00:40:19,000 First, 913 00:40:19,000 --> 00:40:20,000 programming skills. 914 00:40:20,000 --> 00:40:21,000 You all have that. 915 00:40:21,000 --> 00:40:23,000 You need a good idea. 916 00:40:23,000 --> 00:40:26,000 I don't think I have any good ideas for programming. If I did, I'd probably do them myself, 917 00:40:26,000 --> 00:40:29,000 but if you have a good idea, and you want to make $1 billion with it, the 918 00:40:29,000 --> 00:40:32,000 last thing you need is a programming language. 919 00:40:32,000 --> 00:40:35,000 Hopefully this quick tour of C++ has showed you. This 920 00:40:35,000 --> 00:40:38,000 is what this language is. It's a tool for solving real problems 921 00:40:38,000 --> 00:40:40,000 that trusts you 922 00:40:40,000 --> 00:40:41,000 and that means that overall, you'll 923 00:40:41,000 --> 00:40:43,000 have fun doing it. 924 00:40:43,000 --> 00:40:45,000 So if you want to go and run with this, if you want to say, I'm a good programmer. 925 00:40:45,000 --> 00:40:46,000 I can do this and 926 00:40:46,000 --> 00:40:49,000 learn C++. I think you will love it. I think you will enjoy it. I think it will 927 00:40:49,000 --> 00:40:53,000 be some of the most fun you will have sitting in front of a computer. 928 00:40:53,000 --> 00:40:56,000 So have fun with this stuff. That's the whole point. If you're in this class, I hope 929 00:40:56,000 --> 00:41:00,000 you enjoy it. I hope you said, yeah, I can make a computer solve something. I can do graph 930 00:41:00,000 --> 00:41:01,000 algorithms. I can do graph [inaudible]. 931 00:41:01,000 --> 00:41:04,000 I can make data structures. I can make a computer program that plays Boggle 932 00:41:04,000 --> 00:41:08,000 better than I ever could or anyone I know ever can. 933 00:41:08,000 --> 00:41:10,000 Have fun. That's the whole point of this. 934 00:41:10,000 --> 00:41:14,000 If you want to do it in C++, come talk to me. I will give you some references. 935 00:41:14,000 --> 00:41:17,000 Enjoy. Good luck on the final, and I will probably see you around on Friday for the final. 936 00:41:17,000 --> 00:41:18,000 Enjoy.