The Standard C/C++ Library
The C/C++ Standard Library provides a myriad of built in functionality that is part of the
standard C++ implementation. It includes all of the older Standard C Library. All of this
is accessed from the header files using the #include preprocessor directive.
There are a total of about 50 (at the last count) standard header files. Of these 19 are the
Standard C Library header files. All the others are the Standard C++ Library header files
Naming conventions
In the Standard C Library all of the header files have the .h file extension. For example:
#include <stdio.h>
#include <string.h>
In the Standard C++ Library there are equivalents of the 19 Standard C Library header files.
All of these files have a 'c' prepended to the name and have no file extension (.h). For example:
#include <cstdio>
#include <cstring>
The Standard C++ Library also includes about 30 additional header files that are exclusive to C++ and
do not have a .h on the end of them. (But, that having been said, if you take a look at the actual
header files, usually found in C:\Program files\Microsoft Visual Studio 8\VC\include, you will find
that most of the "cname" files don't do anything but include the standard file. For
example, if you open climits you'll see that about the only non-comment line in the file is
#include <limits.h>.)
Accessing the C++ library functionality
All of the Standard C++ Library is wrapped in the standard namespace std. To have access to it,
therefore you must include a using statement:
#include <iostream> // Include Standard C Library
using std::cout; // Provide access to cout in the Standard Namespace
or
using namespace std; // To get access to all of std namespace
Categories of Headers
The headers in the Standard C++ Library can be organized into a number of categories.
You will note that some headers appear in more than one group. The categories are:
-
C++ Language Support
-
Diagnostic Tools
-
General C/C++ utilities
-
Strings
-
Cultural Formatting Support
-
The Standard Template Library
-
Advanced Numerical Computation
This page does not cover all of the header files in detail, but does give a representative sampling
of the most frequently used headers and the most used functions and data constants defined found in those.
C++ Language Support
Provides
-
Common type definitions
-
Support for program start and termination
-
Support for dynamic memory allocation
-
Dynamic type identification
-
Exception handling
Header files included in this group
-
<cfloat>
-
<climits>
-
<limits>
-
<csetjmp>
-
<csignal>
-
<cstdarg>
-
<cstddef>
-
<cstdlib>
-
<ctime>
-
<exception>
<cfloat>
This header defines a number of useful floating point constants. If you include this header you can refer to
each by it’s defined name. Note that the values defined for long double will be platform
specific.
Defined Name
|
Value
|
Description
|
DBL_DIG
|
15
|
Digits of precision
|
DBL_MAX
|
1.7976931348623158e+308
|
Max value
|
DBL_MAX_10_EXP
|
308
|
Max exponent
|
DBL_MIN
|
2.2250738585072014e-308
|
Min positive value
|
DBL_MIN_10_EXP
|
-307
|
Min exponent
|
FLT_DIG
|
6
|
Digits of precision
|
FLT_MAX
|
3.402823466e+38
|
Max value
|
FLT_MAX_10_EXP
|
38
|
Max exponent
|
FLT_MIN
|
1.175494351e-38
|
Min positive value
|
FLT_MIN_10_EXP
|
-37
|
Min exponent
|
LDBL_DIG
|
15
|
Digits of precision
|
LDBL_MAX
|
1.7976931348623158e+308
|
Max value
|
LDBL_MAX_10_EXP
|
308
|
Max exponent
|
LDBL_MIN
|
2.2250738585072014e-308
|
Min positive value
|
LDBL_MIN_10_EXP
|
-307
|
Min exponent
|
Sample Code
#include <cfloat>
using namespace std;
int main()
{
cout << "The following constants are defined in <cfloat>.\n";
cout << "\t DBL_DIG = \t\t" << DBL_DIG << endl;
cout << "\t DBL_MAX = \t\t" << DBL_MAX << endl;
cout << "\t DBL_MAX_10_EXP = \t" << DBL_MAX_10_EXP << endl;
cout << "\t DBL_MIN = \t\t" << DBL_MIN << endl;
cout << "\t DBL_MIN_10_EXP = \t" << DBL_MIN_10_EXP << endl;
cout << "\t FLT_DIG = \t\t" << FLT_DIG << endl;
cout << "\t FLT_MAX = \t\t" << FLT_MAX << endl;
cout << "\t FLT_MAX_10_EXP = \t" << FLT_MAX_10_EXP << endl;
cout << "\t FLT_MIN = \t\t" << FLT_MIN << endl;
cout << "\t FLT_MIN_10_EXP = \t" << FLT_MIN_10_EXP << endl;
cout << "\t LDBL_DIG = \t\t" << LDBL_DIG << endl;
cout << "\t LDBL_MAX = \t\t" << LDBL_MAX << endl;
cout << "\t LDBL_MAX_10_EXP = \t" << LDBL_MAX_10_EXP << endl;
cout << "\t LDBL_MIN = \t\t" << LDBL_MIN << endl;
cout << "\t LDBL_MIN_10_EXP = \t" << LDBL_MIN_10_EXP << endl;
}
<climits> and <limits>
This header defines a number of useful integer constants including min/max ranges for short, int,
and long. <climits> just #includes <limits>.
Max/min values for int, short, long
Defined Name
|
Value
|
Description
|
INT_MAX
|
2147483647
|
Max signed value
|
INT_MIN
|
-2147483647 - 1
|
Min signed value
|
LONG_MAX
|
2147483647L
|
Max signed value
|
LONG_MIN
|
-2147483647L - 1
|
Min signed value
|
SHRT_MAX
|
32767
|
Max signed value
|
SHRT_MIN
|
-32768
|
Min signed value
|
UINT_MAX
|
0xffffffff
|
Max unsigned value
|
ULONG_MAX
|
0xffffffffUL
|
Max unsigned value
|
USHRT_MAX
|
0xffff
|
Max unsigned value
|
Max/min values for char
Defined Name
|
Value
|
Description
|
CHAR_BIT
|
8
|
Bits in a char
|
CHAR_MAX
|
SCHAR_MAX
|
Max char value
|
CHAR_MIN
|
0
|
Min char value
|
SCHAR_MAX
|
127
|
Max signed char value
|
SCHAR_MIN
|
-128
|
Min signed char value
|
UCHAR_MAX
|
0XFF
|
Max unsigned char val
|
There are also definitions for the max and min value for signed integers of 8, 16, 32, 64, and 128 bits.
BTW: For a 128 bit integer the values are
Max = 170,141,183,460,469,231,731,687,303,715,884,105,727
Min = -170,141,183,460,469,231,731,687,303,715,884,105,728
That’s +/- 1.7x1038 or 170 undicillion or
170 million million million million.
Sample Code
#include <climits>
using namespace std;
int main()
{
cout << "The following constants are defined in <climits>.\n";
cout << "\t INT_MAX = \t\t" << INT_MAX << endl;
cout << "\t INT_MIN = \t\t" << INT_MIN << endl;
cout << "\t LONG_MAX = \t\t" << LONG_MAX << endl;
cout << "\t LONG_MIN = \t\t" << LONG_MIN << endl;
cout << "\t SHRT_MAX = \t\t" << SHRT_MAX << endl;
cout << "\t SHRT_MIN = \t\t" << SHRT_MIN << endl;
cout << "\t UINT_MAX = \t\t" << UINT_MAX << endl;
cout << "\t ULONG_MAX = \t\t" << ULONG_MAX << endl;
cout << "\t USHRT_MAX = \t\t" << USHRT_MAX << endl;
cout << "\t CHAR_BIT = \t\t" << CHAR_BIT << endl;
cout << "\t CHAR_MAX = \t\t" << CHAR_MAX << endl;
cout << "\t CHAR_MIN = \t\t" << CHAR_MIN << endl;
cout << "\t SCHAR_MAX = \t\t" << SCHAR_MAX << endl;
cout << "\t SCHAR_MIN = \t\t" << SCHAR_MIN << endl;
cout << "\t UCHAR_MAX = \t\t" << UCHAR_MAX << endl;
}
<cstdlib>
Defines a number of useful constants used by functions declared in other header files and functions
declared in this header file.
Typedefs
typedef unsigned int size_t;
typedef unsigned short wchar_t; // Wide character
exit() function defines
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
Handy macros
#define __max(a,b) (((a) > (b)) ? (a) : (b))
#define __min(a,b) (((a) < (b)) ? (a) : (b))
Define NULL pointer value
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
Useful function prototypes
-
double atof(const char *s) Converts string to float
-
int atoi(const char *s) Converts string to int
-
long atol(const char *s) Converts string to long
-
int rand(void) Returns pseudo-random number in range 0 to
RAND_MAX which is usually 32767
-
void srand(unsigned int seed) Seeds random number generator with seed.
-
void *calloc(size_t nobj, size_t size) Returns pointer to space for an array of
nobj objects of size size or NULL if not enough memory. Space is initialized to zeros.
-
void *malloc(size_t size) Returns pointer to space for an object of size size
or NULL if not enough memory. Space is uninitialized.
-
void *realloc(void *p, size_t, size) Reallocates the size of memory allocated to
object pointed to by p
-
void free(void *p) Free the memory occupied by the object pointed to by p
-
void exit(int status) Causes an immediate normal program termination returning
to the caller (usually the operating system) an integer status which can signal the
exit status, ie. what has occured.
-
void system(const char *s) Used on UNIX systems. Causes string s to be passed to UNIX
to be executed by the shell.
-
int abs(int n) Returns absolute value of its integer argument.
(Don't know why K&R included this one here and not in math.h.)
Sample Code
#include <climits>
#include <time.h>
using namespace std;
int main()
{
cout << "The following functions are found in .\n";
cout << "\t Function: double atof(const char *s)\n";
cout << "\t\t Converting string \"3.14159\" to double. result = " << atof("3.14159") << endl;
cout << "\t Function: int atoi(const char *s)\n";
cout << "\t\t Converting string \"12345\" to int. result = " << atoi("12345") << endl;
cout << "\t Function: int atol(const char *s)\n";
cout << "\t\t Converting string \"123456789\" to long. result = " << atol("123456789") << endl;
cout << "\t Seeding the random number generator with void srand(unsigned int seed)\n";
time_t theTime; // Long for getting time in seconds since midnight, Jan 1, 1970
time(&theTime); // Get the time
srand((unsigned int)theTime); // Use it to seed the random number generator
cout << "\t\t Generating 5 random numbers with int rand(void)\n\t\t\t";
for(int i=0; i<5; i++)
{
cout << rand() << " ";
}
cout << endl;
cout << "\t Function: int abs(int n)\n";
cout << "\t\t Getting the absolute value of \"-12345\". result = " << abs(-12345) << endl;
}
<ctime>
Defines a number of useful constants and functions for handling time (timing and calendars).
Typedefs
typedef long time_t;
typedef long clock_t;
typedef unsigned int size_t; // if not defined already
#defines
#define CLOCKS_PER_SEC 1000; // ticks per second
Handy structures
struct _timeb
{
int dstflag; // non-zero if daylight ST in effect
long millitm; // milliseconds
long time; // time in seconds since midnight 1/1/70
long timezone; // diff in minutes moving west from UTC
};
#ifndef _TM_DEFINED
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
#define _TM_DEFINED
#endif
Useful function prototypes
-
char *asctime(const struct tm *); Convert a tm struct to a char string.
-
char (ctime(const time_t *); Convert a time value in seconds since January 1, 1970,
midnight UTC to a string
-
clock_t clock(void); Returns the number of clock ticks elapsed since the calling
process started running.
-
double difftime(time_t, time_t); Returns time difference between two times in
seconds as a double precision floating point number
-
struct tm *gmtime(const time_t *); Converts a time in seconds since January 1, 1970,
midnight UTC to a tm structure.
-
struct tm *localtime(const time_t *); Converts a time in seconds since January 1, 1970,
midnight UTC to a tm structure adjusting it to the local time zone.
-
time_t time(time_t *); Returns the current system time. Stores value in the time_t
variable pointed to by the argument if it is non-null. Easiest call is just tVal = time(NULL);
This next one has been deprecated but it is a handy time function also and can still be used.
You must #include <sys/types.h> for _ftime() and #include <sys/timeb.h> for the
definition of _timeb struct.
void _ftime(_timeb *); Returns current time with milliseconds in a _timeb structure.
Sample Code
#include <time.h>
#include <sys/types.h> // for _ftime()
#include <sys/timeb.h> // for _timeb struct
using namespace std;
int main()
{
cout << "The following functions are found in <ctime>.\n";
time_t t, t2;
struct tm tStruct;
cout << "\t Function: time_t time(time_t *);\n";
cout << "\t\tCall to time() returned: " << time(&t) << endl;
cout << "\t Function: char *ctime(const time_t *);\n";
cout << "\t\tCall to ctime() returned: " << ctime(&t) << endl;
cout << "\t Function: clock_t clock(void);\n";
cout << "\t\tCall to clock() returned: " << clock() << endl;
cout << "\t Function: double difftime(time_t, time_t);\n";
cout << "\t\tCalling time(&t2)" << endl;
time(&t2);
cout << "\t\tCall to difftime(t1, t2) returned: " << difftime(t, t2) << endl;
tStruct = *(localtime(&t));
cout << "\t Function: struct tm *localtime(const time_t *);\n";
cout << "\t\tCall to localtime() returned structure with values:\n";
cout << "\t\t\t tm.tm_hour = " << tStruct.tm_hour << endl;
cout << "\t\t\t tm.tm_min = " << tStruct.tm_min << endl;
cout << "\t\t\t tm.tm_sec = " << tStruct.tm_sec << endl;
cout << "\t\t\t tm.tm_mday (day of month) = " << tStruct.tm_mday << endl;
cout << "\t\t\t tm.tm_mon (month-Jan=0) = " << tStruct.tm_mon << endl;
cout << "\t\t\t tm.tm_year (years since 1900) = " << tStruct.tm_year << endl;
cout << "\t\t\t tm.tm_wday (days since Sunday) = " << tStruct.tm_wday << endl;
cout << "\t\t\t tm.tm_yday (days since Jan. 1) = " << tStruct.tm_yday << endl;
cout << "\t\t\t tm.tm_isdst (DST flag) = " << tStruct.tm_isdst << endl;
cout << "\t Function: char *asctime(const struct tm *);\n";
cout << "\t\tCall to asctime() returned: " << asctime(&tStruct) << endl;
cout << "\t Function: struct tm *gmtime(const time_t *);\n";
tStruct = *(gmtime(&t));
cout << "\t\tCall to gmtime() returned structure with values:\n";
cout << "\t\t\t tm.tm_hour = " << tStruct.tm_hour << endl;
cout << "\t\t\t tm.tm_min = " << tStruct.tm_min << endl;
cout << "\t\t\t tm.tm_sec = " << tStruct.tm_sec << endl;
cout << "\t\t\t tm.tm_mday (day of month) = " << tStruct.tm_mday << endl;
cout << "\t\t\t tm.tm_mon (month-Jan=0) = " << tStruct.tm_mon << endl;
cout << "\t\t\t tm.tm_year (years since 1900) = " << tStruct.tm_year << endl;
cout << "\t\t\t tm.tm_wday (days since Sunday) = " << tStruct.tm_wday << endl;
cout << "\t\t\t tm.tm_yday (days since Jan. 1) = " << tStruct.tm_yday << endl;
cout << "\t\t\t tm.tm_isdst (DST flag) = " << tStruct.tm_isdst << endl;
cout << "\nAnother handy time function if you include <sys/types.h> and <sys/timeb.h>\n";
_timeb ts2;
cout << "\t Function: void _ftime(_timeb *);\n";
_ftime(&ts2);
cout << "\t\tCall to _ftime() returned structure with values:\n";
cout << "\t\t\t tm.time = " << ts2.time << endl;
cout << "\t\t\t tm.millitm = " << ts2.millitm << endl;
cout << "\t\t\t tm.timezone = " << ts2.timezone << endl;
cout << "\t\t\t tm.dstflag = " << ts2.dstflag << endl;
}
…And the others in this group …
<csetjmp>
This defines some structures and functions used by the operating system to save and restore
the system function calls within programs.
<csignal>
This defines a number of operating system level procedures for the microprocessor.
<cstdarg>
This defines a number of operating system level procedures for accessing and handling arguments
to functions.
<cstddef>
This defines a number of definitions for common constants, types and variables used in C++ applications
such as size_t. But, it only defines them if they have not already been defined elsewhere, which in
most cases they already have been.
<exception>
This defines a number of operating system level exception handlers for when your program crashes.
Usually you will not need this. See <stdexcept> below.
Diagnostic Tools
This group includes tools for reporting exception conditions, for documenting program assertions
using the assert macro, and support for error codes. The header files in this group are:
<cassert>, <cerrno>, and <stdexcept>.
<cassert>
This defines the assert macro which can be used in debugging.
// assert.cpp
#include <iostream>
#include <cassert>
#include <string>
using namespace std;
void check_string(char *str);
void main()
{
char str1[] = "non-NULL string";
char *str2 = NULL;
cout << "Checking first string\n";
check_string(str1);
cout << "Checking second string\n";
check_string(str2)
}
void check_string(char *str)
{
assert(str != NULL);
}
<cerrno>
This just includes the <errno.h> header which defines a number of error codes which can be returned
by the perror() function. This is used with file I/O.
<stdexcept>
This just includes the <stdexcept.h> header which in turn just includes the
<exception> header which defines a number of operating system level exception
handlers for when a program crashes.
General C/C++ utilities
This group includes a number of utilities used by other parts of the standard library. The header files
include <ctime>, <functional>, <memory>, and <utility>.
<ctime>
This one is also included in the C++ language support and has already been covered.
<functional>
This provides some support for the Standard Template Library. See the STL link for more details.
<memory>
This header defines the new and delete operators. Fortunately you don’t have to bother
with #including this one as it is done automatically.
<utility>
This provides some support for the Standard Template Library. See the STL link for more details.
Strings
This group includes support for traditional Kernigan and Ritchie strings in char arrays,
support for the string class, and
support for the char and w_char (2 byte character type used in UNICODE applications).
The header files include <cctype>, <cstdlib>, <cstring>, <cwchar>, <cwctype>,
and <string>.
<cctype>
Includes functions to test characters. The arguments must be characters or integers whose value is an
unsigned char (ASCII 0-128). Each function returns non-zero (true) if the character meets the
function's implied condition. This header includes, but is not limited to...
isalnum(c) alphanumeric character (letter or digit)
isalpha(c) letter
iscntrl(c) control character (ASCII 0-31)
isdigit(c) decimal digit
islower(c) lower case letter
ispunct(c) printing character except for space or letter or digit, ie. is punctuation
isspace(c) space, formfeed, newline, CR, tab, vertical tab, ie. white space
isupper(c) upper case letter
isxdigit(c) hexadecimal digit
This header also includes two handy functions:
int tolower(int c) converts c to lower case if a letter
int toupper(int c) converts c to upper case if a letter
If c does not fit the required criteria (upper and lower case respectively, then c is returned unchanged.
Sample Code
#include <ctype>
using namespace std;
int main()
{
cout << "The following functions are found in <cctype>.\n";
cout << "\t Function: isalnum('A') returned " << isalnum('A') << endl;
cout << "\t Function: isalnum('-') returned " << isalnum('-') << endl;
cout << "\t Function: isalpha('Z') returned " << isalpha('Z') << endl;
cout << "\t Function: isalpha('1') returned " << isalpha('1') << endl;
cout << "\t Function: iscntrl('\\0') returned " << iscntrl('\0') << endl;
cout << "\t Function: iscntrl('A') returned " << iscntrl('A') << endl;
cout << "\t Function: isdigit('1') returned " << isdigit('1') << endl;
cout << "\t Function: isdigit('A') returned " << isdigit('A') << endl;
cout << "\t Function: islower('a') returned " << islower('a') << endl;
cout << "\t Function: islower('A') returned " << islower('A') << endl;
cout << "\t Function: isupper('a') returned " << isupper('a') << endl;
cout << "\t Function: isupper('A') returned " << isupper('A') << endl;
cout << "\t Function: ispunct('.') returned " << ispunct('.') << endl;
cout << "\t Function: ispunct('A') returned " << ispunct('A') << endl;
cout << "\t Function: isspace(' ') returned " << isspace(' ') << endl;
cout << "\t Function: isspace('A') returned " << isspace('A') << endl;
cout << "\t Function: isxdigit('A') returned " << isxdigit('A') << endl;
cout << "\t Function: isxdigit('G') returned " << isxdigit('G') << endl;
cout << "\t Function: tolower('A') returned " << tolower('A') << endl;
cout << "\t Function: toupper('z') returned " << toupper('z') << endl;
}
<string.h> and <cstring>
Provides many "string" handling functions for strings implemented as character arrays.
Arguments are character arrays, or occasionally integers designating a length and/or location.
Functions in the header includes but is not limited to...
char *strcpy(s,ct) Copy string ct to string s, including the '\0'; return s
char *strncpy(s,ct,n) Copy at most n charadcters of ct to s; return s
char *strcat(s,ct) Concatenate string ct to end of string s; return s.
char *strncat(s,ct,n) Concatenate at most n characters of ct to end of s;
return s
int strcmp(cs,ct) Compare string cs to string ct; return <0 if cs<ct,
0 if cs==ct, or >0 if cs>ct
int strncmp(s,ct,n) Compare at most n characters of string cs to string ct;
return <0 if cs<ct, 0 if cs==ct, or >0 if cs>ct
char *strchr(cs,c) Return pointer to first occurance of c in cs or NULL
if not present.
char *strrchr(cs,c) Return pointer to last occurance of c in cs or NULL
if not present.
char *strstr(cs,ct) Return pointer to first occurance of string ct in cs, or
NULL if not present.
size_t strlen(cs) Returns length of cs (treat as integer return value).
char *strtok(s,ct) Returns a token split from s using delimiters in ct.
This is a parser. See the code example below.
Sample Code
#include <cstring>
using namespace std;
int main()
{
cout << "The following functions are found in <cstring> to handle K&R style\n";
cout << " null terminated strings in character arrays.\n";
cout << "char str[64]; has been defined.\n\n";
char str[64];
strcpy(str, "This is a string.");
cout << "\t Function: strcpy(str, \"This is a string.\") set str = " << str << endl;
strncpy(str, "So is this ", 11);
cout << "\t Function: strncpy(str, \"So is this \", 11) set str = " << str << endl;
cout << "\t\tNote that the strncpy function does not add a null terminator.\n";
cout << "\t\t unless the new length of the string exceeds the old length.\n";
cout << "\t Resetting str to \"This is a string.\"\n";
strcpy(str, "This is a string.");
strcat(str, " With a little extra. ");
cout << "\t Function: strcat(str, \" With a little extra. \") set str = \n\t\t" << str << endl;
strncat(str, " And a bit more ", 11);
cout << "\t Function: strncat(str, \" And a bit more\", 11) set str = \n\t\t" << str << endl << endl;
cout << "\t Resetting str to \"This is a string.\"\n";
strcpy(str, "This is a string.");
cout << "\t Function: strcmp(str, \"This is!\") returned " << strcmp(str, "This is!") << endl;
cout << "\t Function: strcmp(str, \"This is a string.\") returned " << strcmp(str, "This is a string.") << endl;
cout << "\t Function: strcmp(str, \"This is a string and then some.\") returned " << strcmp(str, "This is a string and then some.") << endl;
cout << "\t Function: strncmp(str, \"This is a mess.\", 10) returned " <<
strncmp(str, "This is a mess.", 10) << endl << endl;
cout << "\t Function: strchr(str, 'i') returned " << strchr(str, 'i') << endl;
cout << "\t Function: strrchr(str, 'i') returned " << strrchr(str, 'i') << endl;
cout << "\t Function: strstr(str, \"is a\") returned " << strstr(str, "is a") << endl << endl;
cout << "\t Function: strlen(str) returned " << strlen(str) << endl << endl;
cout << "\t Function: strtok(str, \" .,;:!?\")\n";
cout << "\t\t 1st call: strtok(str, \" .,;:!?\") returned " << strtok(str, " .,;:!?") << endl;
cout << "\t\t 2nd call: strtok(NULL, \" .,;:!?\") returned " << strtok(NULL, " .,;:!?") << endl;
cout << "\t\t 3rd call: strtok(NULL, \" .,;:!?\") returned " << strtok(NULL, " .,;:!?") << endl;
cout << "\t\t 4th call: strtok(NULL, \" .,;:!?\") returned " << strtok(NULL, " .,;:!?") << endl;
}
<cstdlib>
Already covered this under language support.
<cwchar>
Provides support for wide char (Unicode) 2 byte characters.
Cultural Formatting Support
Provides numeric, monetary, day/time formatting, and special language support such as for
Chinese, Japanese, Korean, Russian, and Arabic. Most of this you don’t have to be concerned
about unless you are programming in one of those languages using their alphabet.
The header files include <clocale> and <locale>.
The Standard Template Library
Provides many containers, algorithms, and iterators.
The header files include <algorithms>, <deque>, <list>, <map>, <queue>, <set>,
<stack>, <vector>, and <iterator>. See the STL link for details.
Advanced Numerical Computation
These headers provide all the math operations as well as support for complex number types,
numeric arrays, and many other numeric algorithms. The header files include
<cfloat>, <cmath>, <complex>, <cstdlib>, <numeric>, and <valarray>.
<cfloat>
Already covered this under language support.
<cmath> and <math.h>
These headers provide a variety of math functions. The functions include but are not limited to:
sin(x) Returns sine of x
cos(x) Returns cosine of x
tan(x) Returns tangent of x
asin(x) Returns arcsine of x in range [-π/2,π/2],
x
[-1,1]
acos(x) Returns arcosine of x in range [0,π],
x
[-1,1]
atan(x) Returns arctangent of x in range [-π/2,π/2]
sinh(x) Returns hyperbolic sine of x
cosh(x) Returns hyperbolic cosine of x
tanh(x) Returns hyperbolic tangent of x
exp(x) Returns function, ex
log(x) Returns natural logarithm, ln(x), x>0
log10(x) Returns base 10 logarithm, log10(x), x>0
pow(x,y) Returns xy, a domain error occurs if x=0 and y≤0,or
if x<0 and y is not an integer.
sqrt(x) Returns √x, x≥0
ceil(x) Returns smallest integer not less than x
floor(x) Returns largest integer not greater than x
Also includes functions for handling float and long double data types.
Sample Code
#include <cmath>
using namespace std;
int main()
{
cout << "The following functions are found in <cmath>. All angles must be given\n";
cout << " in radians, not degrees. 1 radian = 57.2957795 degrees so o convert\n";
cout << " degrees to radians divide by 57.2957795. In the following examples\n";
cout << " ang1 = 45 degrees, ang2 = 135 degrees, ang3 = 225 degrees, and \n";
cout << " ang4 = 315 degrees. All have been converted to radians.\n\n";
double ang1 = (45.0 / 57.2957795); // Convert all angles to radians
double ang2 = (135.0 / 57.2957795);
double ang3 = (225.0 / 57.2957795);
double ang4 = (315.0 / 57.2957795);
cout << "ang1 = " << ang1 << " ang2 = " << ang2 << " ang3 = " << ang3
<< " ang4 = " << ang4 << endl << endl;
cout << "\t Function: sin(ang1) returned " << sin(ang1) << endl;
cout << "\t Function: sin(ang2) returned " << sin(ang2) << endl;
cout << "\t Function: sin(ang3) returned " << sin(ang3) << endl;
cout << "\t Function: sin(ang4) returned " << sin(ang4) << endl;
cout << "\t Function: cos(ang1) returned " << cos(ang1) << endl;
cout << "\t Function: cos(ang2) returned " << cos(ang2) << endl;
cout << "\t Function: cos(ang3) returned " << cos(ang3) << endl;
cout << "\t Function: cos(ang4) returned " << cos(ang4) << endl;
cout << "\t Function: tan(ang1) returned " << tan(ang1) << endl;
cout << "\t Function: tan(ang2) returned " << tan(ang2) << endl;
cout << "\t Function: tan(ang3) returned " << tan(ang3) << endl;
cout << "\t Function: tan(ang4) returned " << tan(ang4) << endl;
cout << "\t Function: asin(sin(ang1)) returned " << asin(sin(ang1)) << endl;
cout << "\t Function: asin(sin(ang2)) returned " << asin(sin(ang2)) << endl;
cout << "\t Function: asin(sin(ang3)) returned " << asin(sin(ang3)) << endl;
cout << "\t Function: asin(sin(ang4)) returned " << asin(sin(ang4)) << endl;
cout << "\t Function: acos(cos(ang1)) returned " << acos(cos(ang1)) << endl;
cout << "\t Function: acos(cos(ang2)) returned " << acos(cos(ang2)) << endl;
cout << "\t Function: acos(cos(ang3)) returned " << acos(cos(ang3)) << endl;
cout << "\t Function: acos(cos(ang4)) returned " << acos(cos(ang4)) << endl;
cout << "\t Function: atan(tan(ang1)) returned " << atan(tan(ang1)) << endl;
cout << "\t Function: atan(tan(ang2)) returned " << atan(tan(ang2)) << endl;
cout << "\t Function: atan(tan(ang3)) returned " << atan(tan(ang3)) << endl;
cout << "\t Function: atan(tan(ang4)) returned " << atan(tan(ang4)) << endl;
cout << "\t Function: sinh(ang1) returned " << sinh(ang1) << endl;
cout << "\t Function: sinh(ang2) returned " << sinh(ang2) << endl;
cout << "\t Function: sinh(ang3) returned " << sinh(ang3) << endl;
cout << "\t Function: sinh(ang4) returned " << sinh(ang4) << endl;
cout << "\t Function: cosh(ang1) returned " << cosh(ang1) << endl;
cout << "\t Function: cosh(ang2) returned " << cosh(ang2) << endl;
cout << "\t Function: cosh(ang3) returned " << cosh(ang3) << endl;
cout << "\t Function: cosh(ang4) returned " << cosh(ang4) << endl;
cout << "\t Function: tanh(ang1) returned " << tanh(ang1) << endl;
cout << "\t Function: tanh(ang2) returned " << tanh(ang2) << endl;
cout << "\t Function: tanh(ang3) returned " << tanh(ang3) << endl;
cout << "\t Function: tanh(ang4) returned " << tanh(ang4) << endl;
cout << "\t Function: exp(2.0) returned " << exp(2.0) << endl;
cout << "\t Function: exp(4.0) returned " << exp(4.0) << endl;
cout << "\t Function: exp(8.0) returned " << exp(8.0) << endl;
cout << "\t Function: log(10.0) returned " << log(10.0) << endl;
cout << "\t Function: log(100.0) returned " << log(100.0) << endl;
cout << "\t Function: log(1000.0) returned " << log(1000.0) << endl;
cout << "\t Function: log10(10.0) returned " << log10(10.0) << endl;
cout << "\t Function: log10(100.0) returned " << log10(100.0) << endl;
cout << "\t Function: log10(1000.0) returned " << log10(1000.0) << endl;
cout << "\t Function: pow(2.0,2.0) returned " << pow(2.0,2.0) << endl;
cout << "\t Function: pow(2.0,8.0) returned " << pow(2.0,8.0) << endl;
cout << "\t Function: pow(2.0,16.0) returned " << pow(2.0,16.0) << endl;
cout << "\t Function: sqrt(16.0) returned " << sqrt(16.0) << endl;
cout << "\t Function: sqrt(17.0) returned " << sqrt(17.0) << endl;
cout << "\t Function: sqrt(18.0) returned " << sqrt(18.0) << endl;
cout << "\t Function: ceil(16.4) returned " << ceil(16.4) << endl;
cout << "\t Function: ceil(16.6) returned " << ceil(16.6) << endl;
cout << "\t Function: floor(16.4) returned " << floor(16.4) << endl;
cout << "\t Function: floor(16.6) returned " << floor(16.6) << endl;
}
<complex>
Includes functions for handling complex numbers of vectors (giving a magnitude and direction)
not the C++ vectors.
<cstdlib>
Already covered this under language support.
<numeric>
This is part of the Standard Template Library. See the STL link for details.
<valarray>
This is part of the Standard Template Library. See the STL link for details.
Input/Output
This includes support for all the input/output operations. The header files include
<cstdio>, <cstdlib>, <cwchar>, <fstream>, <iomanip>, <ios>, <iosfwd>,
<iostream>, <ostream>, <stringstream>, and <streambuf>. See the
link on File I/O for details.
A note on strstream and sstream: These are old headers that have now been deprecated in the
latest version of Standard C++. Use <stringstream> instead.