How can we deal with Optional data on in C++ || Top and latest explanation 2021

Optional data

What is Optional data So they wouldn’t be talking about what happens when we have Data that may or may not be a lot of the times we have like a function that for example returns. Unless they were reading a file but what happens if the file could not be read what happens if we can load the file if it’s not present if the daughter just isn’t in the format that we expect we still need to return something from the functionality the time in that particular scenario you might just return an empty string but that’s not really that great because it doesn’t make much sense I mean one of the file was just empty there should be a way for us to actually see if the doctor was present or if the daughter wasn’t present and that’s why I asked to be optional comes in so as to the optional is near in staples plus 17 and it’s going to be like part of like a 3 part miniseries in which we cover 3 different classes 3 different new types that gives us to deal with daughter that either may or may not be that what is of a type that way not sure about so let’s jump in and take a little bit of a look at some card that could be written one way but now with S. city optional we have a better way of dealing with Donna that may or may not be that so this is going to be a super simple example. Optional data Optional data

I’m starting with absolutely no regard at all and what I’m going to do is vital function which creates a file so this will basically be like restraining from file right or maybe will rename it read file as string for this should return the entire file for example as a strength will take in a cost STD string file part. And then argue here we’ll just have an input file stream. We’ll take in the fall part of what we actually want to read I’ll just quickly include F. strain and yes I will make a decision about how to read files in super bowls and I haven’t done that yet and now that we have our input file stream what we want to do is read it if it’s valid if the file was opened successfully or if it wasn’t we have to handle that as well Sir very simply we can just write if strain and then we’ll read the file here and then eventually will disclose that file for him however if it didn’t work out right we need to handle that as well so usually we might do that and in fact I just write this code a bit differently we would basically retain our string that we rats let’s just say we had an STD string with our results and then we basically read that file over here and then we returned our result right that would be what it would be like if it was successful and then if it’s not successful well what do we return right we could just return an empty string or we could return Estrin constructed like this which is the same exact result but the point is it’s kind of hard for us and our if that file was opened successfully or not because you can imagine if we have our daughter Thea and it’s our rate file as string and we have on file path which is. Optional data Optional data

Which is a doddle taxed if we have that here what how do we check if that was opened successfully will we would basically have to do something like doesn’t equal nothing right and then trade is that that’s not nice at all what I would much prefer is some way of actually knowing if that thought it was rather excessively or not because again maybe that follows that but it was empty that might be valid we need a way to know if it’s not valid answer another solution might be to basically have this palpable in right which was you know success let’s just say right it’s a billing that we passed by reference it’s going to be our output variables by Michael it out success and then if the stream will opens you Optional data Optional data know with that success to true and will retain that otherwise will success without success to Folsom will basically just return an empty string so in that case instead of doing this we need to take in a bowling which is. Basically file opened successfully and then we simply popped back into here right and then we can check that and that obviously raise a lot better than an empty string however it’s still not particularly nice so that is where STT optional can help us Essity optional which we can include by just including the optional had a file here is basically exactly what it sounds like it’s optional as to whether or not the Data is present so we change this to B. S. the optional and then of course we still have a string as out type that is optional and if it is giving me an error here saying that man’s face as he has no member optional. Optional data Optional data

As I mentioned earlier this is a C. ++ 17 feature to make sure that you go into your settings and you’ll compile settings and switch your language version to save us lost 710 and then now what we do is we still treat everything as it was before and somehow I removed the extreme includes some reason so we still do everything that we did before I will get rid of this out successful and we don’t need that anymore and then instead of attending an empty string what we can do is return an empty SAT optional and you can do that just by having Kelly brackets like this and then if we want our string to be returned we simply return result and you can see it’s really that simple and then if we scroll down into main here we don’t need this anymore we don’t need this we changed this to be an STD optional activities train. Right all optionally you could use order right that’s also fine and then what we do is we just check Data. Zdummyz.has value. And if has value it’s true that means that optional has been set and we can basically we’ll just look out to the console file it read successfully. And by the way just a quick note while I was editing this article this is future Chan are talking you can also just ride. Optional data Optional data

If Data because as a bull operators instead of having to write. a .has value if data is all stored totally legit and I think it looks even clean out and my son anyway back to posture otherwise if it doesn’t have value then we know that file was not read successfully serve file north Arkansas okay so we have 2 different branches here based on whether or not that Donna was actually set on and of course to access that we have a bunch of ways to access it as well we can simply use. I’m an hour and then we just have a string that we can also simply the reference that strings or will say this is a string and it it’s just basically taught at the reference like that and we can access it as a normal string so it’s much the same as a smart pointing in terms of if you want to actually access that data and in fact I think that schools are a.a.value which you can use so let’s test this out and see if that works what I’ll do is just create a dummy file here I’ll make sure that I’m in show all files and just in sandbox I will create a new file. I’ll just call it Data. Tax we might just throw in some data into here and then here it is in my solution explorer what I’ll do is try lotus said as Donna. Text of course if I hit I’ll just throw in a little see influx yes that happens all this and close immediately and will just run this program you can see the file was read successfully now let’s just say that file didn’t exist I’ll go ahead and at least it will hit F. 5 and you can see this as popping up yep and. Optional data Optional data

So easily now able to switch on whether or not that file was read successfully as one of the really useful things that we can do with this let’s just say that we’re trying to read the file and we don’t really care too much about whether or not the file was read on off like as in we still care about it but it’s not the end of the wild if the file couldn’t be opened because of the file can be opened or if that particular section of the file wasn’t sat a wasn’t read maybe we have a default value for it that’s also quite common so what we can actually do is have STD string value and we can set this dasa.value for Optional data Optional data all right and what this will do is exactly what it sounds like if the if the daughter is actually present in that Essity optional it will return to us that string if it’s not it will return whatever value we possibly here so we can say you are not present for example right. This is really useful for if you actually pausing files and trying to for example extract like variables or any kind of elements that have been set because quite often you might have something that is literally optional to have in the file and if a certain parameter was not set in the file you can go with a default value of what you specify here this is very useful because for example we could just simply have an Antioch counts for example maybe which are on a bench marking in the file it says how many times from the bench mark now if this was present in the file we can extract that counts right by just calling counts dollar value or if it wasn’t present we can go with a default value of 100 and. Optional data Optional data

You can see how we can do that because this case this would come from the file thank let’s test that out as well what we’ll do is we will log about value and of course since the file does not exist what we should see in this case I’ll just log and line as well what we should see in this case is not present as you can see that’s what we get not present because the files couldn’t be our friends all right so have you guys enjoyed this article hopefully it helps you out a city optional as I mentioned it’s incredibly useful and you can see that it really does help kind of simplify your card and hopefully let you produce more readable card you guys enjoy this article can also help support everything that I do here on you should vote on a patriarchal full session the channel huge thank you as always to all patrons that make these articles possible leave a comment below. Optional data Optional data

Also See

How to setup C++ on Linux | easy and epic explanation 2021

Leave a Comment

Your email address will not be published. Required fields are marked *