Reread Essential C ++ Reading Note 1

zhaozj2021-02-16  101

Reread Essential C Reading Note 1

A little time in the holiday, I plan to read Essential C this book. When I read this book, the time span was too big, causing it to have forgotten the front. I plan to focus on object-oriented.

By SSSA2000

7/24/2004

Chapter 1 C Edge

1, the definition and initialization of the object

In the book, we mentioned a rare initialization method. Generally, we are all like this:

INT A = 100; however, if there are multiple initial values, such as assigning a plurality of complexes, you need to have false parts and real part, then this method can not be used, you can use the constructor assignment method: Complex A (2 4) Indicates that a complex variable A is assigned (2, 4), and similarly, convert INT A = 100 into this method: INT A (100).

2, arrays and vectors

Compare the statement of two containers:

Int a [10];

Vector a (10);

Vector is more flexible than Array, but initialization is convenient than VECTOR.

When you initialize, if you write this sentence, the compiler will prompt error: Vector a (4) = (1, 2, 3, 4). Because Vector does not support initialization of this approach. Must this:

Vector a (4);

a [0] = 1;

a [1] = 2;

a [2] = 3;

a [3] = 4 ;;

Or use an array that has been initialized:

INT B [4] = {1, 2, 3, 4};

Vector a (b, b 4);

We know that the actual number of group names is the address of this array in memory, so it is passed to the VECTOR is the revelation and termination address of array B.

We can use A.Size () to get the size of the Vector, so the vector is more suitable for the length that is often changed.

3, still pointer

A few days ago, I wrote an article about the pointer in the C language. Here, I saw the pointer really kind.

The book is mentioned in the book to detect the importance of the empty pointer. It can be seen that the master is not the same. I have seen a lot of books. I rarely mentioned that I have to detect the empty pointer. In fact, the empty pointer is very dangerous.

Suppose there are several of the following vector: a, b, c, and we need to access these four vectors in the program, can declare an array:

Vector * e [4] = {& a, & b, & c, & d}

Here, e is an array, type is vector * so we can operate these four vectors by e [i].

4, file read and write:

Mainly the specific application of streams.

#include

#include

#include

Main ()

{

String a = "hello", b = "world";

OFSTREAM OUTFILE ("1.TXT");

IF (! outfile)

CERR << "Unable to open file";

Else Outfile << a << b << endl;

}

Of course we can use OFStream outfile ("1.txt", iOS_BASE :: App); to use Append mode

Here we use OFStream to write, we can of course use ifstream to read. Main ()

{

String a = "hello", b = "world", c;

IFStream Infile ("1.txt");

IF (! infile)

CERR << "Unable to open file";

Else

{INFILE >> C;

Cout << C;

}

}

(Chapter 1)

转载请注明原文地址:https://www.9cbs.com/read-13420.html

New Post(0)