What do you need to know to use the STL?

Started by
7 comments, last by xiuhcoatl 19 years ago
Hi, I was going to make a long post asking all about how to use the stl, but really at the end of the day I just need some examples of how it's used and what (those uses) do. I realise eventually I probably will need/want to know how it actually works ( I have already been trying with that), but at the moment I think I'd actually learn better by seeing how it's used. so examples of, 1) setting up, initialising etc. 2)usage, ie function calls, using an iterator, whatever..? 3)Maybe some of the rules as well, like is there a special way you have to set up your classes etc? (edited) I'm not really even asking for difficult thngs, just some basic usages or any rules you have to know about etc. thanks. maybe this can help any one else as well.
Advertisement
I am sure there are some decent tutorials out there but the best references I have (and use) are Stroustrup's book (it covers templates, not real well - but its still the best C++ reference imho), "STL Programming from the ground up" by Herbert Schildt, and Effective STL (Scott Meyers) (http://www.amazon.com/exec/obidos/ASIN/0201749629/103-5531549-5184651).

There's an online source that would be close to Schildt's book with out a lot of the explanations (http://www.sgi.com/tech/stl/) that is hard to beat too.



Hope that helps,


#dth-0
"C and C++ programmers seem to think that the shortest distance between two points is the great circle route on a spherical distortion of Euclidean space."Stephen Dewhurst
Quote:Original post by Stevieboy
Hi, I was going to make a long post asking all about how to use the stl, but really at the end of the day I just need some examples of how it's used and what (those uses) do.

I realise eventually I probably will need/want to know how it actually works ( I have already been trying with that), but at the moment I think I'd actually learn better by seeing how it's used.

so examples of,
1) setting up, initialising etc.
2)usage, ie function calls, using an iterator, whatever..?
I'm not really even asking for difficult thngs, just some basic usages.

thanks. maybe this can help any one else as well.


An example of adding 5 ints to a vector, sorting it, and printing them back out:
#include <vector>      // for std::vector#include <algorithm>   // for std::sort#include <iostream>    // for std::cout NOT IOSTREAM.H!std::vector<int> vInts;// Add to the vectorvInts.push_back(5);vInts.push_back(42);vInts.push_back(1);vInts.push_back(-17);vInts.push_back(8);// Sort itstd::sort(vInts.begin(),vInts.end());// Print them out:for(std::vector<int>::iterator it=vInts.begin(); it!=vInts.end(); ++it)   std::cout << *it << std::endl;


Quote;

// Sort it
std::sort(vInts.begin(),vInts.end());

// Print them out:
for(std::vector<int>::iterator it=vInts.begin(); it!=vInts.end(); ++it)
std::cout << *it << std::endl;

/quote


Yeah thanks, thats exactly the sort of thing I've been looking for, I was trying to do somethng like that earlier, but I couldn't figure out what all the parameters were etc.


xiuhcoatl, some of the reviews of Effective STL say it goes into it quite deeply that might be a bit too much for me at the moment, I don't know? although it seems to be a reasonable price £21 here. Thanks

Quote:Original post by Stevieboy

Quote;

// Sort it
std::sort(vInts.begin(),vInts.end());

// Print them out:
for(std::vector<int>::iterator it=vInts.begin(); it!=vInts.end(); ++it)
std::cout << *it << std::endl;

/quote


Yeah thanks, thats exactly the sort of thing I've been looking for, I was trying to do somethng like that earlier, but I couldn't figure out what all the parameters were etc.


In the world of *STL* the best thing to do is focus on using the standard library algorithms over explicit loop code, it's generally preferred for a number of reasons so the that code would be:

#include <algorithm> // std::copy#include <iterator>  // std::ostream_iterator#include <vector>    // std::vector#include <iostream>  // std::cout.....std::copy(vInts.begin(), vInts.end(),          std::ostream_iterator<int>(std::cout, "\n"));
Unfortunately, the STL is fairly humongous. Further it's not really as though bits and pieces can be learned/used independantly of one another. That seems to make tutorials for it [imo] fairly ineffective, since they cannot serially go "learn this, good, next that, good..."

Anyways, I just started trying to learn it in seriousness a few weeks ago, mainly trying to overcome similar reservations. I kinda of need to know how things work to be able and use them, and the STL doesn't lend itself to that. My suggestion is to try and just dive in and use it. Experience seems to be the best teacher, as a good bit of learning the STL is just thinking around templates and ranges and some of the generic practices. Like the example above: thinking in "copy range to output" rather than "print each thing" is most of the battle.

The only rule you really need to know to do that is that removing an element invalidates iterators.
There is a book out there you can purchase, and I have been meaning to purchase it myself. I'm not sure of the name, but if you go browse the Books section of this website you'd be sure to find it (or if someone wishes to supply the name). From what I hear from many on here, its probably the best single resource that you can buy on the STL. I learned what I know from online tutorials (mostly found through Google)
and just teaching myself with examples from here.

Good luck to you!
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
Quote:
There is a book out there you can purchase, and I have been meaning to purchase it myself. I'm not sure of the name, but if you go browse the Books section of this website you'd be sure to find it (or if someone wishes to supply the name).


I believe the book you are referring to is Josuttis's The C++ Standard Library.

Jim.
Quote:Original post by Stevieboy
xiuhcoatl, some of the reviews of Effective STL say it goes into it quite deeply that might be a bit too much for me at the moment, I don't know? although it seems to be a reasonable price £21 here. Thanks


Maybe, The Schildt book and the SGI reference was where I got a lot of bang for my buck - however Effective STL talks about best practices. Personally, I like the fact that Effective STL explains *why* you should or shouldn't do something that, even though syntactically correct, is semantically questionable. The price is a little high if you are on a budget - I think you can get it used for about $25-$30 US which is a little better.

If you can you derive from free sources (SGI, GameDev) how to use STL then I would compliment it with a reference that explains how to use STL well. Otherwise go with one of the other references, I am sure they are quite good.


Hope that helps,

#dth-0

"C and C++ programmers seem to think that the shortest distance between two points is the great circle route on a spherical distortion of Euclidean space."Stephen Dewhurst

This topic is closed to new replies.

Advertisement