Standard ANSI C++ header files

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Standard ANSI C++ header files

Postby alloydog » Mon Jul 17, 2006 10:18 pm

I have been trying to find a detailed list of the standard ANSI C++ header files (<algorithm> <iomanip> <list> <queue> and so on).

I found several lists, but none explain what each file does and where you would/could/should use it. I have even looked at the ANSI home site, but with no luck.
User avatar
alloydog
LXF regular
 
Posts: 600
Joined: Thu Apr 07, 2005 7:32 pm

RE: Standard ANSI C++ header files

Postby jjmac » Tue Jul 18, 2006 8:25 am

I doubt if gcc.org would have such a list. I think the approach to explanation/description is via the functions themselves rather than the header there declared/defined in. Possibly suggesting that the header isn't a static document. And so could change over time. The function may change in it's form, but it's purpose is likely not to. Just a thought.


jm
http://counter.li.org
#313537

The FVWM wm -=- www.fvwm.org -=-

Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ...
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia

RE: Standard ANSI C++ header files

Postby alloydog » Tue Jul 18, 2006 8:49 am

Hmm, doesn't help...

What I mean is, if you're learning C++, how do you know which headers to use?

Most beginniers tutorials only cover a few, with <iostream> being the most common.
User avatar
alloydog
LXF regular
 
Posts: 600
Joined: Thu Apr 07, 2005 7:32 pm

RE: Standard ANSI C++ header files

Postby jjmac » Tue Jul 18, 2006 9:09 am

>>
Hmm, doesn't help...

What I mean is, if you're learning C++, how do you know which headers to use?
>>

Sorry it didn't help. As for the second part, the various examples that a person may come accross will generally have the relavent headers stated. If not, looking the function up in a 'man' page will show the particular header concerned.

What i was trying to say though, was that the main focus is with the function. Your thought on approching it via a header list isn't really neccessary. And you will find it isn't the approach that is taken. It is possible that the header could change, though, also unlikely at the same time. But, as it is possible, the function itself is the better thing to look at. While its' interface and structure may change, its' purpose wont. So the relevant header will also be stated in any discusion on the function itself.

All the headers in /usr/include/C++/<version>

are worth looking into.

You should find things like <cstdlib>, <cstdio>, <iostream> etc will be commonly needed by most programs. As will the statement ...

using namespace std;

placed up the top of each source file.



jm
http://counter.li.org
#313537

The FVWM wm -=- www.fvwm.org -=-

Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ...
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia

RE: Standard ANSI C++ header files

Postby alloydog » Tue Jul 18, 2006 6:27 pm

sorry, never meant to come across rude... :oops:

I was wondering, for example, if I tried to write a random number generator, say a "simple" die programme, other than <iostream> how would I know which, if any other headers to use?

Yeah, I know I could just web-search "random number generator c++" but that doesn't teach me anything, besides th eusefulness of search engines, that is ;)

I never though of the man pages - RTFM!
User avatar
alloydog
LXF regular
 
Posts: 600
Joined: Thu Apr 07, 2005 7:32 pm

RE: Standard ANSI C++ header files

Postby GMorgan » Tue Jul 18, 2006 9:52 pm

For random numbers you use the rand() and srand() functions. You need the <cstdlib> library header to use them. Basically to get a random number between 0 and RAND_MAX (where RAND_MAX is at least 32767) enter

x = rand();
where x is an integer.

The srand function allows you to seed the random number generator (this is crucial otherwise your random numbers will always be the same). This works like this

srand( y );
again y is an integer.

Generally you see people calling the srand function with the time function. This gives the appreance of randomness because very few people in history could percieve the link between the time and the numbers generated. To use the time function you must add the <ctime> library header. It works like this.

srand ( time( NULL ) );

Be sure to set this at least once in your program but you can use it several times depending on how your program is to work. If you are doing some kind of modelling then each time the model runs a new seeding is probably a good idea. Once you've seeded the generator what you actually get is a series of predetermined but apparently random numbers and the rand() function just outputs them one at a time.

One last thing. To generate a number between a certain range use this equation

x = m + rand() % n;

where the range is m < (m + n - 1). Hope this helps.

Last time I checked 'C++ programming tutorials' gives 2m results, you can't have tried them all ;) . One resource I picked up which is quite complete is thinking in C++ volumes 1 and 2, you can pick them up on PDF for free.
GMorgan
LXF regular
 
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK

RE: Standard ANSI C++ header files

Postby alloydog » Tue Jul 18, 2006 10:02 pm

I'm glad you explained that.

Last time I tired it, all I got was a psuedorandom sequence!

I give it a bash again.
User avatar
alloydog
LXF regular
 
Posts: 600
Joined: Thu Apr 07, 2005 7:32 pm

RE: Standard ANSI C++ header files

Postby jjmac » Tue Jul 18, 2006 10:33 pm

alloydog wrote:
>>
sorry, never meant to come across rude...
>>

twasn't taken as such :),

As above though, it can be very hard to put something together, if it isn't already known. Some sought of example, template will always be needed. Unless you happen to be RMS of course (grin). Pretty much all that is drawn up will be sourced/templated/extended from previous works. So, once the particular function set has been acquired, then the relevant headers naturally follow.

A person doesn't have to limit themselves to C++ examples/templates to write up a C++ expression. Other language units can also be useful.. They will likely be using the same basic functions and logic flow. Pascal units can be helpful there.

The mention of B Eckels (i always spell that name wrong) "Tthinking in C++" is a really good tutorial. His stuff on Java is also worth while too.

>>
Yeah, I know I could just web-search "random number generator c++" but that doesn't teach me anything,
>>

I would have to disagree with that :)



jm
http://counter.li.org
#313537

The FVWM wm -=- www.fvwm.org -=-

Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ...
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia

Re: RE: Standard ANSI C++ header files

Postby GMorgan » Wed Jul 19, 2006 2:38 pm

alloydog wrote:I'm glad you explained that.

Last time I tired it, all I got was a psuedorandom sequence!

I give it a bash again.


Another trick. Because srand takes an integer as an argument and integers are limited in size (4 bytes on most machines, lets call it MAX) it leaves you with only a limited number of combinations for your random numbers. to get away from this when you have a discreet set of items like say a pack of cards use the number generator to shuffle them rather than place them.

It has several benefits because first of all placing a pack of cards via random number generation has a problem with indefinite postponement where it could take a millisecond or it could take 5 minutes to shuffle the pack, reshuffling doesn't have this problem. Also with reshuffling the number of posible outcomes goes up as a power of the posible outputs of rand for every shuffle you make so the first shuffle gives MAX posible packs while the second shuffle gives MAX*MAX and the third MAX*MAX*MAX etc.

Anyway to do this first time fill a 52 card array (in order if need be) then take a loop for 0 < x < 51 then generate a random number y between 0 and 51 for every pass of the loop. Simply swap the values in the array at x and y.
GMorgan
LXF regular
 
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests