I have never used C ++ for development. I have always used oo languages such as C # and Java, or scripting languages such as JS and shell. So I occasionally want to write some C ++CodeIt is quite difficult.
1. How to access a two-dimensional array
1 // The arry that we want to access
2 Char Country [ 255 ] [ 60 ] = { " China " , " England " , " USA " };
3
4 // Method 1
5 Int I = 0 ;
6 While ( * Country [I] ! = Null ){
7 STD: cout < Country [I] < STD: Endl;
8 I ++ ;
9 }
10
11 // Method 2
12 Int I = 0
13 For (I = 0 ; I < 255 ; I ++ )
14 {
15 Char * Country = Parr -> Country [I];
16 Cout < Country < Endl;
17 }
2. Declaration and use of function pointers
1 // FP is defined as a function pointer type in the creverseip type.
2 Typedef bool (creverseip :: * FP )( Const Char * Inputip, ipinfo * Ipinfo );
3
4 // Declare a FP type variable in creverseip
5 Class Creverseip
6 {
7 /* Some other stuff */
8 Public :
9 FP fparse;
10 /* Some other stuff */
11 }
12
13 // Assignment
14 Reviptool -> Fparse = & Creverseip: getiplocation_old;
15
16 // Use
17 Retvalue = ( This -> * Fparse) (strline. c_str (), ipinfo );
3. file read/write (read and write in the following sample code)
1 Bool creverseip: parseipfiletofile ( String Srcfile, String Desfile)
2 {
3 Ifstream streamin (srcfile. c_str ());
4 If (Streamin. Fail ())
5 {
6 Cout < Srcfile < " Cocould not be opened. " < Endl;
7 Return False;
8 }
9 Fstream desstream;
10 Desstream. Open (desfile. c_str (), IOS :: In );
11 If (Desstream)
12 {
13 Cout < Desfile < " Already exists, overwrite? (Y/N) " < Endl;
14 Desstream. Close ();
15 Char C;
16 // C = getchar ();
17 CIN > C;
18 Switch (C)
19 {
20 Case ' Y ' :
21 Break ;
22 Case ' N ' :
23 Return False;
24 Default :
25 Break ;
26 }
27 }
28
29 Ofstream streamout (desfile. c_str (), IOS :: Out );
30 Streamout < " IP \ tcountry \ tprovince \ tcity " < Endl;
31 String Strline;
32 Ipinfo * Ipinfo = New Ipinfo ();
33 Bool retvalue = False;
34 While (Getline (streamin, strline ))
35 {
36 Retvalue = ( This -> * Fparse) (strline. c_str (), ipinfo );
37 If (Retvalue = True && Ipinfo ! = Null)
38 {
39 Streamout < Ipinfo -> IP < " \ T " < Ipinfo -> Country < " \ T " < Ipinfo -> Province < " \ T " < Ipinfo -> City < Endl;
40 }
41 Else
42 {
43 Continue ;
44 }
45 }
46 Streamin. Close ();
47 Streamout. Close ();
48 Delete ipinfo;
49 Return True;
50 }