STL Tutorials?

Started by
7 comments, last by JWindebank 18 years, 10 months ago
Hi, Is there anywhere online I can find good tutorials for the STL? I'm still not fully understanding templates, and I have no idea what
std::iterator
,
std::bitset
,
std::list
etc. do and how to use them. I'd like to avoid expensive books for the time being. I've tried [google], but drawn a bit of a blank so far. Thanks in advance, ukdeveloper.
Advertisement
Quote:Original post by ukdeveloper
Is there anywhere online I can find good tutorials for the STL?


None that I am aware of. A good book (e.g. "The C++ Standard Library, a tutorial and reference" by N. Josuttis along with "Accelerated C++" by A. Koenig and B. Moo) will beat most online tutorials.

std::iterator

There is no such beast. There are, however, std::vector::iterator, std::list::iterator ...

Have you ever used pointer arithmetic? Had a pointer point to the beginning of an array and then incremented it to point to successive elements? Iterators are a generalization of that concept.

std::bitset

Have you ever used an int to hold binary flags? Have you noticed how you can only have (e.g.) 32 of them. A bitset is a class intended to store an arbitrary (but fixed) number of such binary flags.

std::list

It's a run-of-the-mill doubly linked list class.

how to use them

There are STL references available online. Use them. Watch out that SGI's reference is rather outdated and does not exactly reflect the library as implemented on your system. If you're using Visual Studio, check out Dinkumware's site instead (or your compiler docs).

Quote:I'd like to avoid expensive books for the time being. I've tried [google], but drawn a bit of a blank so far.


Sorry. If you have specific questions I'd be glad to answer them, but I don't have the time to actually write a comprehensive STL tutorials.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
SGI tutorials (ctrl+f what you need)
CProgramming tutorials (go to the Standard Template Library section)

Now for some quick explanations:

iterator - an iterator is something that keeps track of a position in a container. There are iterators to go forwards and backwards.

std::vector<int> intVec;
intVec.push_back(1);
intVec.push_back(2);
intVec.push_back(3);

// Normal way
for( int x = 0; x < intVec.size; x++ )
std::cout << intVec[x] << "\n";

// Iterator way
for( std::vector<int>::iterator itr = intVec.begin(); itr != intVec.end(); itr++ )
std::cout << (*itr) << "\n";

In that example, the iterator is a pointer to the index, so you must dereference it to get the value, since you are working with an 'int' iterator.

bitset - a bitset. Think of it as a std::vector<bool>, becuse that's all it really is. I had a post on using it, but it's buried somewhere in some forum [lol]

list - a list is like a vector, expect you will use iterators to access and go though it. It works like a vector, except it's not index based.

Well just a few things I think, I hope I haven't mispoken on anything [wink]
I've not found any really good tutorials or even references for the full STL online. cplusplus.com has a fine reference for the iostream portions only, but most C++ books go over those adequately.

I know you're not interested in books, but in the future, Josuttis' [sp?] C++ Standard Library [which is the standard people suggest] is a fair reference, and a poor tutorial. It is very good at describing the little nuances and gotchas though.

People here are pretty good about responding to questions.
This book isn't too expensive and I find it a useful reference for STL STL Programming from the Ground Up. Although some people obviously hate it by the looks of those reviews! Anyway the examples are striaghtforward and easy to understand, it's nice & cheap too. The first time I found it was in the library at college, so you might be able to get a loan of one from a library.

Quote:Original post by pkelly83
This book isn't too expensive and I find it a useful reference for STL STL Programming from the Ground Up. Although some people obviously hate it by the looks of those reviews! Anyway the examples are striaghtforward and easy to understand, it's nice & cheap too.


I have the book, C++ from the Ground Up and it is the same way! People write horrible things about it, but going though it, I find it quite useful and easy to understand as well. Oh well...[headshake]
My advice: Don't try to undserstand how the STL works yet, first learn howto use it. The inner working of the STL(now called the c++ standard library I believe) is a pretty complex topic. Iterators are a design pattern and as such aren't found in just the STL, it would porbably be worth the time of going to the library and getting some books on design patterns if you want to understand the how and the why behind them. When it come to using just think of an iterator as a your interface to the container, you do operations on it like you would an object instead of doing them on the container itself. Maybe a simple example would help you.

I recomend you just dive into SGI's documentation an use it.
doc's

#include <iostream>   //for std::cout and std::endl#include <vector>     // this include the STL container vector                      // a vector is simply a runtime resizeable array                      // Choosing which container is right for which job is another topic                      // though std::vector and std::list are probably the most used.int main(int argc, char *argv[]){    //first well declare our container, these can hold anytype but for simplicity well choose int    std::vector<int> Vec;        //now we need an iterator to access it with, you can have one or many of these all point to the same container    std::vector<int>::iterator Veci;        //now well add a few items to the vector with its push_back() method    //push_back adds an item to the end of the vector. there are other ways to add thins such as the insert method    //you can add anything that matches the type specified in <>'s earlier    Vec.push_back(10);       int a = 1;    Vec.push_back(a);        for(int i = 0; i < 100; i++) // now well add 100 more elements    {        Vec.push_back(i);    }        //now well use the iterator Veci to access the vector Vec and print out the results    std::cout << "Forward printout" << std::endl;    //Vec.begin() and Vec.end() return iterator to the beggining and end of the container        for(Veci = Vec.begin(); //set the iterator to the begging of the container        Veci != Vec.end();  //stop looping when the iterator is at the end        Veci++)              //set the iterator to the next element    {        std::cout << *Veci << std::endl;   // we use *Veci becuase Veci is a pointer to an element in the vector                                           // So we have to dereference it to get the value from it    }        //Now lets mix things up a little and print them out backwords    //There is more than one way to do this, but ill just go the easy way    //we need a reverse iterator to do this    std::cout << "Reverse printout" << std::endl;        std::vector<int>::reverse_iterator Vecir;    for(Vecir = Vec.rbegin();     //set the iterator to the begin of the container (remeber though these are reverse iterators so rbegin is really end - 1)        Vecir != Vec.rend();      //stop looping when the iterator is at the end        Vecir++)                  //set the iterator to the next element    {        std::cout << *Vecir << " ";   // we use *Vecir becuase Vecir is a pointer to an element in the vector                                      // So we have to dereference it to get the value from it    }        std::cout<<std::endl;       //Removing elements is a little trickier but not much    //lets remove all the even elements while looping though forwards    for(Veci = Vec.begin();        Veci != Vec.end();        Veci++)    {        //check if element is even        if(*Veci % 2 == 0)         {            //erase returns a valid iterator, if you forget to assign removes value to the iterator             //if you dont remember to do this Veci wont point to a valid element and could cause a crash            Veci = Vec.erase(Veci);        }    }        //Well output that list now    std::cout << "Odd's printout" << std::endl;         for(Veci = Vec.begin();        Veci != Vec.end();        Veci++)    {        std::cout << *Veci << " ";    }                      //Now well erase the entire vector    //erase is overload to erase a single element like earlier    //or a range of element like we're about to do    Vec.erase(Vec.begin(), Vec.end());        //and thats it! hopefully thats enough to get ya started using the STL.        std::cout<<std::endl;        system("PAUSE");	    return 0;}


(more comments in that than I've put in all my source i've written combined lol)
Guys, you've been brilliant. This is just what I need to get me started.

Thanks for all your help,

ukdeveloper.
If you are not aware, there is also Aaron Cox's Tutorials which do well to help explain Templates. :)

This topic is closed to new replies.

Advertisement