introduction to STL

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

introduction to STL

Post by phlox81 » Fri Sep 23, 2005 10:52 am

Some asked for it, i thought about writing a little tutorial...

First, STL is the base of Standard C++, it is guaranteed, to work
on a widespread field of platforms. And, the STL is not perfect,
but offers quite a lot of interesting stuff.

The easiest to begin with, are containers imho.

http://www.linuxsoftware.co.nz/containerchoice.png
^a little overview over the containers which the STL offers.

First, std::vector and std::list are practical showing how to work
with STL Containers:

Code: Select all

#include <vector>
#include <list>
#include <iostream>
#include <algorithm>

//opening standard namespace:
using namespace std;
struct numbers{int num[6];};
// ignore the function & the struct, read the mainfunction first.
// Operator << for output
ostream& operator<<(ostream& out,numbers& num)
{
	for(int i =0; i < 6; ++i)
		out << num.num[i] << ' ';
	out << endl;
	return out;
}
// struct to call for_each
struct output_numbers
{
	ostream& out;
	output_numbers(ostream& out):out(out){};
	void operator()(numbers& num){out << num;}
}
int main()
{
	vector<int> ivec(49);//49 items are reserved in ivec
	list<numbers> ilist; // list containing struct numbers
	for(int i =0; i < ivec.size(); ++i)
	{
		ivec[i]=i;//filling the vector with actual values
	}
	//we want to make a lottery, we cast 6 out of 49, and save them in the list
	//first we mix the vector:
	random_shuffle(ivec.begin(),ivec.end());
	//cast the numbers 10 times
	list<numbers>::reverse_iterator it;//to access the list
	for(int i =0; i < 10; ++i)
	{
		ilist.push_back(numbers());//creating new instance of numbers
		it = ilist.rbegin();//last item of the list.
		for(int j =0; j < 6;++j)
		{
			it->num[j]=ivec[rand()%49];
		}
	}
	// now we print it out to the console:
	std::for_each(ilist.begin(),ilist.end(),output_numbers(cout));
}
Information about STL you also find at www.cppreference.com

to be continued ;)

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Tue Sep 27, 2005 7:13 am

STL Part II ;)

Handling Files.

There are 3 Classes, that do the the job:
fstream, ofstream, and ifstream.
ofstream is for output, ifstream for input,
fstream for both.
For platformindependent directoryaccess you can
take a look at boost::filesystem, which also offers a boost::fstream.

How to read Data from a File line by line:

Code: Select all

#include <fstream>
#include <algorithm>
#include <iostream>
#include <list>
using namespace std;
ifstream FileIn("Main.cpp");
if (FileIn)     // FileIn is valid
{
    list<string> Contents;              // Container for every line

    // reading data as long as its not eof
    for (string ReadString; getline(FileIn, ReadString); )        
        Contents.push_back(ReadString);   // put line at the end of the list
       
    // Print all elements.
    ostream_iterator<string> Out(cout, "\n");
    copy(Contents.begin(), Contents.end(), Out);
}

Post Reply