(upbeat music)
- [Narrator] Your challenge for this chapter
was to read a text file, decode it,
store its data in a structure and print the results,
all using the STL, The Standard Template Library.
Here's my solution.
The top of this solution looks very similar
to the chapter eight solution,
I've got the size of a buffer, forget line,
I've got the path to the items dot text file which
you'll need to make this match the path
to the file on your system,
this is the path to the file on my system
that I'm recording on here, and I've got a tab character
and this is where it starts to get different.
Here instead of a structure, I'm using a class,
I've got my three data members sku name and description.
I've got constructors here, a default constructor,
a constructor with parameters,
I've got a copy constructor,
a copy operator, and a destructor.
And then I've got this reset function
which is just a convenience function,
it's used by the destructor
and it's used by the default constructor,
and I've got my setters and getters
for my three data members.
So this is all actually more than what I'm going to use
but this is pretty much the minimum
of what I'm willing to do for any real class.
I've got everything that I need here
and then if I need to add some things,
for pretty printing or whatever, I can do that,
but this is in my view the bare minimum.
And then I've got a couple of the methods out here
because they were just a little bit too big to put in line.
And now the split string because I'm using the STL,
this actually becomes pretty simple and so
I'm actually splitting the string and returning a vector
with each of the separated out strings.
This is a lot easier to do with STL strings and vectors
than it would be with native arrays and C strings,
and that's because I can really just let
the vector class in the string class
do a lot of the heavy lifting.
And so this ends up being pretty simple.
And then down here in main,
again, this all just becomes a little bit more simple.
I've got my character buff, I've got my ifstream,
while in file dot good I called get line,
I split the string using my function up above
and then I use that already split string
to simply populate and construct my object
and then print it out.
Then I close the file and I'm done.
Very very simple.
This was a good challenge to exercise
a lot of your knowledge from this course,
and I hope you took some time to experiment
and learn from the experience.