0:00:00.000,0:00:10.000 0:00:10.000,0:00:11.000 0:00:11.000,0:00:15.000 This presentation is delivered by the Stanford Center for Professional 0:00:15.000,0:00:25.000 Development. 0:00:25.000,0:00:29.000 I'm guessing by the fact that I can see myself that we're on. 0:00:29.000,0:00:33.000 Welcome back to CS106L. My name's Keith. I'm a section 0:00:33.000,0:00:37.000 leader here. I teach CS106L, which is the standard C++ program and 0:00:37.000,0:00:37.000 laboratory. 0:00:37.000,0:00:39.000 I recognize a few people here, which 0:00:39.000,0:00:40.000 is great. 0:00:40.000,0:00:44.000 Julie wanted me to come in today and talk to you about the C++ 0:00:44.000,0:00:47.000 programming language, what's it look like outside of CS106B? 0:00:47.000,0:00:50.000 What sorts of things do you need to be aware of to just kind of give you a general 0:00:50.000,0:00:53.000 picture of what this language looks like? 0:00:53.000,0:00:56.000 Before I get into the actual C++ stuff, I wanted to start off by 0:00:56.000,0:00:59.000 congratulating all of you for making it through this class. That's not a 0:00:59.000,0:01:01.000 small accomplishment. 0:01:01.000,0:01:04.000 If you think about when you started, day one, probably looking at this screen right here, probably 0:01:04.000,0:01:05.000 thinking, 0:01:05.000,0:01:08.000 what's that [inaudible], what's Genlib, what's C [inaudible], and why's it 0:01:08.000,0:01:11.000 really, really less than something? 0:01:11.000,0:01:12.000 Look where you are now. 0:01:12.000,0:01:16.000 You now know how to be a client of the vector, the stack, the map, the queue 0:01:16.000,0:01:17.000 and the set. 0:01:17.000,0:01:20.000 You know that recursion, you know pointers, you know Linklis and binary 0:01:20.000,0:01:21.000 [inaudible] and graphs. 0:01:21.000,0:01:25.000 You know algorithmic analysis and graph algorithms and searching and sorting. 0:01:25.000,0:01:26.000 You know how the lexicon works. 0:01:26.000,0:01:29.000 You know how to implement all of these classes. You know data abstraction. 0:01:29.000,0:01:34.000 That's not a small accomplishment. That's really something to be proud of. 0:01:34.000,0:01:37.000 What I wanted to say is that these are real, practical tools that will follow 0:01:37.000,0:01:39.000 you, no matter where you take them. 0:01:39.000,0:01:43.000 Some of you might go on in CS. You might think, wow, I really like this class. I want to 0:01:43.000,0:01:45.000 see where it goes. That's great. 0:01:45.000,0:01:48.000 No matter where you take it, if you go and take algorithms, you take 0:01:48.000,0:01:52.000 compliers or data bases or operating systems, the skills you've learned here is really 0:01:52.000,0:01:54.000 going to be the foundation 0:01:54.000,0:01:57.000 of all of your programing. You're always going to be using maps. You're always 0:01:57.000,0:02:00.000 going to be using vectors. You're going to need recursion, no matter where you 0:02:00.000,0:02:01.000 apply it. 0:02:01.000,0:02:04.000 It's really wonderful that you have these skills. 0:02:04.000,0:02:06.000 Of course, some of you aren't going to go on in computer science. That's totally fine. You 0:02:06.000,0:02:11.000 took this class and said, it's not for me or, you know, maybe not. Well, 0:02:11.000,0:02:12.000 that's fine too because 0:02:12.000,0:02:15.000 whether you go into sociology or philosophy or psychology or math or 0:02:15.000,0:02:16.000 physics or chemistry, 0:02:16.000,0:02:18.000 there are problems to be solved, 0:02:18.000,0:02:20.000 and there are problems you can solve with a computer. 0:02:20.000,0:02:23.000 What you've learned in this class is the most valuable skill of them all, which 0:02:23.000,0:02:25.000 is how to take a problem, 0:02:25.000,0:02:26.000 model it 0:02:26.000,0:02:27.000 and solve it. 0:02:27.000,0:02:29.000 That really is something. 0:02:29.000,0:02:32.000 The skills you've learned here transcend any specific programming language. 0:02:32.000,0:02:36.000 You can do everything you've done in here, in Java, in C++, in PHP, 0:02:36.000,0:02:41.000 in Python. Everything that you think of, you will be using these same tools. 0:02:41.000,0:02:42.000 That said, 0:02:42.000,0:02:46.000 we've taught you everything in this class using the C++ programming 0:02:46.000,0:02:46.000 language. 0:02:46.000,0:02:48.000 We use C++ 0:02:48.000,0:02:52.000 because it's very good for expressing the concepts that we were going over. It 0:02:52.000,0:02:56.000 has great support for recursion, has exposed pointers, so you can do things like 0:02:56.000,0:02:58.000 Linklis and binary trees quite nicely. 0:02:58.000,0:03:01.000 It has objects so you can do data abstraction. It's got templates. It's a very 0:03:01.000,0:03:05.000 good mix of different programing strategies and different programming styles 0:03:05.000,0:03:07.000 that means we can teach you things without having to bog you down in 0:03:07.000,0:03:09.000 language syntax. 0:03:09.000,0:03:11.000 That said, this really hasn't been much of a class 0:03:11.000,0:03:13.000 in C++. 0:03:13.000,0:03:16.000 Think of it was we all put you inside the C++ bus and drove 0:03:16.000,0:03:19.000 you up to the top of the mountain of CS106B knowledge. 0:03:19.000,0:03:21.000 You know that the bus got you up there, 0:03:21.000,0:03:24.000 but you don't really know what's going on under the hood. 0:03:24.000,0:03:27.000 That's okay because what you learn in this class is more important than that. 0:03:27.000,0:03:30.000 All the programing language and knowledge in the universe is not going to help you 0:03:30.000,0:03:33.000 solve a problem if you don't know how to use a vector, you don't know how to make a 0:03:33.000,0:03:36.000 map, and you can't make a recursive function solve things. 0:03:36.000,0:03:40.000 What I want to do today is talk about what the C++ language is. What's 0:03:40.000,0:03:42.000 it look like the real world? 0:03:42.000,0:03:43.000 What kind of stuff do you need to know? 0:03:43.000,0:03:46.000 More importantly, how do you take these skills that you've learned, which are very 0:03:46.000,0:03:50.000 generic, and go and apply them in this language. 0:03:50.000,0:03:53.000 I love C++. I've been using it for years. I think it's a beautiful 0:03:53.000,0:03:56.000 language. It's got support for so many different programing paradigms. It's 0:03:56.000,0:03:59.000 easy to use once you get a handle on it. 0:03:59.000,0:04:01.000 It lets you solve problems in so many different ways. 0:04:01.000,0:04:04.000 Really, I just want to show you what it looks like. 0:04:04.000,0:04:07.000 Think of this as an appetizer. I'm just going to give you the - this 0:04:07.000,0:04:10.000 is the C++ appetizer plate. I'll show you a little bit of this, a 0:04:10.000,0:04:11.000 little bit of that, 0:04:11.000,0:04:15.000 give you enough working knowledge that you know where to go to get help. 0:04:15.000,0:04:16.000 You can see 0:04:16.000,0:04:18.000 this is what you need to learn, get 0:04:18.000,0:04:21.000 an overview of what we've been doing, what we've provided you, what we haven't 0:04:21.000,0:04:22.000 provided you, 0:04:22.000,0:04:25.000 so that by the time you're done, you can think, 0:04:25.000,0:04:27.000 maybe I want to go on in this language. 0:04:27.000,0:04:30.000 I'm not going to force you to learn C++. I can't do that. I'm 0:04:30.000,0:04:33.000 not going to follow you around, did you learn C++ yet? Did you learn C++ yet? 0:04:33.000,0:04:35.000 No, I'm not going to do that. 0:04:35.000,0:04:38.000 You guys are all competent programmers right now. 0:04:38.000,0:04:41.000 You know how to solve these problems. You know how to take on these challenges. 0:04:41.000,0:04:43.000 So I just want to show you the language so you can decide whether or not you 0:04:43.000,0:04:44.000 want to pursue it. 0:04:44.000,0:04:47.000 Maybe you will. That'd be great. If not, 0:04:47.000,0:04:48.000 that's okay, too, 0:04:48.000,0:04:51.000 because you know how to tackle problems, and all you need now is a language to 0:04:51.000,0:04:53.000 do it 0:04:53.000,0:04:56.000 in. I want to show you basically four aspects of C++. First 0:04:56.000,0:05:00.000 some philosophy and some history because every language has a personality. 0:05:00.000,0:05:03.000 Every language wants to treat you as a programmer a different way, give you some 0:05:03.000,0:05:05.000 different options. So I just want to show you what to expect when 0:05:05.000,0:05:07.000 you're working with C++. 0:05:07.000,0:05:10.000 As a language, it has a lot of criticisms. How many of you have heard 0:05:10.000,0:05:15.000 bad things about C++ in any way, shape or form? 0:05:15.000,0:05:18.000 Wow, every, single person in this room, basically. 0:05:18.000,0:05:21.000 So there's a lot of criticisms leveled at this language, and what I want to 0:05:21.000,0:05:22.000 do 0:05:22.000,0:05:25.000 is show you what the mindset is so you can take a look at these 0:05:25.000,0:05:28.000 criticisms and evaluate them. In all seriousness, most of the 0:05:28.000,0:05:30.000 criticisms leveled at this language are 0:05:30.000,0:05:33.000 criticisms with things a language doesn't try to do. 0:05:33.000,0:05:37.000 At the same time, if you get this philosophy and you get this history, you'll 0:05:37.000,0:05:38.000 understand where this language came from, 0:05:38.000,0:05:41.000 what it's designed to do and where it's going. 0:05:41.000,0:05:44.000 The second thing I want to talk about is the actual mechanics. 0:05:44.000,0:05:47.000 What does the programing language look like? So I'll talk about the 0:05:47.000,0:05:47.000 libraries, and 0:05:47.000,0:05:49.000 I'll talk about the core language features. 0:05:49.000,0:05:52.000 What happens when you take away things like [inaudible] and Synfi? What are you going to have 0:05:52.000,0:05:54.000 to do now that you don't have those? In 0:05:54.000,0:05:58.000 terms of it's actually pretty straightforward and you already know most of it. 0:05:58.000,0:06:00.000 Then some language features that we didn't cover, things that you would expect to 0:06:00.000,0:06:02.000 see in a professional setting 0:06:02.000,0:06:05.000 that we didn't go over in this class because it's needlessly complicated and 0:06:05.000,0:06:08.000 doesn't have much to do with this general data structure and 0:06:08.000,0:06:10.000 algorithm challenges we've been giving you. 0:06:10.000,0:06:12.000 Finally, just some references. 0:06:12.000,0:06:14.000 Where do you go? 0:06:14.000,0:06:17.000 You want to learn C++? Great. We have a list of wonderful books and resources 0:06:17.000,0:06:18.000 you can go and reference, 0:06:18.000,0:06:20.000 and it can get you up to speed very quickly. 0:06:20.000,0:06:22.000 So it's kind of a game plan. 0:06:22.000,0:06:23.000 See what's out there, 0:06:23.000,0:06:28.000 have some fun with it and learn where to go. Sound good? Okay. 0:06:28.000,0:06:29.000 So I want to start off 0:06:29.000,0:06:32.000 with a quick history of C++. 0:06:32.000,0:06:34.000 So C++ did not come into being one day when a whole bunch of programmers came 0:06:34.000,0:06:37.000 into a room, made some demonic incantations over a bubbling cauldron and 0:06:37.000,0:06:39.000 walked out. It didn't happen. 0:06:39.000,0:06:42.000 It was not invented in one day. People did not sit down and say, our language is going to look like 0:06:42.000,0:06:44.000 this. 0:06:44.000,0:06:45.000 It has a history. 0:06:45.000,0:06:49.000 It has evolution, and you can see different traces of we tried doing 0:06:49.000,0:06:51.000 this. It didn't work. 0:06:51.000,0:06:54.000 Let's go add this feature, etc. 0:06:54.000,0:06:56.000 It started off 0:06:56.000,0:06:58.000 with this guy. 0:06:58.000,0:07:02.000 So I will attempt to pronounce his name. It's Bjarne Stroustrup. 0:07:02.000,0:07:05.000 I've been told that the way to pronounce this is to say Stroustrup with a 0:07:05.000,0:07:10.000 Norwegian accent while cramming a potato down your throat. I'll 0:07:10.000,0:07:10.000 just 0:07:10.000,0:07:14.000 get it close. Anyway, he's a Danish computer scientist, and he was getting his PhD in computer 0:07:14.000,0:07:15.000 science from Cambridge, 0:07:15.000,0:07:19.000 and his specialty was in distributive systems and operating systems. So his goal 0:07:19.000,0:07:22.000 was, let's take some problems, spread it out over several computers, have 0:07:22.000,0:07:26.000 them communicate via network protocol and get some results. 0:07:26.000,0:07:29.000 He chose to write this program in a language called Simula. I 0:07:29.000,0:07:34.000 confess, I don't know much about this language. I haven't heard anyone using it, but 0:07:34.000,0:07:37.000 back then, it was the his object-oriented language, and he said it helped him think 0:07:37.000,0:07:38.000 about the problem. 0:07:38.000,0:07:42.000 It had classes, so you could build a computer object, a protocol object, a 0:07:42.000,0:07:43.000 network object, 0:07:43.000,0:07:46.000 and they'd send messages to each other the same way that a 0:07:46.000,0:07:48.000 physical computer, a physical network 0:07:48.000,0:07:51.000 and a physical protocol would communicate with each other. He 0:07:51.000,0:07:54.000 got it working pretty fast and ran it, 0:07:54.000,0:07:57.000 and much to his surprise, he found out that it was so abysmally slow that he could get no 0:07:57.000,0:07:59.000 practical results out of it. The 0:07:59.000,0:08:01.000 question is what went wrong? 0:08:01.000,0:08:04.000 It wasn't his program. It was the Simula language implementation. 0:08:04.000,0:08:08.000 He actually ran a profiler on his code to figure out why it was going slowly. 0:08:08.000,0:08:10.000 80 percent of the time his program was running, it was going automatic 0:08:10.000,0:08:13.000 garbage collection, even though he was doing [inaudible] memory management. 0:08:13.000,0:08:14.000 So think about this. 0:08:14.000,0:08:17.000 You spend several weeks writing a program. 0:08:17.000,0:08:17.000 You run it, 0:08:17.000,0:08:20.000 and for every line of code you're writing, four lines of meaningless code 0:08:20.000,0:08:24.000 that someone else wrote are also executing. That's really slow. So 0:08:24.000,0:08:27.000 he had to change approaches. He went and rewrote this entire program in a 0:08:27.000,0:08:30.000 different language called BCPL, which is related to C and the B 0:08:30.000,0:08:33.000 programing language that came before C. 0:08:33.000,0:08:36.000 Of course, it's very low-level, it's very fast, but it didn't have these nice 0:08:36.000,0:08:37.000 high-level features in it. 0:08:37.000,0:08:39.000 He got it working, but it took a lot of time. 0:08:39.000,0:08:42.000 When it was done, he decided, I'm never going to tackle a 0:08:42.000,0:08:46.000 problem like this again until I have a proper tool for the job. He 0:08:46.000,0:08:49.000 ended up at AT&T Bell labs, which is the same place that the C programming language came 0:08:49.000,0:08:53.000 out of. In fact, he knew Kernighan and Ritchie, who invented that, 0:08:53.000,0:08:55.000 and came up with this language called C with classes. It was 0:08:55.000,0:08:58.000 a precursor to C++, and people loved it. 0:08:58.000,0:09:02.000 It combined the best features of Simula, which is this object-oriented design, 0:09:02.000,0:09:04.000 with the best features of C, which is the runtime efficiency. 0:09:04.000,0:09:06.000 It was something that was fast, 0:09:06.000,0:09:09.000 flexible and helped you think about problems. 0:09:09.000,0:09:11.000 What he would do is work on this implementation. 0:09:11.000,0:09:14.000 He had people actually using the C++, and when they needed new 0:09:14.000,0:09:15.000 features, they said, 0:09:15.000,0:09:17.000 Bjarne, it doesn't work. He'd go fix it. 0:09:17.000,0:09:20.000 We had this cycle of real programmers solving real problems 0:09:20.000,0:09:23.000 with this developer saying, okay, let's go fix it. 0:09:23.000,0:09:26.000 After a while, you ended up with C++. I think this 0:09:26.000,0:09:28.000 is the 25th anniversary of C++, so 0:09:28.000,0:09:30.000 how exciting. 0:09:30.000,0:09:33.000 I want to talk about the philosophy a little bit. What is really driving the 0:09:33.000,0:09:36.000 development of this language? What does it expect out of you? 0:09:36.000,0:09:39.000 So I have some quotes from this book called The Design and Evolution of C++. It's 0:09:39.000,0:09:40.000 a good book. You should read it. 0:09:40.000,0:09:42.000 The first one, 0:09:42.000,0:09:45.000 C++'s evolution should be driven by real problems, and you don't 0:09:45.000,0:09:47.000 want to get down on a quest for perfection. 0:09:47.000,0:09:51.000 Basically, this language is designed to solve real problems that 0:09:51.000,0:09:53.000 real programmers like you are going to run into 0:09:53.000,0:09:55.000 in not necessarily the best way. 0:09:55.000,0:09:56.000 It tries to do a good job. 0:09:56.000,0:10:01.000 It's not intended to be perfect. There are some sloppy edges, but it works. 0:10:01.000,0:10:04.000 Because it is driven by real problems and real design issues, you can solve 0:10:04.000,0:10:06.000 real problems with it. 0:10:06.000,0:10:07.000 If you think that's a good thing in a language, 0:10:07.000,0:10:11.000 I personally do. I think it's good to have a language that can solve problems, 0:10:11.000,0:10:14.000 then C++ is a good choice. 0:10:14.000,0:10:17.000 This is probably the one that you've seen the most. Don't try to force people. 0:10:17.000,0:10:21.000 C++ gives you so many choices that your question should not be, 0:10:21.000,0:10:24.000 how do I do this in the language, but more, which of these hundreds of 0:10:24.000,0:10:26.000 options is the best choice for me? 0:10:26.000,0:10:29.000 It really trusts you as a language. It will give you an incredible amount of 0:10:29.000,0:10:33.000 flexibility, even to the point where it will let you make the wrong decision. 0:10:33.000,0:10:36.000 I've always thought C++ is a language that will let you ride a 0:10:36.000,0:10:37.000 bicycle without a helmet 0:10:37.000,0:10:40.000 because that one time that you need to go under a bridge that's exactly a quarter inch over your 0:10:40.000,0:10:43.000 head, you wont have your helmet knock you off your bike. You 0:10:43.000,0:10:45.000 do have to worry about a lot of risks, 0:10:45.000,0:10:48.000 but in the end, you'll have more flexibility than you'll find in most other 0:10:48.000,0:10:50.000 languages you 0:10:50.000,0:10:52.000 see. Finally, I think the most important one 0:10:52.000,0:10:54.000 is this. 0:10:54.000,0:10:56.000 C++ makes programming more enjoyable 0:10:56.000,0:10:58.000 for serious programmers. 0:10:58.000,0:11:01.000 The two things here are serious programmers and more enjoyable. 0:11:01.000,0:11:04.000 This is a professional language. It's used in industry everywhere. 0:11:04.000,0:11:05.000 It's very fast. 0:11:05.000,0:11:08.000 It's very efficient. It has a bit of a learning curve. It is kind of 0:11:08.000,0:11:11.000 tricky to get into the language, but once you've got it, 0:11:11.000,0:11:13.000 the second part, the more enjoyable, is so true. 0:11:13.000,0:11:16.000 This language is fun to write things in. 0:11:16.000,0:11:19.000 Once you've got it down, you will actually step back from your program and say, wow, 0:11:19.000,0:11:22.000 I just wrote something that took every, single word out of this set that I had 0:11:22.000,0:11:24.000 [inaudible] contains a specific letter 0:11:24.000,0:11:25.000 in a single line of code. 0:11:25.000,0:11:29.000 Or I lettered all the contents of this file into my set in one line of code. Or I 0:11:29.000,0:11:31.000 just wrote a template that 0:11:31.000,0:11:34.000 is a multi-dimensional ray of any dimension I want. 0:11:34.000,0:11:37.000 Those are not trivial accomplishments, and you can do it with 0:11:37.000,0:11:38.000 this language. 0:11:38.000,0:11:39.000 It's fun. 0:11:39.000,0:11:41.000 It's a great language, and if you think that this sort of philosophy is what you 0:11:41.000,0:11:43.000 want, where it will trust you, 0:11:43.000,0:11:46.000 it will really let you make the decisions rather than forcing you into any one 0:11:46.000,0:11:47.000 paradigm, 0:11:47.000,0:11:48.000 learn C++. 0:11:48.000,0:11:51.000 I think you'll love it. So 0:11:51.000,0:11:55.000 philosophy. There's lots of it, but I'm not here to teach a philosophy 0:11:55.000,0: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 0:11:58.000,0:12:00.000 language look like? 0:12:00.000,0:12:02.000 The first thing I want to do is talk about 0:12:02.000,0:12:04.000 what Genlib.H has. 0:12:04.000,0:12:06.000 Since day one, you probably have seen 0:12:06.000,0:12:07.000 things like this. How 0:12:07.000,0:12:10.000 to include Genlib. Very first line, right here. 0:12:10.000,0:12:14.000 What is in this Genlib thing? Has anybody actually looked at Genlib before? Actually, pulled up 0:12:14.000,0:12:20.000 the Genlib file and looked inside? No one? Okay. 0:12:20.000,0:12:22.000 If you look inside Genlib, 0:12:22.000,0:12:24.000 it looks mostly like this. 0:12:24.000,0:12:27.000 There are basically three important lines you need to know. 0:12:27.000,0:12:28.000 Time includes string. 0:12:28.000,0:12:31.000 So we talked to you about the string classes that was a built-in type, like Ent or 0:12:31.000,0:12:35.000 double. It's a class. It's like any other object, like a vector or a map. You do need 0:12:35.000,0:12:36.000 to pound include it. 0:12:36.000,0:12:39.000 We just took care of it for you because since you use it everywhere and it's easy to 0:12:39.000,0:12:42.000 forget and you can get some pretty nasty compiler errors if you don't, 0:12:42.000,0:12:44.000 we thought, 0:12:44.000,0:12:47.000 we'll be nice. We'll do that for you. 0:12:47.000,0:12:48.000 The second thing is this error function. 0:12:48.000,0:12:52.000 You've seen error. Genlib just gives you a prototype for it. The end. 0:12:52.000,0:12:56.000 But this last one right here using name space STD. Using the standard 0:12:56.000,0:12:58.000 name space. What on 0:12:58.000,0:13:00.000 earth is this line? 0:13:00.000,0:13:03.000 It looks almost like an English sentence. 0:13:03.000,0:13:05.000 Compiler, make my code work. 0:13:05.000,0:13:07.000 What's going on here? 0:13:07.000,0:13:10.000 Well, I'll show you. 0:13:10.000,0:13:14.000 Suppose I have this little program back here. Can anybody see this? So I want to pound include [inaudible] 0:13:14.000,0:13:17.000 and pound include string. 0:13:17.000,0:13:19.000 If you notice here, I don't have Genlib, and I'm going to make 0:13:19.000,0:13:22.000 a string and print it out. 0:13:22.000,0:13:26.000 So the question is, when I say string, my string, 0:13:26.000,0:13:28.000 what am I referring to? 0:13:28.000,0:13:31.000 Well, when this happens, the compiler says, well, go look for something called 0:13:31.000,0:13:32.000 string. 0:13:32.000,0:13:34.000 When we pound included string, 0:13:34.000,0:13:38.000 it put it inside this big bucket of something called name space standard. 0:13:38.000,0:13:41.000 The idea is that the real name of string is standard string. The real name of C out 0:13:41.000,0:13:44.000 is standard C out. The real name of endal is standard endal. 0:13:44.000,0:13:47.000 It's just so that if you make your own versions of those things, it doesn't have 0:13:47.000,0:13:51.000 the same name as the standard. It won't conflict, and your program will still compile. 0:13:51.000,0:13:54.000 But the problem is that if you try to run this, what 0:13:54.000,0:13:55.000 will happen is that 0:13:55.000,0:13:56.000 the compiler says string, 0:13:56.000,0:13:59.000 looks up here and hits this wall. It 0:13:59.000,0:14:03.000 says, oops, that's inside the standard name space. I can't go in there, 0:14:03.000,0:14:06.000 and you'll get this really nasty compiler error, something like, string's not defined. C out's not defined. 0:14:06.000,0:14:08.000 Endal's not defined. 0:14:08.000,0:14:12.000 Now, most of the time when you're programming, you want things like C out and 0:14:12.000,0:14:14.000 string to actually exist. 0:14:14.000,0:14:17.000 So rather than saying you have to call it standard string, standard C out, standard 0:14:17.000,0:14:19.000 endal, 0:14:19.000,0:14:22.000 you can introduce this little phrase right here, 0:14:22.000,0:14:24.000 using name space standard. What it says 0:14:24.000,0:14:26.000 is take this wall and get rid of it. 0:14:26.000,0:14:29.000 It's like, Mr. Compiler, tear down this wall, 0:14:29.000,0:14:30.000 and the result 0:14:30.000,0:14:34.000 is that this barrier gets a little bit 0:14:34.000,0:14:34.000 transparent. 0:14:34.000,0:14:38.000 It's all still included inside of the standard name 0:14:38.000,0:14:39.000 space, but now it's accessible. 0:14:39.000,0:14:42.000 So when you go, string goes, oh, yeah, that thing, and 0:14:42.000,0:14:44.000 actually compiles. 0:14:44.000,0:14:46.000 Now most of today, I'm not going to be hitting you with C++ and saying, 0:14:46.000,0:14:49.000 you must know this. You must know this. There's just not enough time to 0:14:49.000,0:14:50.000 go over everything, 0:14:50.000,0:14:53.000 but this little line might be something worth committing to memory because it's 0:14:53.000,0:14:56.000 the most noticeable change you'll see when you stop using Genlib. 0:14:56.000,0:14:59.000 If you don't put that line in your code, you'll get lots of compiler errors, and it will 0:14:59.000,0:15:00.000 just scare you. 0:15:00.000,0:15:02.000 So if you put this line in, 0:15:02.000,0:15:06.000 I'd say 90 percent of your compiler problems will go away. 0:15:06.000,0:15:07.000 That's that line. 0:15:07.000,0:15:10.000 That's Genlib, and if you want to take a look, 0:15:10.000,0:15:13.000 if you right this code, you basically replace all of 0:15:13.000,0:15:16.000 Genlib. The first header file you'll have to include is this one, CSTDLIB, the C 0:15:16.000,0:15:18.000 standard library. 0:15:18.000,0:15:22.000 I'm thinking that when the invented the C programing language, it cost them 0:15:22.000,0:15:26.000 several million dollars per consonant or vowel they put in their header files. 0:15:26.000,0:15:30.000 So they put it as small as possible but still readable. The idea 0:15:30.000,0:15:32.000 is that you have these two lines. 0:15:32.000,0:15:33.000 Pound include string 0:15:33.000,0:15:35.000 using the standard name 0:15:35.000,0:15:38.000 space. Then right here in this error function, you just 0:15:38.000,0:15:41.000 print everything to C error, which is like C out. It's just designed for error 0:15:41.000,0:15:45.000 handling. You call exit minus one, which says, quit this program. Report some error to 0:15:45.000,0:15:49.000 the operating system and we're done. 0:15:49.000,0:15:52.000 This is it. This is all that is contained in Genlib, and 0:15:52.000,0:15:54.000 the only two important lines are these two right here. 0:15:54.000,0:15:57.000 Including the string class and using the standard name space. If you do 0:15:57.000,0:15:58.000 that, 0:15:58.000,0:16:00.000 and you get [inaudible] Genlib, 0:16:00.000,0:16:03.000 you're going to be in perfectly good shape. 0:16:03.000,0:16:06.000 Now, Genlib is probably one of the easier ones, but what about some of these other 0:16:06.000,0:16:08.000 headers you've seen? 0:16:08.000,0:16:11.000 I could go into some detail about all of these, but I don't have 0:16:11.000,0:16:14.000 time. I really wish I could show you how these work, 0:16:14.000,0:16:17.000 but I'm going to give you a quick overview today 0:16:17.000,0:16:19.000 to show you what you'll need to know if you want to get off these 0:16:19.000,0:16:21.000 libraries. 0:16:21.000,0:16:25.000 The first one is [inaudible]. This is the one that gives you string to integer, 0:16:25.000,0:16:30.000 integer to string, string to reel, convert to uppercase, convert to lowercase, etc. 0:16:30.000,0:16:32.000 Behind the scenes, most of this stuff is backed by this class called a string stream. You've seen 0:16:32.000,0:16:33.000 [inaudible] 0:16:33.000,0:16:37.000 streams, you've seen - they're all streams. You can do 0:16:37.000,0:16:39.000 less than, you can do greater than. 0:16:39.000,0:16:42.000 The point of the string stream, it does the same thing, except that 0:16:42.000,0:16:44.000 it writes to a string buffer. 0:16:44.000,0:16:48.000 So you can take some integer, dump it into a string, that's your integer to 0:16:48.000,0:16:49.000 string function right there. 0:16:49.000,0:16:52.000 There's a couple other things you can do with it. We use it for doing 0:16:52.000,0:16:52.000 0:16:52.000,0:16:56.000 conversion between strings in other data types like integers. 0:16:56.000,0:16:59.000 So you might want to look into that if you want to play around with it. 0:16:59.000,0:17:01.000 As for convert to upper or lower case, 0:17:01.000,0:17:04.000 there are these functions, two upper and two lower, that take individual characters 0:17:04.000,0:17:07.000 and convert them to upper or lower case. Just 0:17:07.000,0:17:10.000 fore loop over your string. Call that function every time. 0:17:10.000,0:17:13.000 Presto. Your string is in uppercase. That was pretty straightforward. 0:17:13.000,0:17:16.000 If you're interested, in CS106L, we actually wrote some of these 0:17:16.000,0:17:20.000 functions. If you want to have a working implementation of [inaudible], if you go to 0:17:20.000,0:17:24.000 the CS106L web site, under the code section, there is a working implementation 0:17:24.000,0:17:26.000 there. So if you want to keep using these things, 0:17:26.000,0:17:28.000 just go download that file, take a look at it, 0:17:28.000,0:17:31.000 play around with it, and it should work. 0:17:31.000,0:17:32.000 So 0:17:32.000,0:17:34.000 if you wanted to set up a 0:17:34.000,0:17:36.000 coding environment on some 0:17:36.000,0:17:38.000 Linux machine that 0:17:38.000,0:17:41.000 doesn't have any of the standard PC or Mac startup files, 0:17:41.000,0:17:46.000 we can just use that? Yeah, it uses only standard C++, so it will compile everywhere. 0:17:46.000,0:17:49.000 It works pretty well. I think we only defined a few of them, but you 0:17:49.000,0:17:51.000 can see how they work, and you can define it for 0:17:51.000,0:17:55.000 - you can do string to integer, string to double or string to reel pretty easily just be changing a 0:17:55.000,0:18:00.000 couple types around. Okay. 0:18:00.000,0:18:03.000 This is probably the big one you're going to miss. Sympio.H. 0:18:03.000,0:18:09.000 How many of you have ever played around with raw C++ input functions before? 0:18:09.000,0:18:12.000 They're kind of nasty. I've always thought this thing called C in, which 0:18:12.000,0:18:15.000 is the counterpart of C out that does input, is kind of like a rose. It's 0:18:15.000,0:18:19.000 very sweet. It's very delicate, but the second you break it, it stabs you with a thorn, 0:18:19.000,0:18:22.000 and it hurts a lot. 0:18:22.000,0:18:25.000 Really, you have to think of this as probably one of the most powerful one of 0:18:25.000,0:18:29.000 the hardest-to-use input libraries out of any programing language you'll see. 0:18:29.000,0:18:32.000 We gave you this Sympio library just to take care of all these things for you so 0:18:32.000,0:18:34.000 you don't have to worry about 0:18:34.000,0:18:36.000 it. 0:18:36.000,0:18:39.000 Everybody remember get line? You can use it to get a line out of a file? 0:18:39.000,0:18:43.000 You can use get line on this C in stream to do input reading. 0:18:43.000,0:18:44.000 Real line as text 0:18:44.000,0:18:46.000 uses string stream to convert it to an integer, 0:18:46.000,0:18:49.000 plus or minus a couple extra things. That's get integer. 0:18:49.000,0:18:52.000 Again, if you go to the CS106L web site, there is a working implementation of 0:18:52.000,0:18:53.000 this. 0:18:53.000,0:18:56.000 If you do want to consider doing C++ beyond this, it actually is 0:18:56.000,0:19:00.000 a pretty good set of input functions. You'd be surprised how many people don't 0:19:00.000,0:19:01.000 actually know how to write these things. 0:19:01.000,0:19:04.000 Go fire it up. Have some fun, 0:19:04.000,0:19:05.000 see what they do and 0:19:05.000,0:19:07.000 that way, you continue using the coding conventions that you've seen 0:19:07.000,0:19:12.000 but by only using standard C++. What about 0:19:12.000,0:19:14.000 random? 0:19:14.000,0:19:17.000 The random functions in C++ are 0:19:17.000,0:19:21.000 pretty simple. There's two functions, rand and Crand. 0:19:21.000,0:19:23.000 Random and C, the randomizer. Again, 0:19:23.000,0:19:27.000 consonants are expensive, vowels are expensive. 0:19:27.000,0:19:30.000 Rand gives you random integers in the range zero 0:19:30.000,0:19:31.000 to rand max. 0:19:31.000,0:19:34.000 So if you want to get a random double, you want to get a random integer, a random Goulian, 0:19:34.000,0:19:39.000 just take the number in that range, scale it down to zero one, scale it back up, translate it, 0:19:39.000,0:19:40.000 etc. 0:19:40.000,0:19:42.000 It turns out that this is probably the easiest one to rewrite from scratch. You just have to 0:19:42.000,0:19:46.000 be a little bit careful of how you do our bounce checking, but it's not so bad. 0:19:46.000,0:19:49.000 The header file for that is C standard library, but you can probably build 0:19:49.000,0:19:53.000 this one up from scratch pretty simply. 0:19:53.000,0:19:56.000 The one that you actually are going to be missing is this one. 0:19:56.000,0:19:58.000 Graphics.H and extended graphics. 0:19:58.000,0:20:02.000 Unfortunately, C++ does not have a standard graphics library. It 0:20:02.000,0:20:05.000 just doesn't exist. There's a couple reasons. One, it's very hard to 0:20:05.000,0:20:08.000 standardize graphics. You have to make sure that it's something that works on every, 0:20:08.000,0:20:10.000 single platform, which is very hard to do. 0:20:10.000,0:20:13.000 Also, if you're writing C++ code from a microprocessor where 0:20:13.000,0:20:16.000 you don't have graphics, it would be difficult to support the 0:20:16.000,0:20:18.000 library. 0:20:18.000,0:20:21.000 So there's no standard C++ equivalent, but I bet you this program 0:20:21.000,0:20:24.000 that I'm using right now is written in C++. It's got some kind of 0:20:24.000,0:20:25.000 graphics. 0:20:25.000,0:20:27.000 Most of this is from third-party libraries. I do 0:20:27.000,0:20:29.000 some Windows programing, so I use - for the Win32 API, which does some 0:20:29.000,0:20:31.000 graphics stuff. 0:20:31.000,0:20:35.000 There's a bunch of cross-platform ones like open GL. You can do X Window system. 0:20:35.000,0:20:36.000 I think Mac's is called Carbon. I 0:20:36.000,0:20:38.000 might be wrong about that. 0:20:38.000,0:20:41.000 [Crosstalk] Yeah, 0:20:41.000,0:20:44.000 there's a lot, and you won't have trouble finding it. You just won't 0:20:44.000,0:20:47.000 have a standard library that does it for you. So shop around with this. The 0:20:47.000,0:20:48.000 0:20:48.000,0:20:50.000 ones we give you are pretty nice. 0:20:50.000,0:20:53.000 I might tell you how they work except that I don't know because it's 0:20:53.000,0:20:55.000 really, really hard. 0:20:55.000,0:21:00.000 So take a look around, and you'll find some ones that are quite nice. 0:21:00.000,0:21:02.000 Everything I've shown you up to this point are simple, like 0:21:02.000,0:21:03.000 how to read input, 0:21:03.000,0:21:04.000 how to convert to a string, 0:21:04.000,0:21:07.000 things that, while they're nice and important, are not going to 0:21:07.000,0:21:08.000 make or break your program. 0:21:08.000,0:21:11.000 The thing that is going to make a difference are 0:21:11.000,0:21:12.000 these ADTs. 0:21:12.000,0:21:14.000 The vector, the set, the map, 0:21:14.000,0:21:15.000 the stack, the queue. 0:21:15.000,0:21:16.000 Those are important. 0:21:16.000,0:21:17.000 If you don't have a map, 0:21:17.000,0:21:20.000 the number of things you can do in a program drops exponentially. 0:21:20.000,0:21:24.000 It's really impressive how much you need these data structures. 0:21:24.000,0:21:27.000 The good news is that C++ not only has a very good support for these data 0:21:27.000,0:21:31.000 structures, it has probably the best library in any standard programming 0:21:31.000,0:21:32.000 language 0:21:32.000,0:21:33.000 that does these things for you. 0:21:33.000,0:21:37.000 It's called the Standard Template Library, which is the 0:21:37.000,0:21:41.000 STL. For every, single container class you have seen in this class, all the ADTs, 0:21:41.000,0:21:42.000 you will find them in the STL. 0:21:42.000,0:21:46.000 The names might be a little bit different, the syntax is a little bit weirder, but conceptually, 0:21:46.000,0:21:49.000 it's the same thing. A vector is a vector, no matter what language 0:21:49.000,0:21:51.000 you write it 0:21:51.000,0:21:54.000 in. The STL 0:21:54.000,0:21:57.000 on top of that gives you these things called algorithms. 0:21:57.000,0:22:00.000 I'll talk about this in a little bit. 0:22:00.000,0:22:02.000 Basically, it's the best thing since sliced bread. 0:22:02.000,0:22:06.000 I'm not kidding. It's really wonderful and amazing, and I'll talk about that in a 0:22:06.000,0:22:07.000 little bit. 0:22:07.000,0:22:10.000 The STL is one of the major features of the C++ standard library. 0:22:10.000,0:22:11.000 It's 0:22:11.000,0:22:12.000 brilliantly designed. 0:22:12.000,0:22:13.000 It's not necessarily 0:22:13.000,0:22:17.000 the most intuitive thing, but once you get a handle of it, it's very 0:22:17.000,0:22:20.000 powerful. 0:22:20.000,0:22:23.000 The way you can visualize what this looks like is as this 0:22:23.000,0:22:25.000 somewhat pyramid 0:22:25.000,0:22:25.000 structure. 0:22:25.000,0:22:27.000 At the very bottom, you've got containers. 0:22:27.000,0:22:31.000 Your data structures, this is your vector, you map, your set. 0:22:31.000,0:22:33.000 You have iterators on top of that. You've all seen iterators in this class. 0:22:33.000,0:22:36.000 You've seen map and set iterators. 0:22:36.000,0:22:37.000 The STL is basically 0:22:37.000,0:22:41.000 all about iterators. Every, single container class has 0:22:41.000,0:22:42.000 iterators. 0:22:42.000,0:22:45.000 You can iterate over a vector or map or set. 0:22:45.000,0:22:46.000 Those worked 0:22:46.000,0:22:49.000 to build these things called algorithms where they're 0:22:49.000,0:22:51.000 functions that operate on ranges of data. 0:22:51.000,0:22:54.000 It's really impressive what you can do with these things. I'll show you a 0:22:54.000,0:22:57.000 little bit of that 0:22:57.000,0:23:01.000 later. These container classes, the good news is that the names are pretty similar. 0:23:01.000,0:23:04.000 Basically, take the 106 version, make it lowercase, you've got your 0:23:04.000,0:23:08.000 STL. So big vector turns into little vector. Big map turns into little map, 0:23:08.000,0:23:13.000 etc. One exception is grid. There is no STL grid class. It's very easy to 0:23:13.000,0:23:16.000 make one. All you have to do is take a vector and do some math to wrap 0:23:16.000,0:23:20.000 a two-dimensional container into a one-dimensional one. In fact, that's how our 0:23:20.000,0:23:22.000 grid works, so you'll have to build that one. 0:23:22.000,0:23:25.000 Otherwise, everything's taken care for you, and that's nice. 0:23:25.000,0:23:28.000 Again, they have the same fundamental abstractions. You know how to work with a 0:23:28.000,0:23:33.000 stack, so you know how to use the STL stack with a little extra syntax. 0:23:33.000,0:23:34.000 The big thing I will say up front, 0:23:34.000,0:23:36.000 the emphasis in the STL 0:23:36.000,0:23:37.000 is on speed. 0:23:37.000,0:23:40.000 In fact, they are designed to be really, really fast. 0:23:40.000,0:23:45.000 That means their notion of safety is, good one. 0:23:45.000,0:23:48.000 So what this means is you'll have to do your own bounce checking. 0:23:48.000,0:23:50.000 When you're iterating, you'll have to make sure you don't walk off the edge of your 0:23:50.000,0:23:51.000 container 0:23:51.000,0:23:52.000 and stuff like that. 0:23:52.000,0:23:56.000 One of the major reasons we didn't go over the STL in this class 0:23:56.000,0:23:57.000 is that, 0:23:57.000,0:24:00.000 which is if you're trying to figure out how to work with a vector, it's 0:24:00.000,0:24:03.000 really bad if you also have to worry about doing your own bounce checking. 0:24:03.000,0:24:05.000 If you're working with a map and you have to use the STL map, 0:24:05.000,0:24:07.000 you probably wouldn't ever want to use another map 0:24:07.000,0:24:09.000 for the rest of your life. It's pretty complicated. 0:24:09.000,0: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 0:24:12.000,0:24:16.000 fast. 0:24:16.000,0:24:19.000 I want to give you a quick example of what this might look like. 0:24:19.000,0:24:22.000 This is a program I've written using the CS106 vector. I want to go 0:24:22.000,0:24:24.000 change it to use the STL to show you 0:24:24.000,0:24:28.000 it's the same thing with a little bit different syntax. 0:24:28.000,0:24:32.000 The first thing you have to do is say, the Specter.H we gave you, so you have 0:24:32.000,0:24:33.000 to change that to the standard vector, 0:24:33.000,0:24:36.000 which is vector in brackets. 0:24:36.000,0:24:40.000 Number of characters we have changed so far. Three characters. 0:24:40.000,0:24:44.000 Next thing, the big vector becomes little vector. Four characters. 0:24:44.000,0:24:48.000 The only big difference is this add function. Anyone who isn't in 0:24:48.000,0:24:50.000 my class want to guess 0:24:50.000,0:24:54.000 what you would call the function to append something to the end of a vector? Anyone 0:24:54.000,0:24:57.000 want to take a guess? [Inaudible]. 0:24:57.000,0:24:58.000 Append? That would 0:24:58.000,0:25:02.000 be nice. It's actually called push back. 0:25:02.000,0:25:05.000 There's good reason for this. It means all these different containers 0:25:05.000,0:25:08.000 have similar function names. Push back, 0:25:08.000,0:25:09.000 append, potato, 0:25:09.000,0:25:12.000 potato. 0:25:12.000,0:25:15.000 Okay. That's probably about ten or eleven character changes. Then 0:25:15.000,0:25:16.000 to loop over the thing, 0:25:16.000,0:25:18.000 it's exactly what you've seen before. 0:25:18.000,0:25:19.000 There is a size function. 0:25:19.000,0:25:23.000 There are brackets. You can go and iterate. You can go and access things. 0:25:23.000,0:25:26.000 The result, as you can see, is not that different. 0:25:26.000,0:25:28.000 There are a couple subtleties that I didn't go into here about how the 0:25:28.000,0:25:32.000 STL behaves different from the vector you've seen, but overall, it 0:25:32.000,0:25:36.000 is the same container. 0:25:36.000,0:25:39.000 The other one I want to call your attention to is the STL map. 0:25:39.000,0:25:42.000 So if you work with the STL, you need to know two containers, and the rest 0:25:42.000,0:25:44.000 follow from there. If you know vector and you know map, 0:25:44.000,0:25:46.000 you've got the whole thing down. 0:25:46.000,0:25:50.000 The STL map is like our map except that instead of going string to some value, it's 0:25:50.000,0:25:51.000 any key type you want 0:25:51.000,0:25:53.000 to any value type you want. 0:25:53.000,0:25:56.000 So you can map strings to ints, you can map ints to strings, 0:25:56.000,0:26:00.000 you can map stack of queue of int to vector of double. 0:26:00.000,0:26:04.000 I don't know why you would, but you have the ability to do so. 0:26:04.000,0:26:07.000 The problem is that the interface on map 0:26:07.000,0:26:11.000 was probably designed by someone who didn't like people. It's really hard 0:26:11.000,0:26:14.000 to use. Anyone who has used it will tell you that. Once you 0:26:14.000,0:26:17.000 get the hang of it, though, it's pretty intuitive. The result is you have a 0:26:17.000,0:26:22.000 very fast map container that is going to be the backbone of any of the programs that you write. 0:26:22.000,0:26:23.000 It's quite a useful class. 0:26:23.000,0:26:26.000 Again, I don't expect you to memorize every, single bit of syntax here, 0:26:26.000,0:26:27.000 everything I'm saying. 0:26:27.000,0:26:29.000 Just keep it in the back of the mind if this is what you want to look into. 0:26:29.000,0:26:33.000 The map you do need to know. Go take a look at that one if you want to 0:26:33.000,0:26:36.000 pursue C++. Now 0:26:36.000,0:26:37.000 iterators. 0:26:37.000,0:26:40.000 So the iterators you've seen in this class are very nice. They're well-behaved. 0:26:40.000,0:26:44.000 Has next, 0:26:44.000,0:26:45.000 next. Has next, 0:26:45.000,0:26:47.000 next. I'm done. [Inaudible] 0:26:47.000,0:26:50.000 iterators are designed to look like pointers. 0:26:50.000,0:26:53.000 You might wonder why anyone would voluntarily make something look like a 0:26:53.000,0:26:54.000 pointer. There's a very good reason. 0:26:54.000,0:26:58.000 This means they're about as smart as a pointer, which means they know 0:26:58.000,0:26:59.000 nothing. 0:26:59.000,0:27:02.000 So if you want to iterate over a range, you have to have two different iterators. You have 0:27:02.000,0:27:03.000 to say start here, 0:27:03.000,0:27:04.000 stop there, 0:27:04.000,0:27:07.000 keep walking forward until you get there. 0:27:07.000,0:27:08.000 Admittedly, 0:27:08.000,0:27:10.000 it's more work, 0:27:10.000,0:27:12.000 but it's very useful because it means that 0:27:12.000,0:27:15.000 if you want to iterate over a 20-element slice out of a 400 0:27:15.000,0:27:17.000 million element container, 0:27:17.000,0:27:19.000 you can do that. Try doing that with our libraries. You have to go iterate all the 0:27:19.000,0:27:21.000 way up the start, 0:27:21.000,0:27:23.000 then keep iterating more. 0:27:23.000,0:27:26.000 The other thing you need to know about the iterators is that they're read/write. 0:27:26.000,0:27:30.000 So you can actually crawl over a container and update the values with an iterator, 0:27:30.000,0:27:32.000 something that you cannot do with our libraries. 0:27:32.000,0:27:35.000 It's a bit more work. The syntax basically looks like pointer reading and 0:27:35.000,0:27:35.000 writing, 0:27:35.000,0:27:40.000 but the result is that they're much more powerful, and they're a lot more flexible. 0:27:40.000,0:27:43.000 Here's a quick example. 0:27:43.000,0:27:46.000 I just wrote some code here that uses a vector iterator. The thing to take a look at is this 0:27:46.000,0:27:49.000 right here. 0:27:49.000,0:27:53.000 So basically, there's a couple parts to point out. I want to show you this so that if you 0:27:53.000,0:27:54.000 see this later on, 0:27:54.000,0:27:57.000 you'll get it. Can everybody see this? 0:27:57.000,0:27:59.000 The first thing is this begin function. 0:27:59.000,0:28:03.000 We call it iterator, they call it begin. It just says, give me a start iterator. 0:28:03.000,0:28:07.000 Since we need two iterators to define a range, we keep going until we hit this iterator called 0:28:07.000,0:28:09.000 N, which means stop. To 0:28:09.000,0:28:13.000 move the iterator forward, has next and next would be too nice, so it's ++, 0:28:13.000,0:28:15.000 and then to read from an iterator or write to it, you just de-reference 0:28:15.000,0:28:17.000 it. 0:28:17.000,0:28:20.000 So you do star iterator, as if it's a pointer, which in many cases, it 0:28:20.000,0:28:22.000 actually is. 0:28:22.000,0:28:24.000 That's the gist of what this looks like. There's a few more nuances 0:28:24.000,0:28:25.000 and subtleties, 0:28:25.000,0:28:28.000 but if you keep in mind that if you're looking at STL stuff, just treat this 0:28:28.000,0:28:34.000 like pointers, it will make a lot of sense. The 0:28:34.000,0:28:37.000 last thing I want to talk about are algorithms. These things are 0:28:37.000,0:28:40.000 amazing. The idea is that everything's got iterators, 0:28:40.000,0:28:42.000 so if you write functions that work on iterators, they will work on any 0:28:42.000,0:28:43.000 container type. 0:28:43.000,0:28:47.000 So if you want a binary search or a vector, you call binary search. 0:28:47.000,0:28:51.000 It's a prewritten function. You want to sort something? Great. Go call sort. 0:28:51.000,0:28:55.000 You guys have seen permutations before, right? You have to go do recursion pulsing in the 0:28:55.000,0:28:58.000 front, permute the rest, stick it back on, that sort of stuff? 0:28:58.000,0:29:00.000 STL, you can do it in a while loop. 0:29:00.000,0:29:02.000 You can while you next permutation something, 0:29:02.000,0:29:05.000 come up with every, single permutation, and you don't have to write a single recursive 0:29:05.000,0:29:07.000 function ever. 0:29:07.000,0:29:10.000 These sorts of things, I think there's 75 different algorithms, anything 0:29:10.000,0:29:12.000 from permuting to searching to sorting, 0:29:12.000,0:29:15.000 to transforming to rearranging to sorting to splitting. 0:29:15.000,0:29:18.000 You name it, there's probably an algorithm that does it. If 0:29:18.000,0:29:19.000 you play around with the STL, 0:29:19.000,0:29:26.000 you get to see all this stuff. It's a lot of fun. 0:29:26.000,0:29:29.000 I've just been showing you some of the libraries. That's basically all the library changes you need to 0:29:29.000,0:29:29.000 know. 0:29:29.000,0:29:32.000 Basically, everything you want to do is still there. 0:29:32.000,0:29:35.000 The syntax is a little different. Some of the libraries you'll have to write yourself, but it's 0:29:35.000,0:29:39.000 all there. It's not like you have to relearn everything from scratch. 0:29:39.000,0:29:42.000 But the libraries are only as good as the language is, 0:29:42.000,0:29:45.000 and there are a couple of language features of C++ that we didn't really talk about 0:29:45.000,0:29:48.000 in this class. I want to go over some of them because you will see them a lot in 0:29:48.000,0:29:50.000 professional code. 0:29:50.000,0:29:53.000 After all, you cannot use a library any more than you understand how the 0:29:53.000,0:29:55.000 language works. So getting a rough understanding of what this will do 0:29:55.000,0:29:59.000 will make your life easier in the long 0:29:59.000,0:30:01.000 run. The first thing I want to talk about is 0:30:01.000,0:30:06.000 const. You've seen const before in the context of const and my context equals five. 0:30:06.000,0:30:10.000 Just make a global constant. Saying that const makes global constants is 0:30:10.000,0:30:13.000 like saying a computer is something that adds numbers. 0:30:13.000,0:30:13.000 Yes, 0:30:13.000,0:30:17.000 but they can also run Windows, for example. 0:30:17.000,0:30:21.000 Const shows up just about everywhere. It's the little keyword that could. 0:30:21.000,0:30:24.000 You'd be amazed just how useful this little keyword can be. 0:30:24.000,0:30:26.000 The biggest thing that it does 0:30:26.000,0:30:29.000 is helps protect you against your own mistakes. 0:30:29.000,0:30:32.000 If you're writing a program and you know something shouldn't change, tag 0:30:32.000,0:30:33.000 it const 0:30:33.000,0:30:36.000 because that way, later down the lie, if you do accidentally change it, it says, 0:30:36.000,0:30:39.000 that's a mistake. Compiler error. You screwed up. 0:30:39.000,0:30:42.000 It protects you. That should've been a double equal, not 0:30:42.000,0:30:44.000 a single equal, 0:30:44.000,0:30:46.000 those sorts of things. 0:30:46.000,0:30:49.000 You've all probably seen that the C++ compiler doesn't treat anything as sacred. Like, oh, you don't 0:30:49.000,0:30:51.000 want to return 0:30:51.000,0:30:56.000 something? Or something like, oh, wow, you did single equal instead of double equal. 0:30:56.000,0:30:57.000 You're the boss. 0:30:57.000,0:31:00.000 The second you try to break const, it pulls out its big ruler of judgment and 0:31:00.000,0:31:04.000 whacks you on the wrist with it, like shame on your for breaking this constants. It 0:31:04.000,0:31:06.000 is the compiler trying to help you out. 0:31:06.000,0:31:08.000 Please know about this keyword. I'll give you 0:31:08.000,0:31:11.000 an example. See 0:31:11.000,0:31:15.000 this function prototype? Void do something takes a vector by reference. 0:31:15.000,0:31:18.000 So I've passed my vector into this function, and the question is, what's it going to 0:31:18.000,0:31:20.000 hold when it comes back? 0:31:20.000,0:31:22.000 Well, it could be the same. It 0:31:22.000,0:31:25.000 could be completely empty. There could be 137 copies of the 0:31:25.000,0:31:26.000 number 137. 0:31:26.000,0:31:28.000 You don't know. 0:31:28.000,0:31:31.000 In this class, we've shown you, pass by reference can be either for 0:31:31.000,0:31:33.000 efficiency reasons because it's not fast enough, 0:31:33.000,0:31:35.000 or it can be 0:31:35.000,0:31:37.000 because you want to actually change the thing. 0:31:37.000,0:31:40.000 It's difficult to see what this function does because you have no idea 0:31:40.000,0:31:43.000 which of those two it is. 0:31:43.000,0:31:46.000 The idea, though, and this is something that you will see in professional code everywhere, 0:31:46.000,0:31:48.000 is doing something like this. 0:31:48.000,0:31:51.000 If I say, void do something const 0:31:51.000,0:31:53.000 vector and my vector, 0:31:53.000,0:31:56.000 that says that this vector can't be modified within the function. 0:31:56.000,0:32:00.000 It's like handing this thing off saying, I'm giving you this value 0:32:00.000,0:32:01.000 for efficiency reasons. 0:32:01.000,0:32:04.000 Donft try to modify it. In fact, you can't modify it. 0:32:04.000,0:32:07.000 It makes your code self-documenting. Someone looking at your functions can go, 0:32:07.000,0:32:09.000 that's const. I can't possibly change anything there, 0:32:09.000,0:32:13.000 and they'll be totally fine handing off the string containing their graduate thesis 0:32:13.000,0:32:14.000 to it, for example. 0:32:14.000,0:32:18.000 If you had your graduate thesis stored as a string, 0:32:18.000,0:32:19.000 which I don't 0:32:19.000,0:32:20.000 know why you would ever do this, 0:32:20.000,0:32:24.000 but if you see a function take the string and parameter, you'd probably be a little 0:32:24.000,0:32:27.000 bit worried. Like, is it going to replace my graduate thesis with, 0:32:27.000,0:32:30.000 hi, I don't have a graduate thesis or what? 0:32:30.000,0:32:34.000 The const is useful. 0:32:34.000,0:32:36.000 The question is, if it's such a useful keyword, 0:32:36.000,0:32:39.000 why didn't we show you this? 0:32:39.000,0:32:43.000 The biggest reason is you can't just use const every now and then. You can't say, I'm going 0:32:43.000,0:32:47.000 to mark this parameter const. I'm gong to say that one's const right there. It 0:32:47.000,0:32:48.000 doesn't work that way. 0:32:48.000,0:32:52.000 Let's say I do mark something const. That object has to know how to behave 0:32:52.000,0:32:53.000 when it can't modify itself. 0:32:53.000,0:32:56.000 So you have to make the entire class written const correct. 0:32:56.000,0:32:59.000 Then if it has any data members that are classes, that class has to be const correct. You get this 0:32:59.000,0:33:00.000 0:33:00.000,0:33:01.000 exploding effect where 0:33:01.000,0:33:05.000 you stick a singe const in, and suddenly every, single piece of your code is marked 0:33:05.000,0:33:06.000 const. 0:33:06.000,0:33:09.000 That's a good thing because it makes your code self-documenting, 0:33:09.000,0:33:13.000 but the point is it's an unnecessary language complication. When 0:33:13.000,0:33:16.000 you're trying to write the PQ class, you should be worried, how do I build a 0:33:16.000,0:33:17.000 chunk list, not oh, 0:33:17.000,0:33:20.000 is this function const? So in the 0:33:20.000,0:33:23.000 professional world, you will see it. Here we thought, it makes things too 0:33:23.000,0:33:28.000 complicated. We'll give it a pass for right 0:33:28.000,0:33:30.000 now. Another big one. Object copying an assignment. 0:33:30.000,0:33:34.000 You've seen disallow copy. It says, don't copy this 0:33:34.000,0:33:38.000 object. It's not standard C++, but I've been told that Google actually 0:33:38.000,0:33:41.000 has a macro that does something just like this, so you're programming the same way Google 0:33:41.000,0:33:42.000 people do. 0:33:42.000,0:33:43.000 The difference is that 0:33:43.000,0:33:45.000 if you're trying to write a PQ 0:33:45.000,0:33:47.000 and it can't copy itself, 0:33:47.000,0:33:51.000 it's a little bit frustrating. Think about it. You make some PQ. You can make a vector 0:33:51.000,0:33:51.000 copy. 0:33:51.000,0:33:52.000 You can make a map copy. 0:33:52.000,0:33:56.000 If you can't make a PQ copy, you'll be wondering, why not? 0:33:56.000,0:33:59.000 So it turns out C++ lets you redefine the way copying 0:33:59.000,0:34:01.000 works. So these two functions here 0:34:01.000,0:34:04.000 called copy constructors and assignment operators. 0:34:04.000,0:34:07.000 While it's very useful to know how to do this, there's a major reason we didn't tell 0:34:07.000,0:34:08.000 you about it in this class. 0:34:08.000,0:34:12.000 It's because it's really hard. 0:34:12.000,0:34:16.000 I think I spent, in 106L, an entire week going over this. 0:34:16.000,0:34:18.000 We don't have a week to tell you how to do this. 0:34:18.000,0:34:21.000 Here's a list of some of the things you have to keep in mind when writing these 0:34:21.000,0:34:21.000 functions. 0:34:21.000,0:34:25.000 [Inaudible] to clean up your own memory, not memory you don't own. To clean 0:34:25.000,0:34:28.000 up the memory, to copy an object, to copy the object correctly, to handle 0:34:28.000,0:34:31.000 these [inaudible], to follow the same rules that C++ syntax dictates. 0:34:31.000,0:34:34.000 There's an awful lot of stuff going on there. 0:34:34.000,0:34:37.000 To expect you to get all this right when you're worrying about your PQ is just 0:34:37.000,0:34:37.000 too much. 0:34:37.000,0:34:40.000 It really is. 0:34:40.000,0:34:41.000 Imagine it's the day before it's due. 0:34:41.000,0:34:43.000 You've got the PQ working beautifully, and 0:34:43.000,0:34:45.000 it doesn't copy 0:34:45.000,0:34:49.000 right. What does that tell you? You've written your copy function wrong, but you totally 0:34:49.000,0:34:50.000 understand and made your point, 0:34:50.000,0:34:52.000 which is how to build a priority queue. 0:34:52.000,0:34:54.000 This is a chunk list. This is a heap. 0:34:54.000,0:34:55.000 This is an unsorted 0:34:55.000,0:34:58.000 vector. That's what's actually important, not building stuff like this. 0:34:58.000,0:35:00.000 When you're in the professional world, 0:35:00.000,0:35:03.000 you do need to do some extra work to make your objects copy 0:35:03.000,0:35:07.000 correctly. So you should look into these functions a little bit. 0:35:07.000,0:35:08.000 Again, to stress, 0:35:08.000,0:35:11.000 the stuff you've learned in here, the actual here's how you build these data 0:35:11.000,0:35:14.000 structures, is more important than these global syntactic nuances that you've got to be 0:35:14.000,0:35:18.000 aware of. 0:35:18.000,0:35:20.000 One more thing. This is pretty cool. 0:35:20.000,0:35:22.000 I have this 0:35:22.000,0:35:23.000 code snippet right here. 0:35:23.000,0:35:24.000 I make a string, 0:35:24.000,0:35:25.000 and I say, 0:35:25.000,0:35:30.000 my string is equal to this is, and then I tack onto it, a string. 0:35:30.000,0:35:32.000 I'm going to iterate over this entire string, 0:35:32.000,0:35:35.000 and every step, I'm going to print out the current character. So 0:35:35.000,0:35:37.000 this is just print out the string 0:35:37.000,0:35:39.000 one letter at a time. Ifm going to 0:35:39.000,0:35:42.000 highlight a few things. 0:35:42.000,0:35:45.000 Why is it legal to [inaudible] equals a string with something else? 0:35:45.000,0:35:48.000 Why string but not priority queue? Why are we allow to do this? 0:35:48.000,0:35:50.000 It's a string. It's an 0:35:50.000,0:35:54.000 object, and we just code plus equals on it. 0:35:54.000,0:35:56.000 Why does it let you do less than, less than? 0:35:56.000,0:35:59.000 What does that mean? Why do streams let you do that but nothing else? Why can 0:35:59.000,0:36:03.000 I read a string with brackets even though it's really an object? 0:36:03.000,0:36:07.000 At a high level, we all know what it means to add something to a string. It 0:36:07.000,0:36:11.000 means take something and stick it on. You know what this less than, less than. It means put into 0:36:11.000,0:36:14.000 a stream. 0:36:14.000,0:36:18.000 [Inaudible] C++, I want to define these operators for my classes. 0:36:18.000,0:36:21.000 It's a technique known as operator overloading. 0:36:21.000,0:36:25.000 Basically, all you do is write functions that are called operator and 0:36:25.000,0:36:28.000 whatever the name of your operator is. So operator equals, operator brackets, operator 0:36:28.000,0:36:29.000 less than, less than. 0:36:29.000,0:36:30.000 Operator plus, plus. 0:36:30.000,0:36:34.000 It's just a syntax convenience. You can write code that looks more 0:36:34.000,0:36:34.000 intuitive 0:36:34.000,0:36:37.000 that does something complex behind the scenes. For 0:36:37.000,0:36:41.000 example, if I write my vector bracket I, it's not like, oh, it's a bracket. It's much 0:36:41.000,0:36:43.000 faster. It really means, call the function called 0:36:43.000,0:36:45.000 operator brackets. 0:36:45.000,0:36:49.000 Again, it's a convenience and nothing else. If you treat it as anything more than a 0:36:49.000,0:36:51.000 convenience, you can kind of hurt yourself with it. 0:36:51.000,0:36:54.000 You can overload basically every operator in C++. You can 0:36:54.000,0:36:58.000 overload things like bit wise [inaudible] with assignment. You can overload the 0:36:58.000,0:37:01.000 exore operator. You can overload parenthesis, and, comma, 0:37:01.000,0:37:03.000 all these operators that most people never use. 0:37:03.000,0:37:06.000 The ones that you see most frequently, though, are these 0:37:06.000,0:37:09.000 ones. Overloading operator less than, less than for stream insertion. 0:37:09.000,0:37:12.000 You can actually make it so that you can make a vector class than 0:37:12.000,0:37:15.000 can print itself out to the screen. It's kind 0:37:15.000,0:37:17.000 of useful. The assignment operator, operator equals, 0:37:17.000,0:37:20.000 the less than operator for comparing things and the parentheses operator. 0:37:20.000,0:37:23.000 You can actually overload parentheses. It's pretty cool stuff. It's 0:37:23.000,0:37:27.000 actually for something called functors, which is really awesome. If you do play around with 0:37:27.000,0:37:30.000 C++, this is definitely something to keep in mind because it will make your 0:37:30.000,0:37:33.000 life a lot more enjoyable. 0:37:33.000,0:37:36.000 I think more than any other language features, C++, except possibly 0:37:36.000,0:37:40.000 multiple inherence, this is the most widely-criticized 0:37:40.000,0:37:41.000 feature of the language. 0:37:41.000,0:37:44.000 The reason is that you can make things that make no sense legal. 0:37:44.000,0:37:47.000 Like, defining the modular operator for two 0:37:47.000,0:37:50.000 PQ. My PQ, my other PQ. Anybody think 0:37:50.000,0:37:54.000 of a sensible interpretation of what it means to mod one PQ by 0:37:54.000,0:37:57.000 another? If you do, please send me an email or 0:37:57.000,0:37:59.000 shout it out right now. I can't think of one. 0:37:59.000,0:38:01.000 But you can make it legal. 0:38:01.000,0:38:04.000 I don't know why you would, but you can. 0:38:04.000,0:38:05.000 The point of this is, if you think 0:38:05.000,0:38:07.000 about it, call it the philosophy. Let 0:38:07.000,0:38:11.000 the programmer make the choice, even if it lets them choose wrong. 0:38:11.000,0:38:14.000 You can do this. You probably shouldn't do it, but 0:38:14.000,0:38:16.000 by giving you the opportunity to do so, 0:38:16.000,0:38:19.000 it means that when you really do need to write a class that has a module 0:38:19.000,0:38:21.000 that's defined, you can do it. 0:38:21.000,0:38:22.000 It's flexibility. It 0:38:22.000,0:38:23.000 means that you have to 0:38:23.000,0:38:30.000 make sure that what you're doing makes sense, but it's flexibility. You've 0:38:30.000,0:38:32.000 all just finished Pathfinder, right? 0:38:32.000,0:38:37.000 Did anybody templatize the [inaudible] PQ, by any chance? Anyone? You guys 0:38:37.000,0:38:39.000 are smart. 0:38:39.000,0:38:44.000 Here's the thing. Let's say I wrote something that says, A equals B. 0:38:44.000,0:38:47.000 What does this mean? It says assign B 0:38:47.000,0:38:49.000 A. What if I write this in a template function? 0:38:49.000,0:38:53.000 I have a template of some unknown type. I'll just say A equals B. 0:38:53.000,0:38:54.000 What's it mean? 0:38:54.000,0:38:58.000 Well, if it's an int, it means take the int and copy it. If it's a floating point 0:38:58.000,0:39:01.000 number, it says take the floating point number and copy it. If 0:39:01.000,0:39:04.000 it's a string, it says make a string deep copy. 0:39:04.000,0:39:08.000 The point is that every, single time you use this same syntax, A equals B, 0:39:08.000,0:39:11.000 but the result is different, and it's always the correct result. 0:39:11.000,0:39:15.000 This is what you can get with operator [inaudible], the ability to take code 0:39:15.000,0:39:19.000 and make it look right for every, single class so that in templates, you can just 0:39:19.000,0:39:23.000 call the function, and you're guaranteed it will work. 0:39:23.000,0:39:26.000 That's a quick tour of some of the language features. I know this 0:39:26.000,0:39:30.000 might seem like an awful lot. I've been going very quickly through it, 0:39:30.000,0:39:33.000 but want to conclude by saying so what? 0:39:33.000,0:39:37.000 I just harangued you with a lot of language features, a lot of syntax, a lot of 0:39:37.000,0:39:37.000 libraries. 0:39:37.000,0:39:39.000 What's the point? 0:39:39.000,0:39:41.000 The point to take out of this is that 0:39:41.000,0:39:43.000 C++ is big, 0:39:43.000,0:39:46.000 but it's also a very powerful language, and it's a very expressive language. I 0:39:46.000,0:39:48.000 think it's a very beautiful language. 0:39:48.000,0:39:51.000 The features that you've learned in this class will go anywhere you want them to. 0:39:51.000,0:39:54.000 If you want to take your coding skills 0:39:54.000,0:39:55.000 and apply 0:39:55.000,0:39:57.000 them to C++, you can, 0:39:57.000,0:39:58.000 and you have this flexibility. 0:39:58.000,0:40:00.000 You really have this gift. 0:40:00.000,0:40:03.000 What you've learned in here is something most people never learn. It's how to make 0:40:03.000,0:40:05.000 a computer solve a problem for you. 0:40:05.000,0:40:07.000 That's a skill that's going to follow you for the rest of your life, no matter where 0:40:07.000,0:40:09.000 you take it. 0:40:09.000,0:40:12.000 If you're interested in C++, I have a huge number of references here, if you 0:40:12.000,0:40:13.000 want to go see them. 0:40:13.000,0:40:16.000 The point is that if you want to solve a problem with a computer, you need three things. 0:40:16.000,0:40:19.000 First, 0:40:19.000,0:40:20.000 programming skills. 0:40:20.000,0:40:21.000 You all have that. 0:40:21.000,0:40:23.000 You need a good idea. 0:40:23.000,0:40:26.000 I don't think I have any good ideas for programming. If I did, I'd probably do them myself, 0:40:26.000,0:40:29.000 but if you have a good idea, and you want to make $1 billion with it, the 0:40:29.000,0:40:32.000 last thing you need is a programming language. 0:40:32.000,0:40:35.000 Hopefully this quick tour of C++ has showed you. This 0:40:35.000,0:40:38.000 is what this language is. It's a tool for solving real problems 0:40:38.000,0:40:40.000 that trusts you 0:40:40.000,0:40:41.000 and that means that overall, you'll 0:40:41.000,0:40:43.000 have fun doing it. 0:40:43.000,0:40:45.000 So if you want to go and run with this, if you want to say, I'm a good programmer. 0:40:45.000,0:40:46.000 I can do this and 0:40:46.000,0:40:49.000 learn C++. I think you will love it. I think you will enjoy it. I think it will 0:40:49.000,0:40:53.000 be some of the most fun you will have sitting in front of a computer. 0:40:53.000,0:40:56.000 So have fun with this stuff. That's the whole point. If you're in this class, I hope 0:40:56.000,0:41:00.000 you enjoy it. I hope you said, yeah, I can make a computer solve something. I can do graph 0:41:00.000,0:41:01.000 algorithms. I can do graph [inaudible]. 0:41:01.000,0:41:04.000 I can make data structures. I can make a computer program that plays Boggle 0:41:04.000,0:41:08.000 better than I ever could or anyone I know ever can. 0:41:08.000,0:41:10.000 Have fun. That's the whole point of this. 0:41:10.000,0:41:14.000 If you want to do it in C++, come talk to me. I will give you some references. 0:41:14.000,0:41:17.000 Enjoy. Good luck on the final, and I will probably see you around on Friday for the final. 0:41:17.000,0:41:18.000 Enjoy.