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.
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.
Quote:Original post by ukdeveloper
Is there anywhere online I can find good tutorials for the STL?
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.
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.
#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;}