Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Standard ANSI C++ header files

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
alloydog
LXF regular


Joined: Thu Apr 07, 2005 8:32 pm
Posts: 600

PostPosted: Mon Jul 17, 2006 11:18 pm    Post subject: Standard ANSI C++ header files Reply with quote

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.
Back to top
View user's profile Send private message
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Tue Jul 18, 2006 9:25 am    Post subject: RE: Standard ANSI C++ header files Reply with quote

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 ...
Back to top
View user's profile Send private message
alloydog
LXF regular


Joined: Thu Apr 07, 2005 8:32 pm
Posts: 600

PostPosted: Tue Jul 18, 2006 9:49 am    Post subject: RE: Standard ANSI C++ header files Reply with quote

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.
Back to top
View user's profile Send private message
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Tue Jul 18, 2006 10:09 am    Post subject: RE: Standard ANSI C++ header files Reply with quote

>>
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 ...
Back to top
View user's profile Send private message
alloydog
LXF regular


Joined: Thu Apr 07, 2005 8:32 pm
Posts: 600

PostPosted: Tue Jul 18, 2006 7:27 pm    Post subject: RE: Standard ANSI C++ header files Reply with quote

sorry, never meant to come across rude... Embarassed

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 Wink

I never though of the man pages - RTFM!
Back to top
View user's profile Send private message
GMorgan
LXF regular


Joined: Thu Jan 12, 2006 6:58 pm
Posts: 684
Location: South Wales, UK

PostPosted: Tue Jul 18, 2006 10:52 pm    Post subject: RE: Standard ANSI C++ header files Reply with quote

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 Wink . 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.
Back to top
View user's profile Send private message
alloydog
LXF regular


Joined: Thu Apr 07, 2005 8:32 pm
Posts: 600

PostPosted: Tue Jul 18, 2006 11:02 pm    Post subject: RE: Standard ANSI C++ header files Reply with quote

I'm glad you explained that.

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

I give it a bash again.
Back to top
View user's profile Send private message
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Tue Jul 18, 2006 11:33 pm    Post subject: RE: Standard ANSI C++ header files Reply with quote

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

twasn't taken as such Smile,

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 Smile



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 ...
Back to top
View user's profile Send private message
GMorgan
LXF regular


Joined: Thu Jan 12, 2006 6:58 pm
Posts: 684
Location: South Wales, UK

PostPosted: Wed Jul 19, 2006 3:38 pm    Post subject: Re: RE: Standard ANSI C++ header files Reply with quote

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.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast