在某些老版本的系统string中无法使用这两个函数:
stoi(): 它可以直接将string转化为int
int stoi (const string& str, size_t* idx = 0, int base = 10); int stoi (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to integer
Parses
str interpreting its content as an integral number of the specified
base, which is returned as an
int value.
If idx is not a null pointer, the function also sets the value of idx to the position of the first character in str after the number.
The function uses strtol (or wcstol) to perform the conversion (see strtol for more details on the process).
它可以被atoi()替换,它需要char*类型的参数,所以需要将string转化为char*,可以调用string.c_str()函数将标准的string转为char *
atoi():
int atoi (const char * str);
Convert string to integer
Parses the C-string
str interpreting its content as an integral number, which is returned as a value of type
int
.
The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
to_string():
string to_string (int val); string to_string (long val); string to_string (long long val); string to_string (unsigned val); string to_string (unsigned long val); string to_string (unsigned long long val); string to_string (float val); string to_string (double val); string to_string (long double val);
Convert numerical value to string
Returns a
string with the representation of
val.
The format used is the same that printf would print for the corresponding type:
type of val | printf equivalent | description |
---|---|---|
int | "%d" | Decimal-base representation of val. The representations of negative values are preceded with a minus sign (-). |
long | "%ld | |
long long | "%lld | |
unsigned | "%u" | Decimal-base representation of val. |
unsigned long | "%lu | |
unsigned long long | "%llu | |
float | "%f" | As many digits are written as needed to represent the integral part, followed by the decimal-point character and six decimal digits. inf (or infinity) is used to represent infinity. nan (followed by an optional sequence of characters) to represent NaNs (Not-a-Number). The representations of negative values are preceded with a minus sign (-). |
double | "%f | |
long double | "%Lf |
sprintf() 可以代替上面的to_string()在老版本上
int sprintf ( char * str, const char * format, ... );
Write formatted data to string
Composes a string with the same text that would be printed if
format was used on
printf, but instead of being printed, the content is stored as a
C string in the buffer pointed by
str.
The size of the buffer should be large enough to contain the entire resulting string (see snprintf for a safer version).
A terminating null character is automatically appended after the content.
After the format parameter, the function expects at least as many additional arguments as needed for format.
Parameters
-
str
-
Pointer to a buffer where the resulting C-string is stored.
The buffer should be large enough to contain the resulting string.
format
- C string that contains a format string that follows the same specifications as format in printf (see printf for details). ... (additional arguments)
-
Depending on the
format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a
format specifier in the
format string (or a pointer to a storage location, for
n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.