Summary of C ++ primer Chinese edition (fourth edition) (2)

Source: Internet
Author: User

The previous article mainly collected the content of the previous five chapters, http://blog.csdn.net/rocket5725/archive/2009/09/17/4563837.aspx

This chapter summarizes statements, functions, and standard I/O libraries.

Vi. Statements

The C ++ language includes simple statements that only complete a single task, and composite statements that are executed as a unit and composed of a group of statements. This chapter describes the statements supported by C ++.

1. The break statement is used to end the recent while, do while, for, or switch statements, and pass the execution right of the program to the statement followed by the terminated statement.

2. The continue statement causes the last iteration of the loop statement to end ahead of schedule.

3. Exception Handling for C ++ includes throw expressions, try blocks, and a group of exception classes defined by the standard library.

4. Use a pre-processor for debugging: $ CC-dndebug main. c

It also provides four constants that are useful during debugging.

If (word. size () <threshold) <br/> cerr <"error:" <__file _ // file name <br/> <": line "<__ line __< <Endl // current row number <br/> <" compiled on "<__ date _ // Date on which the file is compiled <br/> <"at" <__time __< <Endl // time when the file is compiled <br/> <"word read was" <Endl

5. Another debugging technique is to use ndebug preprocessing variables and assert preprocessing macros. The assert macro is defined in the cassert header file. All files using assert must contain this header file.

Assert (word. Size ()> threshold );

VII. Functions

This chapter introduces the definition and declaration of functions. It discusses how to pass parameters to functions and how to return values from functions, and then analyzes three types of special functions: inline functions, class member functions, and overload functions. Finally, we will discuss "function pointers ".

1. The function must specify the return type. It is invalid to explicitly specify the return type:

Test (double V1, double V2) {/*... */} // error: No returned value

2. Every time a function is called, all the parameters of the function will be re-created. The passed parameters will initialize the corresponding parameters. The initialization of the parameters is the same as that of the variables, if the parameter has a non-reference type, copy the value of the real parameter. If the parameter is of the reference type, it is only the alias of the real parameter.

3. Non-reference parameters

(1) pointer Parameters

# Include <iostream> <br/> using namespace STD; <br/> void reset (int * IP) <br/>{< br/> * IP = 0; <br/> IP = 0; <br/>}< br/> int main () <br/>{< br/> int I = 42; <br/> int * P = & I; <br/> cout <"I =" <* P <Endl; // output I = 42 <br/> Reset (p); <br/> cout <"I =" <I <Endl; // output I = 0 <br/> return 0; <br/>}

To protect the value pointed to by the pointer, the form parameter must be defined as the pointer to the const object.

Void use_ptr (const int * P) <br/>{< br/> // user_ptr may read but not write to * P <br/>}

(2) const parameter: You can pass a const real parameter to the function or a non-const real parameter.

Copying a real parameter is not applicable in any situation. It is not suitable for copying a real parameter, including: when you need to modify the value of a real parameter in a function, when you need to pass a large object as a real parameter, and when there is no way to replicate the object.

4. Reference parameters

(1) Use reference parameters to return additional information

(2) Avoid copying using const references

5. Main: process command line options

Int main (INT argc, char * argv []) {...}

6. Functions with variable parameters

Void Foo (parm_list,...); <br/> void Foo (...);

7. Never return a reference to a local object

Const string & manip (const string & S) <br/>{< br/> string ret = s; <br/> return ret; // error: local object references cannot be returned. <br/>}

8. inline functions: This avoids the overhead of function calls. inline functions should be defined in the header file, which is different from other functions.

Inline const string & shorterstring (const string & S1, const string & S2) <br/> {...}

9. pointer to a function: a pointer to a function rather than an object. The following PF Statement is a pointer to a function.

Bool (* PF) (const string &, const string &);

(1) Use typedef to simplify the definition of function pointers

// This definition indicates that cmpfcn is a name pointing to the pointer type of the function <br/> // the pointer type is pointing to the function that returns the bool type and has two const string reference parameters. pointer <br/> typedef bool (* cmpfcn) (const string &, const string &); <br/>

(2) initialization and assignment of pointer to function

Assume that a function is as follows. When the function name is referenced but it is not called, the function name is automatically interpreted as a pointer to the function.

Bool lengthcompare (const string &, const string &);

You can use the function name to initialize or assign values to the function pointer, as shown below:

Cmpfcn pf1 = 0; <br/> cmpfcn pf2 = lengthcompare; <br/> pf1 = lengthcompare; <br/> pf2 = pf1;

(3) call a function through a pointer: a pointer to a function can be used to call the function to which it points. You can call a function directly through a pointer without the need to use the unreferenced operator.

Cmpfcn pF = lengthcompare; <br/> lenthcompare ("hi", "bye"); <br/> PF ("hi", "bye "); <br/> (* PF) ("hi", "bye ");

8. Standard I/O Library

The input/output of C ++ is provided by the standard library. The standard library defines a family of types, supports reading/writing (IO) to files, control windows, and other devices, and defines other types, the string object can be operated like a file, so that data and characters can be converted without Io. This chapter introduces the basic knowledge of the I/O standard library.

1. The IO type is defined in three independent header files: iostream defines the type of the read/write control window, And fstream defines the type of the read/write named file, the type defined by sstream is used to read and write string objects stored in memory.

2. IO objects cannot be copied or assigned values.

Ofstream out1, out2; <br/> out1 = out2; // error: values cannot be assigned. <br/> ofstream print (ofstream ); <br/> out2 = Print (out2); // error: the stream object cannot be copied.,

The form parameter or return type cannot be the stream type. To pass or return an IO object, you must pass or return the pointer or reference of the specified object:

Ofstream & print (ofstream &); // OK: Take a reference, no copy <br/> while (print (out2 )){...} // OK: Pass reference to out2

3. condition state: the IO standard library manages a series of condition state members to mark whether the specified Io object is in the available State or the specific error.

The stream must be in the error-free status for input or output. The simplest way to check whether a stream is available is to check its true value:While (CIN> word) <br/> // OK: read operation successful

4. If you need to refresh all outputs, you 'd better use the unitbuf operator. This operator refresh the stream after each write operation:

Cout <unitbuf <"first" <"second" <nounitbuf; <br/> // equivalent to <br/> cout <"first" <flush <"second" <flush; <br/> // The nounitbuf operator restores the stream to a normally used, system-managed buffer refresh method.

5. The fstream header file defines three types of file I/O support: (1) ifstream, derived from istream, which provides the File Reading function; (2) ofstream, derived from ostream, provides the file writing function; (3) fstream, derived from iostream, provides the ability to read and write the same file. In addition to the inherited behaviors, the fstream type also defines two new operations, open and close, and the form parameter is the constructor of the file name to be opened.

6. Use of file stream objects

(1) check whether the file is successfully opened

Ifstream infile (ifile. c_str (); </P> <p> If (! Infile) <br/>{< br/> cerr <"error: Unable to open input file:" <br/> <ifile <Endl; <br/> return-1; <br/>}

(2) rebind the file stream with the new file: You must disable the current file stream before trying to open the new file.

Ifstream infile ("file1"); <br/> infile. Close (); <br/> infile. Open ("file2 ");

7. File mode:

The file Mode and Its Meaning are shown in the following table:

In Open a file and read it.
Out Open a file and write it.
App Find the end Of the file before each write
Ate After opening the file, immediately locate the file at the end of the file
Trunc Clear an existing file stream when opening the file
Binary Perform Io operations in binary mode

The out, trunc, and app modes can only be used to specify files associated with the ofstream or fstream object; the in mode can only be used to specify files associated with the ifstream or fstream object. All files can be opened in ATE or binary mode.

8. A program that opens and checks the input file: because the current status of the stream in is unclear, call close and clear to set the stream to a valid status, then try to open the specified file.

Ifstream & open_file (ifstream & in, const string & file) <br/>{< br/> in. close (); <br/> in. clear (); <br/> in. open (file. c_str (); <br/> return in; <br/>}

9. String stream

(1) istringstream, derived from istream, provides the read string Function

(2) ostringstream, derived from ostream, provides the string Writing Function

(3) stringstream, derived from iostream, provides the function of reading and writing strings.

 

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.