Today, we will introduce in detail how to format C ++ sprintf. I hope that beginners can fully master this knowledge based on the Content introduced in this article and experience the different application methods that this language brings to us.
Name:
- Printf, sprintf-converts the output result to a specified format.
- Void printf (string format ,...);
- String sprintf (string format ,...);
Syntax:
- Basic concepts of C ++ function templates
- C ++ constant reference correct application method
- Interpretation of c ++ stack template application code
- Common application skills of C ++ clipboard
- C ++ Doxygen implementation function sharing
No return value printf (string format ,...);
String sprintf (string format ,...);
Usage: The format usage of LPC (s) printf () is as follows. Some extended usage of Lynscar (Sean A Reith) is added.
The current version supports the following modifier formats ):
"" Put a space before a positive integer.
"+" Put a + sign before a positive integer.
"-" Align the left of the field. note: the standard (s) printf () presets are aligned to the right, but this is very strange for languages that are mainly string-based. this feature is reserved in order to maintain compatibility with C.
"|" Align the center of the column.
"=" If the string length is longer than the column size, use column mode ). the C ++ sprintf format is only valid for strings, and Other types are invalid. in column mode, the characters in the string will automatically wrap (wrap ).
"#" Table mode. Within the column, each word in the string will change to a line (that is, \ n is added after each word). It is only valid for the string.
N specifies the size of the column as an integer. use * to replace the integer to specify the column size with parameters. if n is 0, this column is filled with null values (zeros) except data. if n is not 0, in addition to data, this column will be placed into space or other specified characters.
". "N indicates that the output precision is n. A normal string is truncated after this (if precision n is greater than the column length, the column length is changed to precision n ). the table uses precision to specify the total number of columns (if no precision is specified, the table automatically calculates the most suitable output result ). precision is invalid for other data types.
":" N specifies the fs and accuracy. If n is 0, the space originally used to fill the column is replaced by a null value (zeros ).
The "@" parameter is an array. In addition to @, the corresponding format data is also used for each element in the array.
The characters in "'X'" single quotation marks (') are used to fill up spaces other than data in the entire column. (the default value is a blank character) (if a null value is specified before the column size and the 'X' string is specified to fill the column, the future prevails.) Note: if you want to fill a string with single or double quotation marks ('), you must use \' or \ "to avoid compiler checks.
The format of the specified type in the C ++ sprintf format is as follows:
- % No parameters are specified here, but all modified formats (modifier) are ignored in % output ).
- The O parameter is of the LPC data type.
- The s parameter is a string.
- The d and I parameters are integers and printed in decimal places.
- The c parameter is an integer and printed as a character.
- The o parameter is an integer and is printed in octal.
- The x parameter is an integer and is printed in hexadecimal notation.
- The X parameter is an integer and is printed in hexadecimal notation (A to F ).
- F floating point number.
Return: sprintf () returns a string in the specified format.
The above is the acceptance of C ++ sprintf formatting.