alleora

computer science & systems development

Arkiv för kategori ‘C++’

C++ standard headers

Skrivet av ratache den augusti 12, 2011

C++ Standard Library header file

reference from C++ How To Program, Deitel

Explanation

<iostream>

Contains function prototypes for the C++ standard input and standard output functions, introduced in Chapter 2, and is covered in more detail in Chapter 15, Stream Input/Output. This header file replaces header file <iostream.h>.

<iomanip>

Contains function prototypes for stream manipulators that format streams of data. This header file is first used in Section 4.9 and is discussed in more detail in Chapter 15, Stream Input/Output. This header file replaces header file <iomanip.h>.

<cmath>

Contains function prototypes for math library functions (discussed in Section 6.3). This header file replaces header file <math.h>.

<cstdlib>

Contains function prototypes for conversions of numbers to text, text to numbers, memory allocation, random numbers and various other utility functions. Portions of the header file are covered in Section 6.7; Chapter 11, Operator Overloading; String and Array Objects; Chapter 16, Exception Handling; Chapter 19, Web Programming; Chapter 22, Bits, Characters, C-Strings and structs; and Appendix E, C Legacy Code Topics. This header file replaces header file <stdlib.h>.

<ctime>

Contains function prototypes and types for manipulating the time and date. This header file replaces header file <time.h>. This header file is used in Section 6.7.

<vector>,
<list>,
<deque>,
<queue>,
<stack>,
<map>,
<set>,
<bitset>

These header files contain classes that implement the C++ Standard Library containers. Containers store data during a program’s execution. The <vector> header is first introduced in Chapter 7, Arrays and Vectors. We discuss all these header files in Chapter 23, Standard Template Library (STL).

<cctype>

Contains function prototypes for functions that test characters for certain properties (such as whether the character is a digit or a punctuation), and function prototypes for functions that can be used to convert lowercase letters to uppercase letters and vice versa. This header file replaces header file <ctype.h>. These topics are discussed in Chapter 8, Pointers and Pointer-Based Strings, and Chapter 22, Bits, Characters, C-Strings and structs.

<cstring>

Contains function prototypes for C-style string-processing functions. This header file replaces header file <string.h>. This header file is used in Chapter 11, Operator Overloading; String and Array Objects.

<typeinfo>

Contains classes for runtime type identification (determining data types at execution time). This header file is discussed in Section 13.8.

<exception>,
<stdexcept>

These header files contain classes that are used for exception handling (discussed in Chapter 16).

<memory>

Contains classes and functions used by the C++ Standard Library to allocate memory to the C++ Standard Library containers. This header is used in Chapter 16, Exception Handling.

<fstream>

Contains function prototypes for functions that perform input from files on disk and output to files on disk (discussed in Chapter 17, File Processing). This header file replaces header file <fstream.h>.

<string>

Contains the definition of class string from the C++ Standard Library (discussed in Chapter 18).

<sstream>

Contains function prototypes for functions that perform input from strings in memory and output to strings in memory (discussed in Chapter 18, Class string and String Stream Processing).

<functional>

Contains classes and functions used by C++ Standard Library algorithms. This header file is used in Chapter 23.

<iterator>

Contains classes for accessing data in the C++ Standard Library containers. This header file is used in Chapter 23, Standard Template Library (STL).

<algorithm>

Contains functions for manipulating data in C++ Standard Library containers. This header file is used in Chapter 23.

<cassert>

Contains macros for adding diagnostics that aid program debugging. This replaces header file <assert.h> from pre-standard C++. This header file is used in Appendix F, Preprocessor.

<cfloat>

Contains the floating-point size limits of the system. This header file replaces header file <float.h>.

<climits>

Contains the integral size limits of the system. This header file replaces header file <limits.h>.

<cstdio>

Contains function prototypes for the C-style standard input/output library functions and information used by them. This header file replaces header file <stdio.h>.

<locale>

Contains classes and functions normally used by stream processing to process data in the natural form for different languages (e.g., monetary formats, sorting strings, character presentation, etc.).

<limits>

Contains classes for defining the numerical data type limits on each computer platform.

<utility>

Contains classes and functions that are used by many C++ Standard Library header files.

Sparad i C++ | Taggad: , , , , , , , , | Lämna en kommentar »

Nyckelord och identifierare i C++

Skrivet av ratache den juni 27, 2011

C++ (standard)Keywords 
Keywords common(standard) to the C and C++ programming languages

auto   const     double  float  int       short   struct   unsigned
break  continue  else    for    long      signed  switch   void
case   default   enum    goto   register  sizeof  typedef  volatile
char   do        extern  if     return    static  union    while


C++ only keywords

asm         dynamic_cast  namespace  reinterpret_cast  try
bool        explicit      new        static_cast       typeid
catch       false         operator   template          typename
class       friend        private    this              using
const_cast  inline        public     throw             virtual
delete      mutable       protected  true              wchar_t


C++ keywords when ASCII is not used 
and      bitand   compl   not_eq   or_eq   xor_eq
and_eq   bitor    not     or       xor

Predefined identifiers who are not reseverd

cin   endl     INT_MIN   iomanip    main      npos  std
cout  include  INT_MAX   iostream   MAX_RAND  NULL  string

Sparad i C++, Programmering | Taggad: , , , , , , , , , | Lämna en kommentar »

Att visa hur overflow fungerar (?)

Skrivet av ratache den juni 25, 2011

Hur fungerar overflow? Vad händer om man ger en variabel sitt maximala värde plus 1? Följande kod visar vad som händer i C++. Istället för en crash ser vi att variablen tilldelas sitt motsatta värde i botten på dess värdeskala.

 

Att visa hur overflow av variabler fungerar:				

// datatype_IO_030
// Per Johansson
//------------------------------------------------------------------------------
#include<iostream> // cout
#include<limits> // INT_MAX
using namespace std;
//-----------------------------------------------------------------------------
int main()
{
	locale swedish("swedish");
    locale::global(swedish);
	//Programmet börjar här.
	int Olle = INT_MAX; // Initera variabeln Olle till maxvärdet
	unsigned int Lisa = Olle ; // Initiera Lisa = Olle
	
	cout << " Olle har " << Olle <<" kr och Lisa har " <<Lisa << " kr." << endl;
	cout << " Addera 1 kr till varje konto" << endl;
	
	Olle = Olle + 1;
	Lisa = Lisa + 1;
	
	cout << " Olle har nu " << Olle <<" kr och Lisa har " << Lisa
	<< " kr." << endl << endl;
	
	Olle = 0;
	Lisa = 0;
	
	cout << " Olle har " << Olle <<" kr och Lisa har " << Lisa << " kr." << endl;
	cout << " Tag 1 kr av Olle resp Lisa" << endl;
	
	Olle = Olle - 1;
	Lisa = Lisa - 1;
	
	cout << " Olle har nu " << Olle <<" kr och Lisa har " << Lisa <<" kr."<< endl;
	return 0;
}

Sparad i C++, Programmering | Taggad: , , , , , , , , , , , , , , , , , , , | Lämna en kommentar »

C++ sommarkurs delsteg 1_done

Skrivet av ratache den juni 23, 2011

// firstfile.cpp
// Ett första försök att göra ett C++ - program
// Per Johansson 2011-06-23
//-----------------------------------------------------------------------------
#include <iostream>   // cout, cin
using namespace std;
//-----------------------------------------------------------------------------
int main()
{
	int kr;

	kr = 250;
	cout << "Per Johansson fick ";
	cout << kr;
	cout << " kronor av Försvarsmakten idag."<< endl;

}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

ANTECKNINGAR!

I C++ i jämnförelse med Python så deklarer man variabeln och dess typ först i koden innan man tilldelar ett värde.

int kr; <-- deklarerar datatypen och variabelnamnet samt allokerar plats för ett 32-bitars tal i RAM-minnet.
int = 250;

I Python blir detta:

kr = 250 (datatypen väljs automatiskt)

Variabelförteckning C++

Numerära tal:
Heltal 			Platsbehov (bitar) 		Talområde
int 			32 					-2 147 483 648 - 2 147 483 647
unsigned int 	        32 					0 – 4 294 967 295
long 			32 					-2 147 483 648 - 2 147 483 647
unsigned long 	32 						0 – 4 294 967 295
short int 		16 					-32 768 – 32 767
enum 			8 					-128 - 127

Flyttal
float 			32 					3,4 10-38 - 3,4 10+38 (pos. & neg.) 7 siff. precision
double 			64 					1,7 10-308 - 1,7 10+308 (pos. & neg.) 15 –“-
long double 	        80 					3,4 10-4932 – 3,4 10 +4932 (pos. & neg.) 18 –“-

Tecken
char 			8 					-128 – 127
unsigned char 	        8 					0 – 255

Datatypernas platsbehov (längd) är beroende av plattform. Ovanstående siffror gäller för 32 bitars Windows operativsystem. 
Dessutom gäller att: int = signed int och long = long int = signed long int. Tecken (char) representeras internt av ett heltal.

Typ 			Exempel 				Kommentar
Int 			int i; 					variabeln i lagrar ett heltal
			int j, J; 				skiljer på gemener och versaler
			int j=3; 				tilldelar värde i samband med deklarationen
			int i, j, k; 			        deklarera flera variabler samtidigt
			int alder; 				det är INTE tillåtet att använd å, ä, ö, Å, Ä, Ö
			int nr_of_colors:	 	        _ tillåtet i variabelnamn

float 			float x; 				variabeln x lagrar ett decimaltal (flyttal)
float 			y = 5.34 				OBS! Punkt (“.”) är decimalavskiljare

char 			char ch 				variabeln ch lagrar ett tecken
			char tecken=’A’; 		        använd ’ ’ för att visa att A är ett tecken

Svenska i C++
//å - '\x86'
//ä - '\x84'
//ö - '\x94'
//Å - '\x8F'
//Ä - '\x8E'
//Ö - '\x99'

int main()
{
    locale swedish("swedish");
    locale::global(swedish);

    cout << "åÅäÄöÖ" << endl << endl;

    return 0;
}

Sparad i C++, Programmering | Taggad: , , , , , , , , , , , , , | 1 Kommentar »

 
Följ

Få meddelanden om nya inlägg via e-post.