stoi()和to_string()

来源:互联网 发布:mac网桥未连接 编辑:程序博客网 时间:2024/05/23 17:35

在某些老版本的系统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 specifiedbase, which is returned as anint value.

If idx is not a null pointer, the function also sets the value of idx to the position of the first character instr after the number.

The function uses strtol (orwcstol) to perform the conversion (seestrtol 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 typeint.

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 eitherstr 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 ofval.

The format used is the same that printf would print for the corresponding type:
type of valprintf equivalentdescriptionint"%d"Decimal-base representation of val.
The representations of negative values are preceded with a minus sign (-).long"%ldlong long"%lldunsigned"%u"Decimal-base representation of val.unsigned long"%luunsigned long long"%llufloat"%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"%flong 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 onprintf, but instead of being printed, the content is stored as aC string in the buffer pointed by str.

The size of the buffer should be large enough to contain the entire resulting string (seesnprintf 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 forformat.

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 asformat inprintf (seeprintf 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 aformat specifier in theformat string (or a pointer to a storage location, forn).
There should be at least as many of these arguments as the number of values specified in theformat specifiers. Additional arguments are ignored by the function.
函数功能说明来自参考网站:http://www.cplusplus.com/

0 0