Memo: C + + Basics-Learning Summary of data types

Source: Internet
Author: User

several common types of data:

Type identifier

Description

Bytes

Range of values

Int

Integral type

4

–2,147,483,648 to 2,147,483,647

VC + + is a long int class

Short

Short-integer

2

–32,768 to 32,767

Long

Long integer type

4

–2,147,483,648 to 2,147,483,647

bool

Boolean type

1

False or True

Char

Character type

1

128 to 127

Float

Single-precision floating-point type

4

3.4E +/-38 (7-digit)

Double

Double-precision floating point

8

1.7E +/-308 (15-digit)

String

String

--

#include <string>

With the unsigned identifier before the numeric type, the type is unsigned data type, such as: unsigned int, unsigned short ...

You can use typeID to determine the type of data:

int a=ten;  if (typeID (a) ==typeid (int"isint type" << Endl;      

For detailed data types, see virtual C + + data type range in MSDN:
Http://msdn.microsoft.com/zh-cn/library/s3f49ktz.aspx

Array

The general form of the definition array is:

Type identifier array name [constant-expression] [constant expression] ...

int a[], b[5];  int a[n*// assuming that n is a constant variable float a[3][4], b[5][];       

To create a dynamic array with pointers:

 int N; DATE *PD; // Assuming that PD is a struct type N = 3; PD = new Date[n]; Pd[0]. Year = 20101].year = 20112].year = Span style= "color: #800080;" >2014 cout << pd[0].year << Pd[2].year << Endl; delete[] PD; // release memory in time        

Enumeration

The general form of declaring enum types is:

Enum Enum type name {Enum constant table column};

Enum//// character value if (d!=satday) {cout<<d<<Endl;}     

Structure

The general form of declaring a struct type is:

struct struct type name {member table column} [variable name table column];

struct book{  string title;  float//" title //12.23;        

Pointer

The pointer is the address that points to a variable. We refer to the address of a variable as a pointer to that variable. A good understanding of pointers, in C + + programming will be very good application. Learn about the relationship of good pointers when they point to classes or arrays.

The general form of defining pointer variables is:

Base type * pointer variable name;

Defining variables // defining pointers  

Defines the pointer with a * pointer character.

Take the address of a variable with the address operator &, in the operation * is the pointer-pointing character, note that at this time * and the definition of the pointer variable when the * meaning is different. Define pointer variables such as int *n; At this point * only the variable is a pointer-type variable, * is meaningless, just one tells us that the variable n is a pointer type. At the time of operation, such as *n=10;  or a=*n; At this point the * is a pointer, an operator, which means the data that points to N. The *n gets the data in memory.

& Fetch address operator.
* pointer operator (or indirect access operator).
Pointer member pointing character

For example:

int a=3;  int *PA; pa = &a;  /////= A pointer to a that is equivalent to a. cout << *pa << endl;       

Classes and Pointers:

The class name is actually an address point. This pointer address can be obtained by using the pointer & operator.
When you use the pointer to point to a class. You can * point to the multibyte pointer variable name, such as *p. To access the members of the class. Using the pointer to the member-pointing character

//////   Suppose X is a variable member of a class. Refer to this variable for pointers. That is, *pa->x and a.x are equivalent. 

Arrays and pointers

The array name is actually an address point. (Same as class name)
such as int n[10] n is the first memory pointing to a string of int type data
Array [] symbol, real sister is the variable address operator. [1] refers to this string of int data in memory of the 2nd address ([0] is the first).
The understanding of array n[1] is to use [1] to find the address, and then use the n[1] to locate the data value that the address points to. It can be understood that n[1] is the data of N in the address XXXX, and the address xxxx is obtained by [1] in the variable address.

Then look at the relationship between the pointer and the data.

pointer int *p, p is an address. *p is the data that the pointer points to.

Data int n[10], M is also an address. M[0] is the one that points to the first data.

can be seen. The pointer p is the same as the array variable m. It's all an address. *p and M[0]. are pointing to the data. [0] here equals the * operator. Similar to * (M0) assumes that M0 refers to the first data in a data string.

As a result, pointers have different meanings when they correspond to individual changes and array variables.

See Example

int n, m[10];

int *p;

P=n; Wrong. Because P is pointing to the address, N is pointing to the data. At this point, you will use the address character &. p=&n;

P=m; Right. Because the array m[10]. M is the first address in a string of data that points to an address. Equivalent to p=&m[0].

P=M[0]; Wrong. Here M[0] is pointing to the address. Equivalent to N, this is the use of the address character & P=&m[0].

p++; *p++; (*p) + +; Compare the differences between the three.

P++ refers to the move down the address.

*p++ refers to the data that is pointed to when the address is moved down. That is * (p++)

(*p) + + refers to the data value that p points to plus 1.

P is the point of the address. So p+1 is actually moving the address to the next address. equivalent to [0+1] in M.

P[0] is actually equivalent to m[0].

* (p++) and * (++p) effect is different. The former is the first fetch *p value, then the P plus 1. The latter is the first to make P plus 1, then take *p. If the initial value of P is a (that is, &a[0]), the output * (p++) gets a[0], and the output * (++p) Gets the value of a[1].

x=* (p++) First "*" operation, get a[i],x=a[i]; Again make p+1,p point to a[i+1].

x=* (++p) first make P self-add 1, then make * operation, get a[i+1]. X=A[I+1] P also point to a[i+1]

Examples of using data pointers:

Define a two-set of data:

    int m[2][12] ={        {31,28,31,30,31,30,31,31,30,31,30,31},//Data set 1 {31,29,31,30,31,30,31,31,30,31,30,31}//Data Set 2};int (*p) [12], *P1; p = &m[0]; P1 = &m[0][0]; (*p) [0] =1;//To m[0][0] Assignment equivalence: (*P) [0] = 1; * (p[0]) = 1; p1[0] =2;//Assigning attention to m[0][0] differs from *p[0]. [0] Here is actually the access to the turn character.cout << *p << ' / "<< *p[0] <<  / < < *P1 <<  ' / " << p1[0] << Endl; cout << P << Span style= "color: #800000;" > ' / << P1 <<  ' / << Endl                 

You want to get a dataset of m[1] and m[2] in a set of data in the array.
You can explicitly define the number dimension pointer int (*P) [12], and the value pointer to p p = &m [1]; or P = &m [2];
When taking a value or a value, it can be passed (*p) [0] = 125; X = (*p) [0];

The relationship between an array and an array pointer is understood only by heart. In order to use the string in C + + well.

Reference

A reference is an alias for a variable. Use the reference symbol & to represent the reference variable. (Note: & Here is different from pointer-to-pin &)

The general form of a definition reference is:
Base type & reference variable name;

IntAint &b = A; //10; // a=10 here B is a, they fit. // Look again at the pointer address &int< Span style= "color: #000000;" > X; int *p;x=12;p = &x; // note refer to &b with pointer to address & different B = *p; // &b=x; p = &b; // This is not to assign the value of B to P, but to assign the address of A to P now *p = A;     

when declaring a reference-type variable, you must initialize it at the same time, and you can no longer assign a value to it. A reference variable has a unique directivity and is specified to point to when it is initialized. After declaring that variable B is a reference to variable A, the reference type variable B is always associated with the variable A that it represents, and can no longer be used as a reference (alias) for other variables during the execution of the function where they are located.

The reference makes it easy to convert pointer-type variables into entity variables.

CTest is a class. CTest *pa;  // Declare a pointer to a class CTest &b=*pa;  //////  equivalent to a.x = 10 with *pa->x      

At this point B is the reference alias of *pa. Or, B is the entity alias of the class that the *PA points to. B is equivalent to a. Use B is equivalent to using a directly. The reference alias is used here, mainly for the convenience of the member notation of the class. Instead of the pointer's member, point to symbol. Make your code more intuitive and easier to write.

References are also commonly used for parameter passing of functions. Such as:

void Dofunc (int &b) {    b++//void Dofunc (int *b) {*b++;}      

Memo: C + + Basics-Learning Summary of data types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.