A quick review of C + + primer chapters

Source: Internet
Author: User

Chapter III:

1,sozeof (int); int N_int=int_max; sizeof N_int; (optional for variable brackets)
2, #include <climits> contains some types of maximum values
3,count default display decimal, Cout<<dec (default) Count<4,bool, char, short, int, long, longlong, float, double, long double
5,COUNT.SETF (Ios_base::fixed,ios_base::floatfield)//Prevent large number switching to e notation, show 6 decimal places after decimal point (reserved 0)
6,c++ style TypeName (value)
7, variable initialization int wren (23); int wren=23;
8,c++ Const and # define differences:
(1) The const compile stage is used, define is in the preprocessing stage;
(2) const can specify type, do security check, and define not
(3) const can be defined in a specified area
(4) const used for more complex types, arrays, structs (large objects to allocate memory, or extern to declare or address them also to allocate memory)
(5) The const constant defines the base type when placed in the symbol table (heap or stack), while define is only expanded, does not allocate memory
9 Type of constant: l,l; U,u; Ul,ul;

10 type conversion The float operation is not promoted to Double,int integer is assigned to float when capturing decimals

Constant folding: #define和const, const defines a constant, you cannot try to change it by changing the value of the variable, but you can change it by referencing its address, that is, by the pointer. Constant folding says that in the compile phase, the variable is replaced with a value, while the constant is recorded in the symbol table, and define is not the same, in three cases, the const allocates memory.
Example: #include <iostream>
int main ()
{
using namespace Std;
const int I=20;
int *p= (int *) &i;//Change the value on I memory address
*p=10;

cout<< (int *) &i<< ":" <<i<<endl;//this I was already replaced by 20 in the compilation phase
cout<<p<< ":" <<*p<<endl;
return 0;
}
Results show 0012ff44:20
0012ff44:10


The fourth chapter:
1, the array can only be defined when initialized, int hand[500]={0}, after which only a single individual element can be assigned;
When initializing an array of {1}, only the first element is 1, and the other 0
A=int[10];sizeof a array of bytes 10*4; sizeof a[0] int type byte number 4

2, string array char cat[5]= "C+owby" or char cat[]= "..."; count<<cat prints the string, not the address;

3, String input: cin is delimited by a blank (space,\b,\n) and cannot read spaces;
Cin.getline (name,20), encounters \r,\n stop, discards newline characters, reads up to 19 char;
Cin.get (name,20). Get () or name=cin.get (); Cin.get (); Cin.clear ()
* Cin,get () or (Cin>>value). Get () handles a carriage return from a character stream by using the cin>> continuous read-in, noting that each time a character or number is read. \ n

4, read digital cin>>year;cin.get () to handle blank line, prevent blocking input

5, #include <string> string class must declare useing namespace STD before use in namespace Std;
Unlike string arrays, each other is assigned a value: STR1=STR2; Splicing: STR1+=STR2; read in: Getline (CIN,STR1) (This is a function, not a CIN class method)

6, contrast #include<cstring>
void strcat (char *t,char *s) {while (*t++=*s++);} string concatenation
strcpy (char *t,char *s);

String Length: String class Method Len=str1.size () (Non-' len=strlen '), array of Strings (CHARR1) (function in CString, No.)
Read-in method: String array cin.getline (name,20), String:getline (CIN,STR1);

7,struct can be assigned to each other, the member is separated by the definition, the value can be assigned as a whole, and the structure array

8, Union, enum Spectum{oen,two=0,three=8,four};spectum Myflag=one;

9, pointer: Must have pointed to the address before use! Cha c= ' a ',*p=&c; (int*) cast to two-byte address type, (double*) four bytes
10,new/delete and Malloc/free comparison:
New/delete is an operator, and Malloc/free is a library function, which is used to request dynamic memory and release dynamic memory.
For objects of non-intrinsic data types, Malloc/free cannot execute constructors and destructors, destined only for C
And New/delete can handle it.
11,new/delete regulation: Memory exhaustion returns 0 (null pointer) can be released, not the new open memory should not be released with delete; an address can be freed only once, no matter how many pointers point to it
12, pointers and array names: basically the same, fundamentally different: The array name is the address constant pointing to the first element and cannot be assigned!
A pointer or array name plus 1 refers to the next block of addresses, that is, the following element
13,cout Intelligent: Cout<<array;array is the array name, cout automatically discern whether it is a string array (try, char array can be, such as character array char array[]=[' A ', ' B ', ', ' C ') also can) or string or char*, if the contents of the array are printed, the other is the first element address of the print group.

1#include <iostream>2#include <cstring>3#include <string>4 5 using namespacestd;6 intMain ()7 {8     stringstr1="Hello";9cout<<str1;//Print String ContentsTen     Chararray[]="Hello Word"; One  Acout<<endl<<array<<endl;//Print string array contents -     intmm[6]={0,1,2,3,4,5}; -cout<<mm<<endl;//Print integer array address the  -     CharC='a', nn[]={'a','b','C','D','e','F'}; -cout<<"nn is:"<<nn<<endl;//Print character array contents, but not initialized after N[4], garbled -     Char*p1=New Char[Strlen (NN) +1]; +cout<<"strlen:"<<strlen (NN) <<endl;//strlen is only useful for string arrays!! How did 12 get here?  - strcpy (P1,NN); +cout<<p1<<endl;//as with the print NN, the subsequent uninitialized is still garbled Acout<<*p1<<endl;//first element content at      -     Charvv[ -]="Hello Word!!!"; -cout<<vv<<Endl; -cout<<"strlen:"<<strlen (VV) <<Endl; -     Char*p2=New Char[Strlen (VV) +1];//this time we can. - strcpy (P2,VV); incout<<p2<<endl;//Print out Hello word -     Delete[] p1; to     Delete[] p2; +     return 0; -  the}
View Code

  

A quick review of C + + primer chapters

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.