 What's up guys my name is Mike the coder and today we are going to talk about sets So before we start going over all the functions about sets. Let's just Go over some of the properties So elements in associated containers of a set Sets are basically associative They're referenced by their key and not by their position in the container So it's not like an array where I have like indexes a set There's no there's no position Okay The elements in the container follow a strict order at all times So all inserted elements are given a position in this order So if I if you insert values in the set They're they follow a strict order. Okay The value of a set is also the key used to identify it. So once I insert a value into the set That's how they use it to identify it No two elements in the container can have equivalent keys. That's basically means that if I insert Two things there that are the same. It's it's Still gonna be the same thing, right? So there's there's no duplicates Everything is unique keys. No two elements have the equivalent keys So remember no duplicates and the container uses an object to dynamically storage its needs so that just means that You could like modify like change the size of it by Dynamically allocating it when you add things to it. Okay, so let's actually talk about the first thing Oh, yeah, so there are also parameters you could use If you want to change a comparator, you could do that and you could also change like this allocator type I'm not gonna go over that but you definitely cannot you also could change the type of you want one set to be Okay, so let's actually start with the constructor. So I'm going to actually go. Oh wait, why do you use OBS my bed? Visual Studio, so let's start with the constructor The big top Okay So there's the empty constructor, which just means that you just create Like literally nothing. So if we go back in here, hold up. It's gonna take a little while Hold up Yeah, okay, so if we I'm gonna just delete all the other stuff I had above real quick, so if you want to import a set just do hashtag include parentheses set Okay, so it's like I Use this bits things to include everything But if you want to do a set you would just do that Okay, yeah include and then set. That's what you would do But this bits macro already included everything so that's why I'm not doing that. Okay, um So yeah, so the first constructor is pretty easy. It's just The set left bracket and then integer and then your name of your set. So let's do first That's what they do in the example. So yeah, this just creates like a set that has nothing in it Okay all right, the second one you could do is a range constructor Which tells me you how many elements corresponding from the element you want to add in that range So for that you could what that means is like if I have like another another If I have Let's say I have like Let's say I have an array or a vector See, let's say to copy right, so I have like one two three four If I want to copy like a range between start to the end Into my set I could do that. So I just do I would do uh, let's get the first Do first and I'll do this and I do copy dot begin copy dot end And this will just copy all the range From of copy into first. So then you would have a one two three four. Okay, and then if you want to like change Like how much you want to copy you could do like plus one and that's gonna include from two three two three four The elements of two three four and so on so forth. So yeah, you could do that But uh, let's just make sure this actually works. So we're gonna go here and uh I'm gonna actually go through the line by line because that's how People use the c++. Okay. Um So yeah here So once they have copy copy is one two three four right and first has nothing so then after this First has one two three four see one two three four So that's how you use the copy of the range constructor using the set And let's say I just want to copy like Instead of from beginning to end. Let's say I want to copy like From the second place to the end right Air vector then that's what I would do um, I actually could print this All the values like that So then um, it's gonna print all the values So this is gonna copy from To oh, yeah three to four three and four. Yeah So that only copies three and four because that goes from Beginning plus two so that's gonna copy everything from here and after To the end it's not gonna copy the first two values. It's gonna copy everything after the second value. So yeah Um, so that's the copy That's the range constructor. Um, you guys have a copy constructor. So if I want to create like a let's see Uh, yeah, okay. If I want to create a copy constructor simple. Um So let's say I have I have like two sets Let's say I have this set copy One two three four and then if I want to copy this all I have to do is just pass it and copy And then it should just copy all the values So if I have two sets if I want to copy the first one to the second one, I just create initialize And then I just put the The name of the previous set into the new one that I want to create And it will just copy everything over um Okay, uh, you have a move constructor this is just like Kind of just moves the elements. Um Yeah, I don't I don't think you'll ever need to use that but I think do they have an example of that? They might um I don't even think they do It's kind of old constructor Moves with allocator. So alloc is separated and the elements are moved. Otherwise an element is constructed uh Move set This is a copies constructor. Uh, I mean I we could try it. Um Yeah, okay. Uh, let's see. Let's look at this Why alloc constructor Instance of that type How to use uh, let's actually look up how to use that. I don't actually know how to use the move because like why would Okay, okay. Um Okay, let's I mean we could try it. Okay, so we have copy and then do this I don't actually use this before What was it this? allocator underscore type alloc Yeah, I've never done the move constructor before but if If there is one, yeah, let me know how you guys do use it Uh, they don't have show how to do how to do it actually but yeah, um Alloc allocator object I don't know Let's see move constructor example. I'm pretty sure like delete explicitly delete Yeah, I don't know I don't know. Yeah, I don't know Let me know if you guys know how to use the move constructor because I don't actually know how to use that You probably wouldn't whatever you need to use that Okay, uh, another one is initialize a list constructor So if you want to do that you could just like do this You just create a list here. It should automatically look at your set Yeah, I just use like braces here For your inside your list of values you want to put in and then they'll initialize your set So if you run it, you should have one two three four Yep, one two three four. Yeah, so that's the List constructor. Um, so I think that's all we need to know about constructors In sets another thing you can pass in a comparator class comparator And that's if you want to like compare classes with each other um I'm not going to do that. But like if you have like two objects Where you need to compare them together, then that's a good way to do it So, yeah, you're passing the comparator. Um, let's go back So that was the all of that as a constructor the destructor is called when you I mean if you want to override it it kind of just destroys it um Okay, you could use a copy Equals operator. So that just copies one set to another. So like if I have like, um, Let's say set second Let's say five six seven eight And if I want to change first to become second, so let's print out first first before So this is before copy print it out after copy so if I want to Change first to become second I'll just do first equals second and I'll just copy everything from second into first So then after the copy it should Get changed. So if we go back here, we're gonna have So I have one two three four and then after the copy becomes five six seven eight Okay, um, okay, uh, let's see. Let's go back Let's go back. Uh See they construct your destructor operator Okay, iterators. Uh, this is exactly the same thing. Um, iterators. I just used to like loop through a set So if I want to loop through a set I do, um I think it's like this crap. Okay. Uh, that's actually to use this Yeah, okay, so you just use like begin middle and Um, yeah, this is this is the this is the operator if you want to do it So if I want to loop through a set you use this If I want to use iterators to loop through a set so we have Set iterator it this is the name it And it's gonna be it's gonna be your set name. So for us it's gonna be first Then first that end and a plus plus it and then we just print out star it And that's how you would print out all your values in your iterator So, yeah, that's how you would use an iterator to go through loop through your set and print values okay, um In the new version to see plus plus you don't actually need this you it's just use auto So I can actually remove this and say Auto it is equal to first stop again. It's not equal to First that in plus plus it and it'll do the same thing You want to do for yeah? okay, um, yeah, that's the begin and end right the it iterators are just uh, they're basically like positions that you could start from beginning and go to end I'll print out all the elements If I want to return If you want to print out backwards you could use r begin and r end So it's the same thing, but we're gonna put r begin r to end And then it's going to print everything backwards. So instead of one two three four it becomes four three two one See four three two one Okay, there's also constant iterator so this means that you can't really change the Change the elements so in other iterators if you like do star It equals they'll change the l element But here this iterator See begin it's not going to change the element. So like, um if I were to Put the iterator before R begin right here and then I do like start it is equal to like seven It's going to change the iterator. Oh, wow. I'll do the set How many it was? Oh, dang. I forgot how to hold up. Um, yeah, so it's set and iterator my iterator. Yeah this so like if if you were to In regular iterators, you could actually modify the values By changing the value of the position. So here I've started out with a iterator called first up again So it's going to point to the first position and if I do like It is equal to like seven. Oh wait start it Oh, I can't do this Oh modify l value. Okay. Um There's definitely a way to change the positions. Uh Oh wait, hold up Okay, uh, there's a way to do it if you use the iterator, but Yeah For constant iterators, you can't really change it Uh, yeah, so these cannot modify the contents point to But uh Oh, I think you could do it here. Actually, I have no idea. There's definitely a way to change the value using iterator In a sense change. I think there's definitely a way Oh, okay. You have to like erase and then Erase yeah, okay. So like you can't actually do star it equals You have to like erase elements like Within iterator I want to change the element using iterator. I would just do First dot erase it And then it's going to erase the first value. So if we print this out, okay. Okay. Yeah, hold on So here before the race after the race and after a race So you could actually erase the value using the iterator So if you look here before the erase it would print out 1234 Then I erase the beginning iterator, right? So we had iterator pointed to the beginning position first, which is this one right Because our set has one two three four and has a value one in the first position So then after I did first dot erase It it just erased the value Of one so it became two three four. So yeah, you could erase values with iterators and sets um You cannot erase but with a c C begin So there's like constant iterators. You cannot erase it. So I can't do like uh First dot erase it Like it's not gonna allow me um We're gonna try Printing it out, but I don't think it will allow you. Oh wait Something's not right It shouldn't have allowed you It should not have allowed you to done that constant iterator. Oh Oh, I had a regular iterator. Okay. This is what I did. I created a regular iterator and I set to a constant iterator and that's somehow allowed it to be But you can't do that Like if you create a constant iterator, it will not allow you to that's strange You create a regular iterator and it would actually erase it, but something's really not right Something it should not allow you to do that, but okay, whatever You can't you're not supposed to be able to change that But yeah, um constant iterator. You should not be able to change for c begin and c end For a constant iterator. Uh, there's also constant reverse iterator. Which is the same thing C r begin c r end Which goes from backwards? Yeah, I don't know why it would allow me to erase that See, okay. Now. Now it's not allowing me. Okay. That's weird. That's super weird Yeah So constant iterator constant reverse iterator just means it's pointing to the end of the sequence And you cannot erase use it to erase things Okay, um What else we have we have empty. Okay, so we could check if an iterator is empty. They're not iterator We try to check if it's set as empty So first has values one two three four So if I want to check because I don't think I just died empty I should tell me if it's empty And it should be false Which is zero Yeah so I could do like if it's empty right now it's empty else It's not empty So it should just print out not empty Yeah, it's not empty Okay, uh, let's go to the next one All right, uh, you could return the size So we have uh, we have values one two three four So our size currently is four four elements in our iterator So just print out the size Not our iterator or set So here we'll just have four elements C4 keep in mind if you were to insert the same element twice There's no duplicates. So it's still going to be four values See it's still four values. I inserted four twice Into the first set, but when I print out the size it's still four values Because there's no duplicates Okay, um We have empty we have size max size. Okay, this is a maximum possible size The set can hold Um, I don't actually know what that is actually so print out the maximum possible size it could hold otherwise we'll Throw an exception So maximum possible size a second hold is about 2 million values 214 million values Um, okay, it's no guarantee you could hold that size actually Okay, so if you get 214 million values, something's wrong um, okay insert and delete Um, that's pretty easy. So let's say I want to insert the values one two three four And I don't want to use a list initialize values. I just do insert one Insert first that Let's actually call it my set Make it a lot more insert two And so on so forth So now if I want to print all the values In my set you should have one and two so here I'm gonna Open it and you should have One and two. Yeah, so that's what it has. So that's how you insert the values You just got insert and whatever value you want to insert into your set Um, you could also erase so I could just do a race I think there's two ways to do it. You could erase using a position iterator or you could just use erase the value so Yeah, so one way to do it is Let's say my set has like one two three four I want to erase the first position of my set. I just do My set dot erase my set dot begin plus one Oh, actually just do begin So that's going to erase the first element. So I should have two three and four two three four right Then if I want to erase like the second position from here here to then What's the problem? Uh, hold up. Let's see. Oh, you can't you can't do that. Okay. Uh, there's no positions in In sets iterators So you're gonna have to do like You have to do plus plus hit plus plus so you can't do like plus two or plus three Because there's no positions in sets So you only could like increment it by one so if I want to erase like the Second element I just have to increment it by one and then do a race. So it should have two three four now Oh, wait. Yeah, it should have one three four one three four Yeah, because I incremented the second element here. So then it erased the two and you have one three four because Originally it pointed to the beginning and I added one to the second position. So now it's here You do two and then when I do a race it removes two so now I have one three four So that's what it has and if I want to erase three, I just do it plus plus again Increment my iterator again And then it should is a race three Yeah, you can only yeah, so now I have one two four So it'll it only could It only could get removed like one by one Like I can't just like plus two on the second position in my set Okay, so that there's that um You also could have removed the value So if I want to remove like the value four I could do that Do my set erase four And then I would just have one two three now because the four is gone Yeah, one two three And that's it. Uh first and last is like a range Now let's say I want to erase all the elements I just do erase my set again my set and And then it's just that's that's just gonna erase all the elements You know, I'm just having nothing So, yeah There's like a range you could remove Begin to that Um, there's that. I think that's it Oh, you could do clear. Oh, you could do swap also Uh, so I have two sets I could actually like swap one with the other And I'll like swap them. That's pretty cool If I want to do like uh one two three four five six seven eight And then I print out the first value and then I print out the see the first set I'm gonna do the same thing This is gonna be second set and it's gonna be second set So I print out the first and second set and then I do kind of after swap So then after when I swap it, this is uh, you could do What is this in text? Oh, yeah first on swap. So it would be my set swap Second set Right, so you would put the name of the set the two sets that you want to swap So my set was the first set and second set is the second name of second set So then if you do my set swap second set it swaps And then we could print out So, yeah, um, so First set has one two three four The second set set has five six seven eight So if you swap them Now the first set has five six seven eight and the second set has one two three four. So they're swapped Okay, that's pretty cool. Um We also could clear this just removes everything So if I wanted first set second set, uh After clear, let's clear so I could do my set Dock clear and it just removes everything So then Here we have one two three four and then If I do my set dot clear it clears. So now I just have nothing So, yeah, um What's the second one in place? Okay Uh this constructs it's kind of like insert, but I think it returns Okay, it returns an iterator to the already Insert element so Yeah, so the difference between here and insert is the insert doesn't return Iterator so here it actually returns like a position So if I want to do like, um Okay, let's say I insert like five six Uh, let's remove Okay, let's say do my set Dot in place Five All right So this is going to insert five into the my set, but I also want the position Where it is at so I could do like an auto it so it returns an iterator Then I could check if it's um if it exists or not So it turns like a pair Um, so if it's inserted it actually increases the set by one And yeah, that's it moves or copies. All right. Okay. Uh, so here Yeah, so here it would do uh, we could check it By doing If it dot second is true Then if that's the case Then Not exist Actually, wait Let me see what it actually prints out. I want to see what the iterator actually does. I've never actually used n place um Insert took a pair, right? Oh dang, okay. Uh, I want to see what both of these values are Hmm, why can't I do that? Okay, I'll copy this I think it tells if it's already inserted or not And I think that's what n place does But I'm not a hundred percent sure Okay, so Okay, okay, so Basically, it just returns like the pair uh, the first value is just like If it's is the value that you inserted and the second one if it actually was inserted So here if it's uh If I already inserted twice the second one would be false But it's not so Yeah, it does nothing So like if I inserted four twice The second pair would that it has is that would be false But four would already exist See Four false So this means the value already existed if you inserted twice So there's like there's a pair of two values The first iterator is like the actual value you want to insert The second one is like a Boolean that tells you if it's already inserted or not So because we already inserted four the second one returns false Um, if we didn't insert a duplicate it would have returned true So here it was four already inserted So it prints out who already inserted in my set It's actually set I say four So if we print it out It would say four already exists in the set Yeah Okay, so that's in place. I think in place might be faster than insert actually Um, but let's actually go to insert In place is logarithmic. What is insert? Oh, yeah, logger is still logarithmic. Okay, so like it doesn't even matter Okay, we have in place. We have in place hint It gives you a hint on the position Where it can be inserted Okay, that's pretty cool. I've never done this before Okay, um Well, let's just copy this. I've never actually seen this before Okay, so they use a in place of strings. So let's actually see what this does. I've never done in place hint before Okay, so You have my set they created a const iterator And then they Okay, so it has alpha omega Does it tell you Okay, so the iterator tells you What does it tell you? I think it tells you the location it's at Inserts a new element in the set if it's unique with a hint on the insert position This element is constructed using args as its construction I don't get it Gives you a hint on where it's new. It's a new position Omega pointer Mean it does not really Wait a minute No, I don't think you'll ever need to use this Might not just use in place. What was the point at in place hint? Okay, whatever it would give you like the the location where it's at. I think hint of where it would be An insert position It's pretty weird Okay, um, so we have in place hint a key compared. So here it would compare the keys If you want to do it this way, um Yeah dot key comparator. So I think normally it inserts like Comparator inserts is the smaller sign. So it goes like smallest to greatest We have like one two three four Like if I do like insert one two three four, I would do that. Um, like dot key comparator. So yeah, um I mean we could we could print it out. So So here we have a my set We just have an integer called highest and we don't use it. But we have a key comparator You get your set dot key underscore comparator And what does this print out? Uh I don't I don't it doesn't even show it. I see key my comp. Oh, yeah right here Yeah, it's less it's less than sign. So by default it's a you insert from smallest to largest um Yeah, I don't know why they're printing it this way But yeah, that's how that's how by default is Um value comparator. I think it's also by default. It's smallest to largest So if you do dot value comparator It also inserts smallest to largest um Okay, so these these these are pretty good these matter, um find Basically finds returns the position iterator to Where the where it's found so like If I do uh, let's actually use this example Because I think it's a good example So these these methods are important. Okay. So here they're inserting 10 20 30 40 50 Into this iterator so they have my set and they have iterator it and they insert uh I times 10 which is 10 20 30 40 50 So then if you do my set dot find if you do your set name dot find It actually returns the position of where it's at so here If you look at the iterator It tells you the position where it's at So whether you found it or not And then uh here he erases it. So he erases the 20 So now it's 10 20 30 And then he also erases the value 40. So now it's only 10 30 50 So yeah 10 30 50 um, so the find function is pretty good because uh You can check if a set contains a value or not. So if I do like if uh, my set Dot find 60 Is not equal to my set dot end So my my set dot end is the end value of it. So if I if What I found is not at the end and that means it's not It's in the list. All right, so this I mean 60 is in the list But it's not our error set doesn't have 60 So this is the if statement if you want to see if Your set contains a value so here Whereas if I do my set dot find 10 It's gonna say 10 is in the list Yeah, 10 is in the list. Yeah, because so you would do check if it's not equal to the end So if I find something it's not equal to the end that means it's in the list You also could do if uh, it's equal to end All right, if I try to find this value And I reach the end and that means it's not in the list So if I do like Try to find 60 This is going to show that 60 is not in the list My set dot find is equal equal to the end Yeah, 60 is not in the list So yeah, that's a good part about fine um Count this is good This is a good function to know um Count basically searches the the number of matches That's equivalent to value So if I have like Let's say inserted like 369 right 10 um Count would check if the number of times I've inserted Is an element of my so like if it's uh It basically Counts how many times the element is inside the set. So here if I do Here, so I have 10 20 30 50 if I do My set dot count 10 it tells me how many times 10 appeared in my set So this should just give me one Yeah Um, you would use this normally for like maps But for ideally for anything that you insert like I said, it's always going to be one And even if I do my set dot insert twice Should be one Unless it becomes two That wouldn't make any sense Yeah, it's still one So you can use this to check if your set contains a value also You do dot count It's greater than zero Yeah upper bound Okay, oh lower bound lower bound is like binary search um You could use it for like find the Find the value where it's like just less than it so Lower bound returns the first element which is not considered to go before value. So Yeah, um, it returns an iterator. So here if I do like Let's just copy this example So here I have I inserted 10 20 30 40 50 60 70 80 90 right And if I do a lower bound on 30 it returns the It returns 30 right lower bound basically just is just searching for a value Using binary search So we return 30 Whereas if I do like an upper bound on 60 it would define the Just the value that's just larger than 60. So in our case would be 70 So, yeah, uh, so here it's gonna If I Erase lower to upper. I love this is really confusing What they're doing is they're erasing the range between lower to upper So any every value any value between 30 and Up to 60 So that'll be 10 20 70 80 90. So, yeah This is a confusing thing um Yeah, 10 20 30 70 80 90 So lower bound returns just the value. That's just less than or equal to it. So in this case we return 30 This upper bound is just like the value. That's just greater than 60. So in this case, it's 70 Here they're erasing the range in the positions. So anything before uh before the after the position of lower bound and before the position of the Upper iterator. So it's erasing everything between 20 to 7 20 to 70. Yeah So all these values, that's what it's doing Um, yeah upper bound just returns to the value that's just right Just greater than the current value Yeah, just that's greater than the current value So normally if you wanted to use um, binary search use lower bound Because it's less than or equal to Oh, yeah, it returns an error to the first value that is not less than that Oh, so it's like greater than Yeah, you can use the upper bound or lower bound. It's basically just binary search. Um equal range Returns to bounds of a range that includes all the elements in the list that are equivalent to about Okay, so like, um Um, what is the range? Wait, what? Get range of equal elements Returns their bounds of a range that includes all the elements in the container that are equivalent to about So it returns all elements that are equal to 30 Oh, okay Okay Okay, so like It basically returns the values that are like Just greater than 30 and less than 30 I think And so just greater than 30, but then that's less than 30 So here we have like 10 20 30 40 50 If I do an equal range, it's going to get me the values that are just Let me just get rid of this Yeah, it just gives me 30 to 40 because that's the range If I find a range between for 30, it's like 30 to 40 Whereas if I had like, um, let's see. Let's say I have like 35 40 Then it should return The range of 30 to 35 I think Oh, yeah 30 to 40. Oh, wow That's cool Oh, no, no, let's make our thing 10 20 30 35 40 50 And your set should be 10 20 35 40 50 It should have a 30 to 35. Yeah, okay So just greater than 30 but less than The second value that's just less than it. So what if it was like, I don't know What if I tried like 10 would be 10 to 20? Yeah, okay, so it's just like the value that's just right after it. That's where it's returning It's kind of weird. Okay All right, um Yeah, that's it. That's all the values. That's it We went find count lower bound upper bound equal range and get allocator also Create an allocator of elements. Well, this is like super low level I guess you're going to allocate the number of elements you want in your set Like I only want five five elements and it'll allow you to do that You just do my set dot get allocator to allocate five But yeah, all right, that's all the that's literally all the methods of set Hope you guys enjoyed this video and come subscribe. I'll check you guys later. Peace