Manipulator of the C ++ input/output stream includes endl, flush, ws, and hex.
Cout <flush; // clears the stream
Cout
Cin> ws; // skip Spaces
Iostream. h also includes the following operators:
Operator |
Function |
Showbase/noshowbase |
When an integer is printed, the base number (decimal, octal, and hexadecimal) is indicated. The format used can be read by the C ++ compiler. |
Showpos/noshowpos |
Display positive value symbol plus (+) |
Uppercase/nouppercase |
Display the uppercase letter A-F representing the hexadecimal value and E in the scientific notation |
Showpoint/noshowpoint |
Indicates the decimal point of the floating point value and the following zero |
Skipws/noskipws |
Skip blank characters in the input |
Left |
Align left and fill right |
Right |
Right alignment, left Filling |
Internal |
Fill in between the pilot or base indicator and Value |
Scientific |
Use scientific notation |
Fixed |
Setprecision () or ios: precision () sets the number of digits after the decimal point |
How can we create our own operators?
We may want to create our own operators, which is quite simple. An operation operator without parameters such as endl is just a function that uses an ostream reference as its parameter. The statement for endl is:
ostream& endl(ostream&);
Example: generate a line feed without refreshing the stream. It is considered that nl is better than endl because the latter always clears the output stream, which may cause execution failure.
ostream& nl(ostream& os) {
return os << "\n";
}
int main() {
cout << "newlines" << nl << "between" << nl << "each" << nl << "word" << nl;
return 0;
}
The difference between const in C language and const in C ++:
Constants were introduced in the early C ++ version, when the standard C Specification was being developed. At that time, constants were considered a good idea and included in C. However, const in C means "a common variable that cannot be changed". In C, it always occupies storage and its name is a global character. The C compiler cannot regard const as a constant during compilation. In C, if you write:
const bufsize=100;
char buf[bufsize];
Although it seems to have done a reasonable thing, this will produce an incorrect result. Because bufsize occupies a certain place of storage, the C compiler does not know its value during compilation. In C, you can choose to write as follows:
const bufsize;
It is wrong to write it in C ++, while the C compiler uses it as a declaration, which indicates that there is storage allocation elsewhere. Because C's default const is an external connection, C ++'s default cosnt is an internal connection, so if you want to do the same thing in C ++, you must use extern to change the connection to an external connection:
extern const bufsize;//declaration only
This method can also be used in C.
Note: using the qualifier const in C language is not very useful, even in a constant expression (which must be obtained during compilation); To use a named value, using const is not very useful. C forces programmers to use # define in a Preprocessor.
Const and enum in the class
Is there any problem in writing the following statements? :
class bob {
const size = 100; // illegal
int array[size]; // illegal
}
Of course, the compilation fails. Why? Because const allocates storage space in class objects, the compiler cannot know what const content is, so it cannot be used as a constant during compilation. This means that for a constant expression in a class, const does not work as it does in C.
The const in the class indicates "this value remains unchanged during the life cycle of this specific object, rather than for the entire class ". So how to create a class constant that can be used in a constant expression?
A common method is to use a unlabeled enum without an instance. All values of the enumeration must be created during compilation. It is partial for the class, but the constant expression can get its value. As a result, we generally see:
class bob {
enum { size = 100 }; // legal
int array[size]; // legal
}
Using enum does not occupy the storage space of objects. Enumeration constants are fully evaluated during compilation. We can also explicitly set the enumerated constant value:enum { one=1,two=2,three};
Const member functions in the class
class X {
int i;
public:
int f() const;
}
Here f () is a const member function, indicating that only the const class object can call this function (the const object cannot call non-const member functions ), if we change any member of an object or call a non-const member function, the compiler sends an error message.
The const keyword must appear in the definition in the same way; otherwise, the compiler regards it as a different function:
int X::f() const { return i;}
Any function that does not modify member data should be declared as a const function, so that it can be used by the const object.
Note: const and destructor are not const member functions, because they always modify objects during initialization and cleaning.
Extended: how to modify a member in the const member function-by bit and by member constWhat if we want to create a const member function but still want to change some data in the object? This is related to the difference between the bitwise const and the member const. The bitwise const indicates that each bit in the object is fixed, so each bit of the object is never changed. By member const, although the entire object is conceptually unchanged, a Member may change. When the compiler notifies a const object, it protects the object.
Here we will introduce two methods to change data members in the const member function.
The first method is called "forced conversion const ". It is executed in a rather strange way. Take this (this keyword generates the address of the current object) and forcibly convert it into a pointer to the current type of object. It seems that this is a required pointer, but it is a const pointer. Therefore, we should forcibly convert it into a normal pointer so that constants can be removed in the operation. The following is an example:
class Y {
int i, j;
public:
Y() { i = j = 0; }
void f() const;
};
void Y::f() const {
//! i++; // error
((Y*)this)->j++; // ok , cast away const feature.
}
This method is feasible and can be seen in the previous program code, but this is not the preferred technology. The problem is: this is not decorated with const, which is hidden in the member function of an object. In this way, if the user cannot see the source code (and find the place where this method is used ), I don't know what happened.
- The second method is recommended, that is, using keywords in the class declaration.
mutable
To specify that a specific data member can be changed in a const object.
class Y {
int i;
mutable int j;
public:
Y() { i = j = 0; }
void f() const;
};
void Y::f() const {
//! i++; // error
((Y*)this)->j++; // ok , mutable.
}
Volatile keywords
The syntax of volatile is the same as that of const, but volatile means that "beyond the scope recognized by the compiler, this data can be changed ". Somehow, the environment is changing data (possibly through multi-task processing), so volatile tells the compiler not to make any assumptions about the data without authorization-this is especially important during optimization. If the compiler says, "I have read the data into the register and have no contact with the register ". Generally, it does not need to read the data again. However, if the data is modified by volatile, the compiler cannot make such assumptions, because it may have been changed by other processes. It must re-read the data rather than optimize the code.
Note:
- Just like creating a const object, programmers can also create a volatile object, or even create a const volatile object. This object cannot be changed by programmers, but can be changed through external tools.
- Like const, we can use volatile for data members, member functions, and objects, and can only call volatile member functions for volatile objects.
- The syntax of volatile is the same as that of const, so they are often put together for discussion. To indicate that either of the two can be selected. They are collectively referred to as the c-v qualifier.